3 Replies Latest reply on Aug 16, 2008 8:12 PM by Mikael Andersson

    How to validate 4 properties REQUIRED on Update and 5 on Regiter? (newbie from WW to Seam)

    Daniel Vanzin Newbie

      Hi, I'm new to JSF/Seam and comming from Webwork (aka Strust2). In WW, when I update a user, I have 2 user objects. One from the database and one from the view. Only the view is validated, and then both are merged.


      One reason to do this is that not all the object's properties need validation, only the ones the user had the chance to update.


      I do this by saying which properties need to be validated in a XML file. This file is specific to an action, so I have like 5 properties that are REQUIRED for register in RegisterAction-validation.xml and only 4 for update UpdateAction-validation.xml.


      To prevent cheating, I can also say in another XML file say which properties cannot be set, so my merging will not be compromised.


      What concept relates to this or how would I do this in Seam?


      thanks

        • 1. Re: How to validate 4 properties REQUIRED on Update and 5 on Regiter? (newbie from WW to Seam)
          Mikael Andersson Master

          Hi


          To make sure something is required you should use the required attribute which is available for all input elements.


          Example:

          <h:inputText value="#{backingBean.property}" required="true"/>


          The value inside the required attribute can be an EL expression, so if you load your XML files required and editable settings into an application scoped backing bean (if it is same for all users, otherwise session scope) you could do something like this:



          <h:inputText value="#{backingBean.reqProperty}" required="#{requiredManager.isRequired('reqProperty')}"/>


          Which would call a boolean isReqProperty(String propertyName) method on you backing bean. This would be overkill if this setting is static, but perhaps useful if it is dynamic.


          Note: passing parameters to methods in EL expressions is something which works in Seam but not in standard JSF.


          Hope this helps a bit,
          Micke

          • 2. Re: How to validate 4 properties REQUIRED on Update and 5 on Regiter? (newbie from WW to Seam)
            Daniel Vanzin Newbie

            Thanks for the answer. I forgot to mention though that I would like to use the Hibernate Validator approach (@NotNull, s:validateAll, ClassValidator.getInvalidValues...).


            Also, the required part I understood, but what about the forbidden ? For example, if a user is editing his profile, and he can see his username, but not change it, he could HTTP temper the request to alter his username. What part of JSF/Seam prevents this?

            • 3. Re: How to validate 4 properties REQUIRED on Update and 5 on Regiter? (newbie from WW to Seam)
              Mikael Andersson Master

              Can't help you with the hibernate validators.


              About the read-only input text, try the normal readonly attribute which should work. I would probably simply use an outputText instead though.


              Something like this:


              <h:inputText value="#{backingBean.username}" rendered="#{backingBean.username ne user.username}"/> 
              <h:outputText value="#{backingBean.username}" rendered="#{backingBean.username eq user.username}"/> 
              



              using some EL to determine if input or output text should be displayed


              - micke