开发者

Dynamic required validation for different buttons?

I'm currently learning about jsf 2.0 from core jsf 2.0 book + glassfish + cdi.

I have a question about the validation feature of JSF.

Let's say i have a very simple login application, which has a very simple layout like :

userid : (in开发者_StackOverflow社区put field for userid - using required="true")

password : (input secret for password - using required="true")

loginButton + registerButton(using immediate="true") + checkUserIdAvailabilityButton

Now, let's say the loginButton is pressed, and the userid and password are left empty, validation error would occur on both of the fields, and that's working as what i intended.

And when the registerButton is pressed, it doesnt care whether the userid or password is filled by the user, since it's using immediate="true", thus bypassing validation and the command gets executed at the apply request value phase, and that's still working as what i intended.

And here comes my problem .. When the checkUserIdAvailabilityButton is pressed, i only expect the userid to be filled, and i dont need to care about whether the password field is filled or not, but the password field will throw error saying that it's a required field.

Is there anyway to resolve this kind of problem ? I know this could be a very simple application, but in my working place, i think they design lots of screens like this, like the save button along with refresh button with different required fields but the buttons are in the same page.

Thank you !


Set both the availability button and username field with immediate="true" and put the register button in a separate <h:form>.

E.g.

<h:form>
     <h:inputText value="#{bean.username}" required="true" immediate="true" />
     <h:inputSecret value="#{bean.password}" required="true" />
     <h:commandButton value="Login" action="#{bean.login}" />
     <h:commandButton value="Check name availability" action="#{bean.checkNameAvailability}" immediate="true" />
</h:form>
<h:form>
     <h:commandButton value="Register" action="#{bean.register}" />
</h:form>

An alternative is to determine in the required attribute which button is been pressed (it's then present as request parameter).

<h:form id="form">
     <h:inputText value="#{bean.username}" required="#{not empty param['form:login'] or not empty param['form:check']}" />
     <h:inputSecret value="#{bean.password}" required="#{not empty param['form:login']}" />
     <h:commandButton id="login" value="Login" action="#{bean.login}" />
     <h:commandButton id="check" value="Check name availability" action="#{bean.checkNameAvailability}" />
     <h:commandButton value="Register" action="#{bean.register}" />
</h:form>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜