【发布时间】:2018-12-19 18:54:15
【问题描述】:
起初我只尝试了“必需”属性,但由于它似乎从未被检查过,所以我添加了一个非常简单的验证器来检查“颜色”属性的长度......但似乎没有任何效果。在同一个表单中,我还有其他 inputText 所需的组件,当我按下 commandButton 时,这些组件会被正确检查。
我有以下代码:
<p:colorPicker id="color" value="#{backBean.color}" required="true" requiredMessage="Required!" validator="ColorValidator" validatorMessage="Required!"/>
<p:commandButton id="createOrUpdateButton"
actionListener="{backBean.createOrUpdate}"
process="@form"
update="@all"
value="Save"
style="width: 95%;" />
验证器:
@FacesValidator("ColorValidator")
public class ColorValidator implements Validator{
public ColorValidator(){
}
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
if (value==null || value.toString().trim().isEmpty()) {
FacesMessage msg = new FacesMessage("Color validation failed.","Please select a color.");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}
}
【问题讨论】:
-
你添加了
<p:messages for="color"/>吗? -
@Selaron 尝试过,但没有运气。
-
那么我担心我们需要一个非常minimal reproducible example
标签: jsf primefaces