如何验证p:pickList至少需要一个选择

时间:2014-09-08 10:40:11

标签: validation jsf jsf-2 primefaces

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

<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做到这一点。 谁能告诉我?

1 个答案:

答案 0 :(得分:2)

案例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>