3 Replies Latest reply on Jul 22, 2009 4:54 PM by niox.nikospara.yahoo.com

    Setting required attribute condition dynamically.

    ambrish_kumar

      Hi Everyone,


      How Can i set the required attribute condition dynamically so that it return true and false.


      My requirement is that for one field the required attribute will be true if a particular field is already filled otherwise required attribute will be false.




      <s:decorate id="nameDecoration" template="../layout/edit.xhtml">
                                                   <ui:define name="label">First Name</ui:define>
                                                   <h:inputText id="name" value="#{loggedInUser.firstName}" required="true" 
                                                   requiredMessage="First Name is required"/>
      
      </s:decorate>



      How can i set true/false value for required attribute depending on some other value.


      Thanks


      Ambrish

        • 1. Re: Setting required attribute condition dynamically.
          niox.nikospara.yahoo.com

          Hi,


          The only possible way to achieve this through JSF is to do a postback. You see the required field is part of the state of the view, saved after rendering and restored at the beginning of the restore view JSF phase.


          With RichFaces you can at least do an AJAX, partial postback as:


          <h:form>
            <h:selectBooleanCheckbox value="#{backingBean.requiredFlag}">
              <a:support
                event="onchange"
                reRender="xxx"
                ajaxSingle="true"
              />
            </h:selectBooleanCheckbox>
          
            <h:inputText
              id="xxx"
              value="#{backingBean.xxx}"
              required="#{backingBean.requiredFlag}"
            />
          </h:form>
          



          I am not sure if ajaxSingle="true" will work; try both true and false. If it does work, keep it. It will limit the network traffic and server load.



          The other way is to bypass JSF and use Javascript directly, to manipulate the input according to the value of the check box.

          • 2. Re: Setting required attribute condition dynamically.
            coralfe

            Answered here in the comments section.


            short answer:


            <h:inputText value="#{userCRUD.selectedRow.username}" size="30" required="#{!empty param['user_addEditForm:fieldDecoration2:pw']}"
                                maxlength="255"  id="user_First"/>

            • 3. Re: Setting required attribute condition dynamically.
              niox.nikospara.yahoo.com

              coral featherstone wrote on Jul 22, 2009 16:21:


              Answered here in the comments section.

              short answer:

              <h:inputText value="#{userCRUD.selectedRow.username}" size="30" required="#{!empty param['user_addEditForm:fieldDecoration2:pw']}"
                                  maxlength="255"  id="user_First"/>




              That's right, if you also want to couple your logic to the structure of the view AND the parameters of the view (ids) AND force yourself to define explicit ids for all the components in the hierarchy.