Validating empty fields using the built-in validation method and validation class

I have the following input field:

<h:inputText value=".." validator="#{labController.validateValue}"/> 

If the field is empty, it will not be checked ( validateValue in the labController not called).

But using a separate validation class:

 <h:inputText value=".." > <f:validator validatorId="labDateValidator"/> </h:inputText> 

can its validate method be called even with an empty input field?

This is what I am observing. Is this behavior implementation or version dependent (I'm using Mojarra 2.1)?

It is assumed that I want to use my own method / class for all validation tasks, including the required validation. Does it work only with the validator class?

+4
source share
1 answer

You are supervised by the JSF. validator enclosed in a MethodExpressionValidator , which, according to the validate() source, really skips validation when null .

 if (value != null) { try { ELContext elContext = context.getELContext(); methodExpression.invoke(elContext, new Object[]{context, component, value}); ... 

This does not consider the new JSF 2.0 context parameter javax.faces.VALIDATE_EMPTY_FIELDS , which is true by default. In fact, it should do the same as UIInput#validate() .

 if (!isEmpty(newValue) || validateEmptyFields(context)) { try { ELContext elContext = context.getELContext(); methodExpression.invoke(elContext, new Object[]{context, component, value}); ... 

This has already been reported as a Mojarra 1508 issue . I voted for it and added a new comment to wake them up.

+6
source

Source: https://habr.com/ru/post/1398050/


All Articles