Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip required validation and invoke the application

Tags:

jsf

jsf-2

I asked my question here but I think it lacks some obvious information. Hence I'm posting my question again.

I have a JSF form which has some required validation on h:inputText fields. When submitting the form I want to skip(or ignore) the required validation check and still invoke the application. I do want to do the form data validation and show the errors based on some condition.

Basically I want to skip the validations and invoke the application when the form is submitted using an ajax call and do the validation when the form is submitted via clicking a submit button.

like image 991
Srini Kandula Avatar asked Dec 26 '22 18:12

Srini Kandula


1 Answers

Just put that condition straight in the required attribute.

<h:inputText ... required="#{someCondition}" />

It namely just accepts any EL expression like as many other attributes. Many starters think that you can only hardcode a "true" or "false" string in it. This is untrue.

For example, when you want to let it evaluate true only when the save button is actually pressed:

<h:inputText ... required="#{not empty param[save.clientId]}" />
...
<h:commandButton value="Cancel" ... />
<h:commandButton binding="#{save}" value="Save" ... />

(note: code is complete as-is, you do not need to bind it to a bean property)

This way the required attribute only evaluates true when the save button is pressed and it evaluates false for any other button or ajax event listener.

like image 71
BalusC Avatar answered Jan 12 '23 03:01

BalusC