Two required and depending fields

时间:2016-07-11 19:55:12

标签: validation jsf

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?

1 个答案:

答案 0 :(得分:1)

您的disabled属性会检查模型值。但是,验证失败时模型值不会更新。然后,您基本上检查先前提交的模型值。

更好地检查提交的值本身,它可以作为由组件的客户端ID标识的HTTP请求参数提供:

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

或者,另外检查组件是否仍然是valid

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

无论如何,当你填写两个字段然后只删除第一个字段时,那些只是不能覆盖这个案例。第二个将显示为已禁用,但仍会填写。从这个问题中你不清楚你在这种情况下究竟想做什么,所以我会把它留给你作为练习。