问题
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?
回答1:
Use <f:validateBean> where on you set the disabled
attribute.
<h:inputText value="#{bean.input}">
<f:validateBean disabled="#{bean.draft}" />
</h:inputText>
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>
with
@ManagedProperty("#{param.draft}")
private boolean draft;
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"));
}
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>
来源:https://stackoverflow.com/questions/7436016/submit-form-without-bean-validation