5 Replies Latest reply on Apr 2, 2014 12:41 PM by Matija Vizintin

    rich:select custom label in validation message

    uwew Newbie

      Using Richfaces 4.3.5.Final we came across a question concerning the labels of validation messages for rich:select input.

       

      We tried to use rich:select like the following:

       

      <rich:panel style="width:220px;">

          <h:outputLabel for="test3" value="Simple select" />

          <rich:select required="true" id="test3">

              <f:selectItem itemValue="0" itemLabel="Option 1" />

              <f:selectItem itemValue="1" itemLabel="Option 2" />

              <f:selectItem itemValue="2" itemLabel="Option 3" />

              <f:selectItem itemValue="3" itemLabel="Option 4" />

              <f:selectItem itemValue="4" itemLabel="Option 5" />

              <rich:validator />

          </rich:select>

          <rich:message for="test3" />                       

      </rich:panel>

       

      When submitting the surrounding form without selecting a value the following error message is displayed:

       

          pb48145:personEditForm:test3: Validation error: value is required

       

      As we have set a custom output label, we would expect a message of that kind:

          Simple select: Validation error: value is required

       

      Is it possible to force the validator to use a custom label instead of the generated element ids? If yes, can anyone show me a hint.

       

      Kind regards,

      Uwe

        • 1. Re: rich:select custom label in validation message
          Dan James Newbie

          Hi Uwe,

           

          I've just found your question from a google search, having encountered a similar problem.

           

          In this case it looks like you could provide a requiredMessage attribute, e.g.:

           

          <rich:select required="true" id="test3" requiredMessage="Simple select: please select a value">

           

          These seem to override the automatic <rich:validator> messages.  It's not a very dynamic solution though - you'd have to specify the whole message each time.

           

          My issue is slightly different - I was experimenting with <rich:notifyMessages> to display the errors to the user on the right hand side of the screen.  I wasn't planning on specifying individual messages, but to just use those generated by <rich:validator> based on the JPA validation constraints placed on the backing entity bean.  However, I've run into the same issue as you that any label specified is not prefixed to the message, so it's not a very user-friendly approach for large forms as the link between field and error is lost.

           

          Did you find any other way of getting round this?

           

          Cheers,

          Dan

          • 2. Re: rich:select custom label in validation message
            Matija Vizintin Newbie

            If you use BeanValidation you can add ValidationMessages.properties with your custom messages.

            • 3. Re: rich:select custom label in validation message
              Michal Petrov Master
              <h:outputLabel for="text" value="Some label" /><h:inputText id="text" />
              <h:inputText id="text" label="Some label" />
              

              These two rows don't do the same thing, the first one will just create a piece of text on your page. The second one will create a label that can be used in validation messages.

               

              <rich:select> doesn't have a label attribute, though as pointed out you can use the requiredMessage attribute.

              • 4. Re: rich:select custom label in validation message
                Dan James Newbie

                Thanks Matija,


                I will be using BeanValidation, but the OP was using one of the built-in validation options in RF.  (I'm new to this, so this SO answer helped clarify the difference for me).

                I've looked into the ValidationMessages.properties option further, but I'm considering having the customised message defined explicitly with the constraint.  E.g.:


                In MyBean.java:


                     @Size(min=3,max=50,message="MyBean name should be between {min} and {max} characters long")

                     @NotNull

                     @Column(unique=true)

                     private String name;


                Note that in this particular case using @Size instead of @Length is more portable, but I wanted the message to be more user-friendly so I have overridden the whole thing.


                An alternative approach to prefix a custom 'label' to the standard output would be:


                     @Size(min=3,max=50,message="Name: {javax.validation.constraints.Size.message}")


                Resulting message would be:  "Name: size must be between 3 and 50"  based on the default ValidationMessages.properties values.


                I don't think either of these is perfect though..


                Apologies for the hijack uwew.  It looks like Michal Petrov has explained why the label attribute does not work for this component type. Additionally, you may find this link has a usable method for removing client IDs from standard JSF validation messages.  In any case, it's been more than a month, so you've undoubtedly found your own way around this...


                Cheers,

                Dan

                • 5. Re: rich:select custom label in validation message
                  Matija Vizintin Newbie

                  I would override javax.validation.constraints.Size.message for example with your desired message instead of defining it directly into message property of @Size. This way you won't lose internationalization. This is working for me.