Alberto
Alberto

Reputation: 2982

Two required and depending fields

I have two required fields, the second depends from the first, I mean, the second field must be disabled when the first one is empty.

At start of the process everything is good, when I fill the first field the second one appears as enable, but when I clean the first field, the validation make an error and the second field is still enable.

<h:inputText required="true" value="#{x.x}" />
<h:inputText required="true" value="#{x.y}" disabled="#{empty x.x}" />

How can I make it works properly?

Upvotes: 0

Views: 58

Answers (1)

BalusC
BalusC

Reputation: 1108632

Your disabled attribute checks the model value. Model values are however not updated on validation fail. You're then basically checking the previously submitted model value.

Better check the submitted value itself instead, which is available as a HTTP request parameter identified by the component's client ID:

<h:inputText binding="#{c}" ... required="true" />
<h:inputText ... required="true" disabled="#{empty param[c.clientId]}" />

Or, additionally check if the component is still valid:

<h:inputText binding="#{c}" ... required="true" />
<h:inputText ... required="true" disabled="#{empty c.value or not c.valid}" />

Regardless, those only doesn't cover the case when you fill out both fields and then remove only the first. The second will appear as disabled, but is still filled out. It's not clear from the question what exactly you want to do in such case, so I'll leave it up to you as an exercise.

Upvotes: 1

Related Questions