【问题标题】:Skipping validation phase but need to update model values跳过验证阶段但需要更新模型值
【发布时间】:2014-06-09 14:49:44
【问题描述】:

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" 然后跳过验证阶段,但没有将值更新到 Emailtextfield 中。

【问题讨论】:

  • 如果我的解释不充分,请评论
  • 不要分享客户详细信息 "@fedex.com" ;)
  • 是 onEmailTypeChange 你提到的验证器吗?
  • 不,这是我在代码中提到的 valueChangListener
  • 你能试着在

标签: java validation jsf icefaces


【解决方案1】:

尝试在复选框中使用 Immediate='true'。 如果您输入 immediate='true' 它将不会验证该字段并将值提交到服务器。

【讨论】:

    【解决方案2】:

    您可以尝试使用自定义验证器。然后,您甚至可以在验证发生之前更新模型。这是我用于相同目的的代码片段:

    validate(FacesContext ctx, UIComponent comp, Object val) {
        ValueExpression ve = ((UIInput) comp).getValueExpression("value");
        ve.setValue(ctx.getELContext(), val);
        // your validation logic...
    }
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-03
      • 2020-05-29
      • 2018-12-01
      相关资源
      最近更新 更多