Individual error message for each jsf validation tag

I have an input field that must comply with the following constraints: the input must have exactly 2 characters, it must only accept letters and numbers, and it must be uppercase. So far I have done the following:

<h:inputText id="code" >
        <f:validateLength minimum="2" maximum="2" />
        <f:validateRegex pattern="^[a-zA-Z0-9]*$"/>
        <f:validateRegex pattern="^[A-Z0-9]*$"/>
</h:inputText>

      

However, I need separate error messages when any of these rollbacks complete . I did a search and I found the following unacceptable solutions:

1) How to customize the JSF validation error message : Providing an attribute validatorMessage

inside a text tag will not do the job because this way I can only provide one message for each text input tag.

2) http://incepttechnologies.blogspot.ro/p/validation-in-jsf.html : the validation method in the bean backup or imperative validation using annotation is @FacesValidator

not good because that is what I am trying to avoid; i want to move checkout from back-end to front-end

3) https://www.mkyong.com/jsf2/customize-validation-error-message-in-jsf-2-0/ : overriding the error message in is messages.properties

not suitable for two reasons: 1. I want to use custom messages about errors only locally (page area), not application-wide. 2. I have the same validator tags that happen twice, but with different templates, and I want a different error message to appear in each situation.

Documentation for validation tags ( http://docs.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/ and http://docs.oracle.com/javaee/6/javaserverfaces/2.0/docs/ pdldocs / facelets / f / validateLength.html ) does not, as I hoped, specify any attributes like "message" or "errorMessage".

+3


source to share


2 answers


I don't have a tFied Kt, but I think the worry would be to put the custom overridden validation messages in a specific properties file (which can only be used for this case) and use the locale attribute on the f: view tag to set the page-level locale so that the messages displayed from a set of locale-specific messages.



+1


source


The workaround is to combine all error messages into one big message. If any validation fails, the corresponding message is included in the large message anyway.

So, I used the attribute validatorMessage

in the tag inputText

. Also, to avoid getting a big message multiple times (when multiple checks worked), I replaced 3 checks with an equivalent one:



<f:validateRegex pattern="^[A-Z0-9]{2}$"/>

      

0


source







All Articles