基于选择单选按钮的Jsf验证

时间:2013-05-13 13:41:52

标签: ajax jsf

       <h:panelGrid id="userActivationGrid" columns="1">
            <p:selectOneRadio value="#{user.activationMode}" layout="pageDirection" immediate="true" >
                <f:selectItem itemLabel="${adminmsgs['create.avtivateMode.button.label']}" itemValue="${adminmsgs['create.avtivateMode.button.itemvalue.label']}"  />
                <f:selectItem itemLabel="${adminmsgs['create.avtivateMode1.button.label']}" itemValue="${adminmsgs['create.avtivateMode1.button.itemvalue.label']}"  />
                <f:ajax event="change" render="userActivationGrid" /> 
                <f:ajax event="click" listener="#{user.tempPassword()}"/>
            </p:selectOneRadio>
            <p:spacer width="10"/>
            <h:panelGrid id="manualActivationGrid" rendered="#{user.manualActivation}" columns="3">
                <h:outputLabel>#{adminmsgs['create.manualActivation.text.label']}</h:outputLabel>
                <p:inputText id="userName" immediate="true" required="true" value="#{user.userName}" requiredMessage="UserName Should Be Entered" validatorMessage="Username Should Be alphanumeric"/>
                <h:message id="userNameMsg" for="userName" />
                <h:outputLabel>#{adminmsgs['create.manualActivation1.text.label']}</h:outputLabel>
                <p:password id="password" immediate="true" required="true" validatorMessage="password should be minimum 5 character" requiredMessage="Password is Mandatory"/>
                <h:message id="passwordMsg" for="password" />
            </h:panelGrid>
        </h:panelGrid>

我正在使用两个单选按钮,用户名和密码应该验证我是否选择了无线电按钮,否则它不会被贬低。请事先帮助我

1 个答案:

答案 0 :(得分:1)

我建议将f:validateRegex代码添加到userNamepassword输入,如下所示:

<p:inputText id="userName" immediate="true" required="true" value="#{user.userName}" requiredMessage="UserName Should Be Entered" validatorMessage="Username Should Be alphanumeric">        
  <f:validateRegex pattern="[_A-Za-z0-9]+" disabled="#{user.validationDisabled}" />
</p:inputText>

validationDisabled属性正在使用单选按钮值来确定是否有必要进行验证。有关Primefaces + JSF验证器的更多示例,请参阅here