7 Replies Latest reply on Jan 17, 2008 12:17 AM by konami

    Validation: @NotNull && required=true

    mvlach

      I have problem with this:

      HibernateValidator is run when value is not empty - I have to use required="true" - OK

      But, why I have to see the default UIInput validation message javax.faces.component.UIInput.REQUIRED, when I have my own @NotNull(message="messages['something.better']").

      Is there any solution to disable the UIInput default validation message ?

      Thanks Mila

        • 1. Re: Validation: @NotNull && required=true
          tony.herstell1

          I think thisis what you are after:

          javax.faces.component.UIInput.CONVERSION Conversion error occurred
          javax.faces.component.UIInput.REQUIRED required
          javax.faces.component.UISelectOne.INVALID Value is not a a valid option
          javax.faces.component.UISelectMany.INVALID Value is not a valid option
          javax.faces.validator.NOT_IN_RANGE Specified attribute is not between the expected values of {0} and {1}
          javax.faces.validator.DoubleRangeValidator.MAXIMUM Value is greater than allowable maximum of '{0}'
          javax.faces.validator.DoubleRangeValidator.MINIMUM Value is less than allowable minimum of '{0}'
          javax.faces.validator.DoubleRangeValidator.TYPE Value is not of the correct type
          javax.faces.validator.LengthValidator.MAXIMUM Value is greater than allowable maximum of '{0}'
          javax.faces.validator.LengthValidator.MINIMUM Value is less than allowable minimum of '{0}'
          javax.faces.validator.LongRangeValidator.MAXIMUM Value is greater than allowable maximum of '{0}'
          javax.faces.validator.LongRangeValidator.MINIMUM Value is less than allowable minimum of '{0}'
          javax.faces.validator.LongRangeValidator.TYPE Value is not of the correct type
          
          


          put into a file in my War Src area.

          I refer to it in my faces.config

          <!-- My Messages Please -->
          <application>
          <message-bundle>nz.co.risingstars.i18n.jsfstandarderrors</message-bundle>
          </application>
          


          This is on top of the messages you put into the messages.properies in the jar.

          (there are a LOT more message that just this list!):
          javax.faces.component.UIInput.CONVERSION error
          javax.faces.component.UIInput.CONVERSION_detail = invalid
          javax.faces.component.UIInput.REQUIRED required
          javax.faces.convert.DateTimeConverter.CONVERSION = must be a valid date
          #javax.faces.convert.DateTimeConverter.CONVERSION_detail = value must be a datetime
          
          #javax.faces.component.UISelectOne.INVALID Value is not a a valid option
          #javax.faces.component.UISelectMany.INVALID Value is not a valid option
          #javax.faces.validator.NOT_IN_RANGE Specified attribute is not between the expected values of {0} and {1}
          #javax.faces.validator.DoubleRangeValidator.MAXIMUM Value is greater than allowable maximum of '{0}'
          #javax.faces.validator.DoubleRangeValidator.MINIMUM Value is less than allowable minimum of '{0}'
          #javax.faces.validator.DoubleRangeValidator.TYPE Value is not of the correct type
          #javax.faces.validator.LengthValidator.MAXIMUM Value is greater than allowable maximum of '{0}'
          #javax.faces.validator.LengthValidator.MINIMUM Value is less than allowable minimum of '{0}'
          #javax.faces.validator.LongRangeValidator.MAXIMUM Value is greater than allowable maximum of '{0}'
          #javax.faces.validator.LongRangeValidator.MINIMUM Value is less than allowable minimum of '{0}'
          #javax.faces.validator.LongRangeValidator.TYPE Value is not of the correct type
          
          #javax.faces.convert.DateTimeConverter.CONVERSION = Conversion Error
          #javax.faces.convert.DateTimeConverter.CONVERSION_detail = "{1}": xxSpecified value is not a valid date/time.
          #javax.faces.converter.DateTimeConverter.DATE=value must be a date
          #javax.faces.converter.DateTimeConverter.DATE_detail=value must be a date, eg. {1}
          #javax.faces.converter.DateTimeConverter.TIME=value must be a time
          #javax.faces.converter.DateTimeConverter.TIME_detail=value must be a time, eg. {1}
          #javax.faces.converter.DateTimeConverter.DATETIME=value must be a date and time
          #javax.faces.converter.DateTimeConverter.DATETIME_detail=value must be a date and time, eg. {1}
          #javax.faces.converter.DateTimeConverter.PATTERN_TYPE=a pattern or type attribute must be specified to convert the value
          #JSF 1.1:
          #javax.faces.convert.BigDecimalConverter.CONVERSION = value must be a number
          #javax.faces.convert.BigDecimalConverter.CONVERSION_detail = value must be a number
          #javax.faces.convert.BigIntegerConverter.CONVERSION = value must be an integre
          #javax.faces.convert.BigIntegerConverter.CONVERSION_detail = value must be a number
          #javax.faces.convert.BooleanConverter.CONVERSION = value must be true or false
          #javax.faces.convert.BooleanConverter.CONVERSION_detail = value must be true or false
          #javax.faces.convert.ByteConverter.CONVERSION = value must be a byte
          #javax.faces.convert.ByteConverter.CONVERSION_detail = value must be a byte
          #javax.faces.convert.CharacterConverter.CONVERSION = value must be a character
          #javax.faces.convert.CharacterConverter.CONVERSION_detail = value must be a character
          #javax.faces.convert.DoubleConverter.CONVERSION = value must be a number
          #javax.faces.convert.DoubleConverter.CONVERSION_detail = value must be a number
          javax.faces.convert.FloatConverter.CONVERSION = value must be a number
          javax.faces.convert.FloatConverter.CONVERSION_detail = value must be a number
          #javax.faces.convert.IntegerConverter.CONVERSION = value must be an integer
          #javax.faces.convert.IntegerConverter.CONVERSION_detail = value must be an integer
          #javax.faces.convert.LongConverter.CONVERSION = value must be an integer
          #javax.faces.convert.LongConverter.CONVERSION_detail = value must be a long integer
          javax.faces.convert.NumberConverter.CONVERSION = value must be a number
          javax.faces.convert.NumberConverter.CONVERSION_detail = value must be a number
          #javax.faces.convert.ShortConverter.CONVERSION = value must be an integer
          #javax.faces.convert.ShortConverter.CONVERSION_detail = value must be a short integer
          


          and you can in8l them of course.



          • 2. Re: Validation: @NotNull && required=true
            tony.herstell1

            If this is helpful then you might want to propose to add it to the FAQ...

            • 3. Re: Validation: @NotNull && required=true
              mvlach

              Thanks for reply, but I don't know how to disable the displaying the UIInput default validation message. When the field isn't empty, the hibernate validation work good.
              If the input is empty the UIInput default message is show - and I would like to don't show this but show the @NotNull.

              M.

              • 4. Re: Validation: @NotNull && required=true
                christian.bauer

                Currently there is no way to utilize the message of @NotNull, you need to change the JSF validator message for required="true" globally.

                • 5. Re: Validation: @NotNull && required=true
                  monkeyden

                  There is no way to do it globally but you can use a phase listener:

                  import java.util.Iterator;
                  
                  import javax.faces.application.FacesMessage;
                  import javax.faces.component.UIComponent;
                  import javax.faces.component.UIViewRoot;
                  import javax.faces.context.FacesContext;
                  import javax.faces.event.PhaseEvent;
                  import javax.faces.event.PhaseId;
                  import javax.faces.event.PhaseListener;
                  
                  /**
                   * Example messages.properties entries:
                   * javax.faces.validator.LengthValidator.MAXIMUM="[fieldLabel]" value is greater than allowable maximum of {0} characters.
                   * javax.faces.validator.LengthValidator.MINIMUM="[fieldLabel]" value must contain at least {0} characters.
                  */
                  
                  @SuppressWarnings("serial")
                  public class MessageListener implements PhaseListener {
                  
                   public PhaseId getPhaseId() {
                   return PhaseId.RENDER_RESPONSE;
                   }
                  
                   public void beforePhase(PhaseEvent e) {
                   FacesContext fc = e.getFacesContext();
                   UIViewRoot root = fc.getViewRoot();
                   Iterator i = fc.getClientIdsWithMessages();
                   while (i.hasNext()) {
                   String clientId = (String) i.next();
                   if(clientId != null) {
                   UIComponent c = root.findComponent(clientId);
                   if (c != null) {
                   String fieldRef =
                   (String) c.getAttributes().get("fieldLabel");
                   if (fieldRef != null) {
                   Iterator j = fc.getMessages(clientId);
                   while (j.hasNext()) {
                   FacesMessage fm = (FacesMessage) j.next();
                   String s = fm.getSummary();
                   s = s.replaceFirst("\\[fieldLabel\\]", fieldRef);
                   fm.setSummary(s);
                   }
                   }
                   }
                   }
                   }
                   }
                  
                   public void afterPhase(PhaseEvent e) {
                   }
                  }
                  


                  Then specify the fielLabel in the f:attribute tag:

                  <h:inputText value="#{contactUs.email}" size="50" maxlength="75" required="true">
                  <f:attribute name="fieldLabel" value="Email"/>
                  </h:inputText>


                  Or, if you require completely different messages for each component, you could specify a resource bundle key
                  <h:inputText value="#{contactUs.email}" size="50" maxlength="75" required="true">
                  <f:attribute name="bundleKey" value="contactus.error.email.required"/>
                  </h:inputText>


                  Using the latter approach, you would get the resource bundle key from the component attributes map in the listener and replace the summary text altogether.

                  • 6. Re: Validation: @NotNull && required=true
                    monkeyden

                    On second thought, this is of no use to you within the context of hibernate validation. This is strictly JSF. I'll be quiet now.

                    • 7. Re: Validation: @NotNull && required=true
                      konami

                      As of JSF 1.2, the h:inputText control has requiredMessage attribute that you can customize for each input field's required error message.