简体   繁体   English

验证失败后重置selectOneMenu表单值

[英]Reset selectOneMenu form value after validation failure

We are attempting to use a selectOneMenu as a "status" control for a business entity. 我们正在尝试将selectOneMenu用作业务实体的“状态”控件。 The dropdown has a list of statuses. 下拉列表具有状态列表。 When the user selects a status, we run a set of complex business rules that determine whether the selected status is ok to set on the business entity. 当用户选择状态时,我们将运行一组复杂的业务规则,这些规则确定所选状态是否可以在业务实体上设置。 If it's not, we write out a message why not, and disallow the status from being set. 如果不是,我们会写一条信息为什么不这样做,并禁止设置状态。 This all works well within the JSF lifecycle, except that I would like to reset the dropdown to the previous status (aka the current status) before the validation failed. 这一切在JSF生命周期中都可以正常运行,除了我想在验证失败之前将下拉菜单重置为以前的状态(又称当前状态)。 So if I switch from status A, to status B, but fail, I want to reset the form control to status B. 因此,如果我从状态A切换到状态B,但失败了,我想将表单控件重置为状态B。

facelet code 小面代码

<p:selectOneMenu id="statusMenu"
    binding="#{mybean.statusMenu}"
    converter="myConverter"
    value="#{mybean.statusValue}">

    <f:event type="preValidate" listener="#{mybean.validateStatus}"/>
    <p:ajax update="statusMenu :someForm :someOtherForm" />

    <f:selectItems value="#{mybean.statusList}"
        var="status"
        itemLabel="#{status.statusText}"
        itemValue="#{status}">
    </f:selectItems>
</p:selectOneMenu>

Backing bean validation menu 支持bean验证菜单

public void validateStatus (ComponentSystemEvent e)
{
    String submittedStatus = ((MyStatus)getStatusMenu().getValue()).getStatusText();

    if(submittedStatus.equals("someStatus"))
    {       
        //do validation
        boolean valid = doBusinessStatusValidationLogicStuff()

        if(!valid)
        {
            //a combination of junk that attempts to reset the form value, or do *something* to it
            getStatusMenu().resetValue();
            getStatusMenu().setSubmittedValue(null);
            getStatusMenu().setValue(null);
            getStatusMenu().setLocalValueSet(false);

            FacesMessage errorMessage = new FacesMessage(severity, summary, detail);
            context.addMessage(null, "you fail");
            context.isValidationFailed();
            context.renderResponse();
        }
    }
}

I can see in the debugger that the bound component's value is set to what I want, however it simply does not update the form control. 我可以在调试器中看到绑定组件的值设置为所需的值,但是它根本不会更新表单控件。 What am I missing? 我想念什么?

ended up being an equals/hashcode issue. 最终成为一个equals / hashcode问题。 So always be sure to check there! 因此,请务必确保在那里检查!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM