问题描述
我有多种形式,其中有必填字段和可选字段.
I have multiple forms, where I have mandatory fields and optional fields.
要提交这样的表格,我需要对必须执行的required-attribute进行验证,效果很好.要取消这样的表单,我使用了p:commandbutton
上的属性immediate="true"
,这使得它的操作在 Apply Request Values -Phase阶段发生,如下所示:如何在单击特定按钮时跳过验证?
To submit such a form I require the validation on the required-attribute to be executed, which works fine.To cancel such a form I use the attribute immediate="true"
on the p:commandbutton
, which makes its action happen during the Apply Request Values-Phase as addressed here: How to skip validation when a specific button is clicked?
但是,对于大型表格,我想为用户提供一个保存"按钮,以便他以后可以继续.
However, for large forms I want to provide the user with a Save-Button, so he can proceed later.
仅保存当前状态,我还想忽略对必填属性的验证.但是,使用immediate="true"
不起作用,因为那样,我的save方法simple不会保存任何内容,因为JSF生命周期从未达到"UpdateModelValues"阶段. (根据 http://www.javacodegeeks.com/2012/01/jsf-and-immediate-attribute-command.html )
For just saving the current state I also want to ignore the validation of the required-attribute. However, using immediate="true"
is not working, because then my save method simple saves nothing, because the JSF lifecycle never hits the "UpdateModelValues"-Phase. (Acording to http://www.javacodegeeks.com/2012/01/jsf-and-immediate-attribute-command.html )
那么,如何绕过必需检查而不跳过生命周期的一半?
So, how to bypass the required-check but not skip half the lifecycle?
推荐答案
每个按钮只要在表单列表中,就在Param-List中创建一个条目.因此,我简单地对"required"参数应用了该条目的检查:
Each Button creates an entry inside the Param-List as long as it's member of the form.So I simple applied a check for the presence of that entry to the "required" parameter:
<h:form id="form" prependId="true">
...
<p:inputText id="someId"
required="#{param['form:save']==null}" ... />
...
<p:commandButton id="save" value="Save" />
<p:commandButton id="submit" value="Submit" />
<p:commandButton id="cancel" value="Cancel" immediate="true" />
</h:form>
当我单击提交"时,param['form:save']
是NULL
,然后将表达式转换为true
,以便执行验证.
When I click "Submit" the param['form:save']
is NULL
, which then turns the expression to true
so the validation is executed.
当我单击保存"时,param['form:save']
是NOT NULL
(但为空!),它解析为false
,因此验证被忽略. (或者说,JSF认为这不是必填字段,因为表达式beeing被评估为false
)
When I click "Save" the param['form:save']
is NOT NULL
(but empty!), which resolves to false
so the validation is ignored. (Or let's say JSF thinks it is not a required field due to the expression beeing evaluated to false
)
这篇关于JSF跳过Required-Validation而不使用即时= true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!