如何验证值瞬间,但检查只能在全提交所需? [英] How to validate values instantly, but check for required only on full submit?

查看:147
本文介绍了如何验证值瞬间,但检查只能在全提交所需?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前添加验证一个表单。有两件事情要检查:值本身的正确性(例如,如果它是一个正整数,或者有效的电子邮件)和所有必填字段是否已填写

I am currently adding validation to a form. There are two things to check: The correctness of the value itself (e.g. if it is a positive integer or valid email) and whether all required fields were filled in.

然而,如果通过某种方式( F:AJAX 或ICEfaces的 partialSubmit 属性)我做的验证类型正确性同时发生(例如,当字段失去焦点),它也将检查要求属性在此同一步骤。在大多数情况下,这是没有问题的,因为用户已经输入的值,并有可能进行校正,而不是返回到一个空白字段。

However, if by some means (f:ajax or IceFaces partialSubmit attribute) I make the validation for type correctness happen at once (e.g. when the field loses focus), it will also check the required attribute in this same step. In most cases this is no problem, as the user already entered a value and is likely to correct it rather than go back to a blank field.

然而,在他实际上希望再次清除该字段的情况下,他再也不能这样做没有得到一个错误。结果,我只是想检查所需的烦躁领域对最后提交页面。

However, in the case where he actually wants to clear the field again, he can no longer do so without getting an error. In consequence, I only want to check the required-ness of fields on finally submitting the page.

目前,我唯一的想法分开的两个验证类型是执行所有需要核对的后盾豆操作方法,从而尝试将其直接通过按钮,最后提交。

At the moment, my only idea to separate the two validation types is by performing all required-checks in the backing beans action method, thus tying it to directly to the final submit via button.

有另一种方式?

(对于背景:为什么人们可能想要再次清除字段的原因是要求可依赖于其它选择的形式改变因此,人们可能会决定毕竟,只有在该正确选择不提供这一领域。实际上使得这个领域可选的。)

(For background: The reason why one might want to clear the field again is that requirements can change depending on other selections in the form. So one might decide to not provide this field after all and only after that correct the option that actually makes this field optional.)

推荐答案

只是让要求属性评估当提交按钮确实是pressed。

Just let the required attribute evaluate true when the submit button has really been pressed.

答案却取决于你的提交按钮的方式执行它的逻辑(标准 F:AJAX ,ICEfaces的,等等)。但它基本上可以归结为,你可以检查的请求参数映射这表明期望的提交按钮已被pressed请求参数。

The answer however depends on the way how your submit button executes its logic (standard, f:ajax, ICEfaces, etc). But it basically boils down to that you could check the request parameter map for a request parameter which indicates that the desired submit button has been pressed.

例如,如果它是一个标准的命令按钮:

E.g., if it's a standard command button:

<h:form id="form">
    ...
    <h:commandButton id="submit" value="Submit" action="#{bean.submit}" />
</h:form>

然后,你可以检查它通过检查按钮的客户端ID是present请求参数图:

Then you could check for it by checking if the button's client ID is present in the request parameter map:

<c:set var="submitButtonPressed" value="#{not empty param['form:submit']}" />
...
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />

或者,如果它是一个&LT; F:AJAX&GT; 键:

<h:form id="form">
    ...
    <h:commandButton id="submit" value="Submit" action="#{bean.submit}">
        <f:ajax execute="@form" ... />
    </h:commandButton>
</h:form>

然后,你可以通过检查 javax.faces.source 参数等于按钮的客户端ID检查:

Then you could check it by checking if javax.faces.source parameter equals the button's client ID:

<c:set var="submitButtonPressed" value="#{param['javax.faces.source'] == 'form:submit'}" />
...
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />

您甚至可以结合两种:

<c:set var="submitButtonPressed" value="#{not empty param['form:submit'] or param['javax.faces.source'] == 'form:submit'}" />
...
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />

这篇关于如何验证值瞬间,但检查只能在全提交所需?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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