8 Replies Latest reply on Apr 13, 2007 5:28 PM by spambob

    Validation messages

    nstoddar

      How much customization is possible in Seam for annotated Hibernate validations? I read the documentation, so I know that I can change the resource key to have a separate message rendered on the screen.

      My problem is this: in some cases I want a message "Contact first name is required.", but in other cases I might want to provide another context to the same validation, e.g. "Billing first name is required.".

      Is this even possible using the Hibernate Validation/Seam? To the best of my knowledge, it looks like I would have to have a 1-to-1 mapping between HTML form and Seam components for this to happen (in other words, no reuse of Seam components since there's no way to customize validation messages to this extent). ... Also a bummer since this precludes my ability to use my JPA model classes.

      Any ideas, or comments on this?

        • 1. Re: Validation messages

          Take a look at the message attribute of the Hibernate Validator annotations, they allow per field overrides of the default message. NotNull is still handled by JSF's required=true mechanism, but you have greater flexibility for other field constraints.

          • 2. Re: Validation messages
            nstoddar

             

            "CptnKirk" wrote:
            Take a look at the message attribute of the Hibernate Validator annotations, they allow per field overrides of the default message. NotNull is still handled by JSF's required=true mechanism, but you have greater flexibility for other field constraints.


            I don't want per-field messages. I want to provide some context to the message itself. For example, I would put

            @NotNull(message="{value.required}")
            value.required={0} is required.


            ... and magically be able to inject some value into {0} from somewhere else with "Contact name" or "Billing name", etc instead of just "Value is required." Does that make sense?

            • 3. Re: Validation messages

              Yep. It makes sense, it would be nice if you could override the validation message at the <s:validate> level. This has long been a complaint of JSF, and one that I believe will be fixed in JSF 2.0. JBoss will start using the 1.2 RI in 4.2, and that may allow some jsf-ext work to be used, including maybe support for what you're looking for.

              Until then, ask Gavin real nice for a <s:validate message="my message"> feature.

              Note though that NotNull is a special case in JSF. I believe that JSF will always handle required fields via its required=true mechanism so NotNull isn't a great example of using Hibernate Validator.

              Also, Seam provides EL and context aware markup in JSF resource bundles. I don't know if it extends this capability to Hibernate Validator resource bundles, I'd assume not.

              • 4. Re: Validation messages
                pmuir

                Apparently you can do

                @Length(min=10,max=20,message="#{messages.lengthValidatorMessage}")


                Then you can take advantage of Seam's per view message bundle feature.

                • 5. Re: Validation messages
                  nstoddar

                   

                  "petemuir" wrote:
                  Apparently you can do

                  @Length(min=10,max=20,message="#{messages.lengthValidatorMessage}")


                  Then you can take advantage of Seam's per view message bundle feature.


                  Do you have a link to some documentation on the "per view message bundle" feature? It sounds useful. In this situation, it wouldn't quite work. My problem is the reuse of fine-grained domain objects, for example an Address. I may have five addresses on a single form, so even if I could customize that message for that view, it would still render the same message for each address instead of "Home address ..." and "Work address ...", etc.

                  • 6. Re: Validation messages
                    pmuir
                    • 7. Re: Validation messages
                      pmuir

                      As CptnKirk says, create a JIRA request for overriding the message on the s:validate tag, sounds like the only way to do what you want...

                      • 8. Re: Validation messages
                        spambob

                        You can do what you want with a PhaseListener. See http://www.oracle.com/technology/pub/articles/masterj2ee/j2ee_wk7.html for more details (the "Adding Meaningful Field References" section) it describes a way doing this by using a PhaseListener that customizes the validation messages based on a parameter supplied by f:attribute tags inside the h:input stuff.

                        PS: +1 for the <s:validate message="my message"> feature.