JSF清除其他字段

时间:2017-11-23 21:50:22

标签: jsf bean-validation

我们有两个普通的输入字段(h:inputText),每个字段在JSF 2.3页面中使用bean验证。 如果第一个输入字段未通过验证,则应清除输入第二个输入字段的值。

JSF:

<h:body>

  <h:form>

    <!-- If either field fails then the other should be cleared -->

    <h:outputLabel value="Enter text that must be lowercase"/>
    <h:inputText value="#{myBean.mandatoryLowercase}" required="true" />

    <h:outputLabel value="Enter a decimal"/>
    <h:inputText value="#{myBean.mandatoryDecimal}" required="true"/>

    <h:commandButton action="#{myController.doSomething}" value="Submit"/>

  </h:form>
</h:body>

MyBean.java

@SessionScoped
@Named
public class MyBean implements Serializable{

@NotNull
@Pattern(regexp="(?=.*[a-z]).+")
private String mandatoryField;

@Digits(integer=6, fraction=2, message = "Must be a decimal")
private BigDecimal someOtherField;

public String getMandatoryLowercase() {
    return mandatoryField;
}

public void setMandatoryLowercase(String mandatoryLowercase) {
    this.mandatoryField = mandatoryLowercase;
}

public BigDecimal getMandatoryDecimal() {
    return someOtherField;
}

public void setMandatoryDecimal(BigDecimal mandatoryDecimal) {
    this.someOtherField = mandatoryDecimal;
}

}

如果mandatoryDecimal无效,清除mandatoryLowercase的正确方法是什么?

一种解决方案可能是使用自定义验证器,它可以识别所有组件,然后可能以这种方式清除值,但这似乎在语义上将字段连接在一起,而实际上它们应该是分开的。

理想情况下,具有RENDER_RESPONSE阶段的<f:viewAction>将是理想的,因为我认为它在逻辑上是有道理的。但是,viewActions无法在该阶段执行。

感谢您的帮助。 编

0 个答案:

没有答案