JSF代码。<ice:inputText id="txtemailFrom" required="true"
requiredMessage="please enter proper value in From Address field" value="#{emailBean1.sender}" maxlength="100" size="40">
<f:validator validatorId="emailValidator" />
</ice:inputText>
<ice:selectOneRadio id="emailType"
value="#{emailBean1.currentEmailType}"
valueChangeListener="#{emailBean1.onEmailTypeChange}"
partialSubmit="true"
style="margin-left:95px">
<f:selectItem itemLabel="Do Not Reply" />
<f:selectItem itemLabel="User's Email" />
<f:selectItem itemLabel="Custom Email" />
</ice:selectOneRadio>
验证器的java代码:
public void onEmailTypeChange(ValueChangeEvent event) {
logger.debug(new LogEntry(" EmailBean.onEmailTypeChange() execution is started."));
String emailType = event.getNewValue().toString();
FacesContext context = FacesContext.getCurrentInstance();
this.setUserEmailId(roleHandler.getUserId() + "@xxx.com");
HtmlInputText txtComponent = (HtmlInputText) Utils.findComponent(
context.getViewRoot(), "txtemailFrom");
if (emailType.equals("Do Not Reply")) {
this.setEmailFrom("donotreply@fedex.com");
txtComponent.setValue("donotreply@fedex.com");
} else if (emailType.equals("User's Email")) {
this.setEmailFrom(this.getUserEmailId());
txtComponent.setValue(this.getUserEmailId());
} else if(emailType.equals("Custom Email")){
this.setEmailFrom("");
txtComponent.setValue("");
}
logger.debug(new LogEntry(
" EmailBean.onEmailTypeChange() execution is ended."));
}
当我更改单选按钮值时,它正在更新fromEmailaddress的inputTextField中的值。我使用这段代码通过java更新值。
HtmlInputText txtComponent = (HtmlInputText) Utils.findComponent(
context.getViewRoot(), "txtemailFrom");
但我的问题是,当我更改单选按钮时,我正在为inputtext字段使用验证器,如果电子邮件无效,则它将失败并显示错误消息。。它按照jsf方法工作,但根据我的要求,它应该更改电子邮件inputTextFied中的值。。我尝试了immediate="true",然后跳过验证阶段,但没有将值更新到Emailtext字段中。
尝试在复选框中使用Immediate='true'。若您设置immediate='true',它将不会验证字段,而是将值提交到服务器。
您可以尝试使用自定义验证器。然后,您甚至可以在验证发生之前更新模型。这里是我用于相同目的的代码片段:
validate(FacesContext ctx, UIComponent comp, Object val) {
ValueExpression ve = ((UIInput) comp).getValueExpression("value");
ve.setValue(ctx.getELContext(), val);
// your validation logic...
}
希望它能有所帮助!