不同按钮的动态需要验证? [英] Dynamic required validation for different buttons?

查看:114
本文介绍了不同按钮的动态需要验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从核心jsf 2.0 book + glassfish + cdi学习jsf 2.0。

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

我对JSF的验证功能有疑问。

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 :(输入字段) for userid - using required =true)

密码:(密码输入密码 - 使用required =true)

loginButton + registerButton(using immediate =true) + checkUserIdAvailabilityButton

userid : (input field for userid - using required="true")
password : (input secret for password - using required="true")
loginButton + registerButton(using immediate="true") + checkUserIdAvailabilityButton

现在,假设按下了loginButton,并且用户ID和密码都是空的,两个字段都会出现验证错误,那就是我打算。

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.

当按下registerButton时,它不关心用户是否填写了用户ID或密码,因为它使用的是immediate =true,因此绕过验证和命令在应用请求值阶段执行,并且仍然像我一样工作打算。

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.

这就是我的问题..当按下checkUserIdAvailabilityButton时,我只希望填充用户ID,而我不需要关心密码字段填充与否,但密码字段会抛出错误,说明它是必填字段。

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.

谢谢!

推荐答案

设置可用性按钮和用户名字段 immediate =true 将注册按钮放在单独的 < h:form> ;

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

例如

<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>

这篇关于不同按钮的动态需要验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆