【问题标题】:How to validate p:pickList to require at least one selection如何验证 p:pickList 至少需要一个选择
【发布时间】:2014-11-01 13:29:11
【问题描述】:

我已经使用列选项列表来自定义页面上表格的列,如果用户禁用选项列表中的所有列,我想显示对话框消息(应启用至少一列)。这意味着用户应该在启用框中至少保留一列。

<p:dialog id="pickListDialog" header="Customize View"
                        widgetVar="dlg">
                        <h:panelGrid>
                            <p:pickList id="columnPickList"
                                value="#{Class_Name.columns}" var="col"
                                itemLabel="#{Class_Name.columnPickList.columnsDisplayMap.get(col)}"
                                itemValue="#{col}">
                                <f:facet name="sourceCaption">Disabled</f:facet>
                                <f:facet name="targetCaption">Enabled</f:facet>
                            </p:pickList>

                            <p:commandButton value="Submit"
                                actionListener="#{Class_Name.columnPickList.saveColumns}" onclick="return disableClick();"
                                ajax="false" />
                        </h:panelGrid>
                    </p:dialog>

Javascript 方法 -

function disableClick(){   
        if (document.getElementById("form:columnPickList_target").value == ""){
    alert("Please Select Atleast One Field");       
    return false;      
        }
    }

我在这里使用了 Javascript。但我想通过 JSF 做到这一点。 谁能告诉我?

【问题讨论】:

  • 使用pickList的必需属性。将其设置为 required="true" 并定义 requiredMessage。

标签: validation jsf jsf-2 primefaces


【解决方案1】:

案例1:使用必需属性

<p:pickList id="columnPickList" required="true" requiredMessage="Atleast one column should be  enabled"
     value="#{Class_Name.columns}" var="col"
     itemLabel="#{Class_Name.columnPickList.columnsDisplayMap.get(col)}"
     itemValue="#{col}">
    <f:facet name="sourceCaption">Disabled</f:facet>
    <f:facet name="targetCaption">Enabled</f:facet>
 </p:pickList>

案例 2:从支持 bean 打开对话框

    public void saveColumns()
{
     validateColumns();

    //doSomthing
}

public void validateColumns() {
     //check if picklist target is empty
       if(columns.getTarget().size()==0)
     {
        //open dialog
       message="Atleast one column should be  enabled";
       RequestContext.getCurrentInstance().execute("validationDialog.show()");
     }
}

private String message; //getter and setter

对话:

<p:dialog id="valid"  widgetVar="validationDialog">
          <p:outputLabel value="#{yourbean.message}" />
</dialog>

【讨论】:

    猜你喜欢
    • 2011-07-21
    • 1970-01-01
    • 2011-03-13
    • 2015-11-06
    • 2017-09-09
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 2018-04-01
    相关资源
    最近更新 更多