3 Replies Latest reply on Dec 19, 2008 5:10 AM by Ingo Jobling

    EL and hibernate validation not working

    Troy Sellers Newbie

      Hi All,


      I am getting an exception when I try the to resolve the value of a jsf field using EL in the tag (i am using ice faces). 
      The tag value is resolved on the UI page, however when i submit the form it seems that the EL is not resolved before the setter on the backing bean is called.


      We basically want to have the field label show up as a value inside the form, when the user clicks in the field the default value (the label) is removed so they can enter their data.  If they tab out of this field, the label needs to be inserted again. 


      So, some code / log snippets...


      An example of one of the input fields ...



      <s:validateAll>
      <ice:inputText id="customerDetails_firstNameInput" 
           value="#{customerDetails.firstName != null ? customerDetails.firstName : messages['customer.details.first.name.default']}" 
           size="15" 
           onfocus="javascript:clearInput(this, '#{messages['customer.details.first.name.default']}');"
           onblur="javascript:populateWithDefault(this, '#{messages['customer.details.first.name.default']}');"
      />
      </s:validateAll>



      The backing bean is simple getter and setter.


      Submitting the form causes the following validation errors



      09:50:31,717 INFO  [EARDeployer] Started J2EE application: file:/C:/java/jbdevstudio/jboss-eap/jboss-as/server/default/deploy/matadorLite-EAR.ear/
      09:52:43,933 INFO  [Contexts] starting up: org.jboss.seam.web.session
      09:52:43,933 INFO  [Contexts] starting up: org.jboss.seam.security.identity
      09:52:43,980 INFO  [AsyncServerDetector] Asynchronous HTTP Service available: false
      09:52:43,980 INFO  [AsyncServerDetector] JMS API available: true
      09:52:43,980 INFO  [AsyncServerDetector] Adapting to Send Updated Views environment.
      09:52:45,402 INFO  [STDOUT] Hibernate: 
          select
              configpara0_.paramKey as paramKey153_,
              configpara0_.paramValue as paramValue153_ 
          from
              dbo.ConfigParam configpara0_
      09:52:45,761 INFO  [OrderTypeActions] Destroying [class com.yum.matador.action.OrderTypeActions]
      09:52:48,714 INFO  [lifecycle] WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
      sourceId=j_id126:customerDetails_firstNameInput[severity=(ERROR 2), summary=(model validation failed:/customerDetails.xhtml @24,10 value="#{customerDetails.firstName != null ? customerDetails.firstName : messages['customer.details.first.name.default']}": Illegal Syntax for Set Operation), detail=(model validation failed:/customerDetails.xhtml @24,10 value="#{customerDetails.firstName != null ? customerDetails.firstName : messages['customer.details.first.name.default']}": Illegal Syntax for Set Operation)]
      sourceId=j_id126:customerDetails_surnameInput[severity=(ERROR 2), summary=(model validation failed:/customerDetails.xhtml @30,10 value="#{(customerDetails.surname != null ? customerDetails.surname : messages['customer.details.surname.default'])}": Illegal Syntax for Set Operation), detail=(model validation failed:/customerDetails.xhtml @30,10 value="#{(customerDetails.surname != null ? customerDetails.surname : messages['customer.details.surname.default'])}": Illegal Syntax for Set Operation)]
      sourceId=j_id126:customerDetails_phoneInput[severity=(ERROR 2), summary=(model validation failed:/customerDetails.xhtml @35,20 value="#{(customerDetails.phoneNumber != null ? customerDetails.phoneNumber : messages['customer.details.phone.default'])}": Illegal Syntax for Set Operation), detail=(model validation failed:/customerDetails.xhtml @35,20 value="#{(customerDetails.phoneNumber != null ? customerDetails.phoneNumber : messages['customer.details.phone.default'])}": Illegal Syntax for Set Operation)]
      sourceId=j_id126:customerDetails_emailInput[severity=(ERROR 2), summary=(model validation failed:/customerDetails.xhtml @40,20 value="#{(customerDetails.emailAddress != null ? customerDetails.emailAddress : messages['customer.details.email.default'])}": Illegal Syntax for Set Operation), detail=(model validation failed:/customerDetails.xhtml @40,20 value="#{(customerDetails.emailAddress != null ? customerDetails.emailAddress : messages['customer.details.email.default'])}": Illegal Syntax for Set Operation)]
      09:52:48,714 INFO  [OrderTypeActions] Destroying [class com.yum.matador.action.OrderTypeActions]



      It seems that my EL is not being evaluated before calling the Setter of the backing bean? Is this the case? How can I fix this?


      Cheers,
      Troy

        • 1. Re: EL and hibernate validation not working
          Ingo Jobling Master

          Hi Troy,


          The value attribute of the inputText tag must be a JSF Value Expression



          That is, it must be a property on a backing bean, with a getter and setter.


          So, it should look something like this:


          <ice:inputText value="#{customerDetails.firstName}" />



          assuming the customerDetails bean contains methods getFirstName and setFirstName.


          For the default values, getFirstName can be written to return the default value when firstName is null.  You may even want to add a transient getFirstNameDefault() method that you can call from getFirstName, as well as from the javascript methods, using #{customerDetails.getFirstNameDefault()}

          • 2. Re: EL and hibernate validation not working
            Troy Sellers Newbie

            Hi Ingo,


            Thanks for this, I guess is something that is starting to become clear to me after a good couple of hours of head-butting a monitor.


            The problem with the method you suggested is that I cannot get my resource bundle default value in my entity bean.


            In the xhtml form I can use #{messages['customer.details.default.first.name']}
            and this will be resolved.  However this isn't resolved in the backing bean.


            Trying to inject the resource bundle using either



            @In
            private ResourceBundle resourceBundle;




            OR



            @In 
            private Map<String, String> messages;




            results in null objects in both cases.  I would like to get a handle on the resource bundle in a number of seam components, however it always results in a null object when trying to use injection like this.


            The only other way would be to hard code these values in my backing beans, which kind of defeats the purpose for having them in the properties in the first place.


            Cheers,
            Troy

            • 3. Re: EL and hibernate validation not working
              Ingo Jobling Master

              In general, when injection fails, you can fall back to the Component.instance() methods.
              So, if you really want to, you can get the resource values from within the entity bean.


              It might be preferable to wrap the entity in a class which extends Seam's Controller class, which gets you easy access to resources, the entity manager, and some other goodies.  You can bind your jsf fields to methods on this class, rather than directly to the entity.  Of course, you have the extra work of copying values to and from the entity, but this pattern does give you a high degree of control, for example, setting default values, doing form level and model based validations, etc.