提交表单而无需验证bean [英] Submit form without bean validation

查看:100
本文介绍了提交表单而无需验证bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,该表单具有带有一些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?

推荐答案

使用

如果此结果评估为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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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