Search code examples
validationjsfbindingjsf-1.2myfaces

Conditional validation without binding attribute


I'm dealing with a legacy code base and come across a situation when it's necessary to validate a field "fieldToValidate" if some other field "otherField" has some value (otherwise field is not validated). However the field "otherField" doesn't have a binding attribute. I can add a binding and then update code like this:

<h:inputTextarea id="fieldToValidate" value="#{MyBean.fieldToValidate}" 
required="#{MyBean.otherField != 'special_value'}" />

However there is a plenty of places where validation should be added and I don't want to modify backing beans. Is there a way to implement validation without adding "binding"?

Validation with some JS library is not a option.


Solution

  • You do not necessarily need to bind it to a bean property. Just omit the MyBean. part to bind it to the view scope directly.

    <h:selectOneMenu binding="#{otherField}" ... />
    ...
    <h:inputTextarea ... required="#{otherField != 'special_value'}" />
    

    See also: