我有一个表单,该表单具有带有一些JSR-303验证bean的域模型.现在,我想包括未经任何验证的保存草稿"功能.如果我在相应的commandButton上设置immediate=true,则将跳过验证,但也会提交表单.
I've got a form which has a domain model with some JSR-303 validation beans. Now I would like to include a "Save draft" feature without any validation. If I set immediate=true on my corresponding commandButton validation is skipped but also the form submit.
是否可以在保存草稿操作中更新模型?
Is there a way to update the model in my save draft action?
推荐答案使用 <f:validateBean> ,在其中设置disabled属性.
<h:inputText value="#{bean.input}"> <f:validateBean disabled="#{bean.draft}" /> </h:inputText>如果此结果评估为true,则将跳过对与输入值关联的属性的所有bean验证.您仅应确保在验证阶段之前设置boolean draft属性.例如
If this evaluates true, this will skip all bean validation on the property associated with the input's value. You should only ensure that the boolean draft property is set before the validations phase takes place. E.g.
<h:commandButton value="Save draft" action="#{bean.saveDraft}"> <f:param name="draft" value="true" /> </h:commandButton>使用
@ManagedProperty("#{param.draft}") private boolean draft;或者如果它是视图范围的Bean,而@ManagedProperty在该范围内将不起作用:
or if it's a view scoped bean on which @ManagedProperty won't work:
public boolean isDraft() { return "true".equals(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("draft")); }另一种方法是通过确定按钮的参数名称是否存在来检查EL是否按下了按钮.例如,使用以下表单和按钮ID
Another way is to check in EL if the button is pressed by determining the presence of its parameter name. For example, with the following form and button ID
<h:form id="form"> <h:inputText value="#{bean.input}"> <f:validateBean disabled="#{not empty param['form:draft']}" /> </h:inputText> <h:commandButton id="draft" value="Save draft" action="#{bean.saveDraft}" /> </h:form>更多推荐
提交表单而无需验证bean
发布评论