Prevent p:messages component from showing messages which are already shown in p:message

ⅰ亾dé卋堺 提交于 2020-01-23 10:43:09

问题


i have an input component that has three types of validation (required,validatorMessage,converterMessage) and this input has its own message icon, and the whole form has a messages component to display all the messages for all components as follows:

<p:message for="idEstNumOfUser" display="icon" id="msgEstNumOfUser" />
                    <p:inputText id="idEstNumOfUser"
                            placeholder="Estimated Number of Users"
                            value="#{mybean.estimatedUserCount}" required="true" requiredMessage=""
                            maxlength="8" title="Estimated Number of Users"
                            validatorMessage="Please enter digits only for 'Estimated Number of Users'"
                            converterMessage="Please enter digits only for 'Estimated Number of Users'">
                            <f:convertNumber />
                    <p:ajax event="blur" update=":betasignup:msgEstNumOfUser" />
                    </p:inputText>

                    <p:messages id="messages"  autoUpdate="true"/>

and i have a style before the error message that shows a warning icon as follows:

.ui-messages-error-summary:before{
                       font-family: FontAwesome;
                       speak: none;
                       font-style: normal;
                       font-weight: normal;
                       font-variant: normal;
                       text-transform: none;
                       line-height: 1;
                       -webkit-font-smoothing: antialiased;
                       content: "\f05a";
                       margin-right: 6px;
                    }

WHAT IS HAPPENING: the generated html for messages component in the case required validation occurs is:

              <div class="ui-messages-error ui-corner-all">
              <span class="ui-messages-error-icon">
              </span>
              <ul>
               <li>
                 <span class="ui-messages-error-summary">
                 </span>
              </li>
             </ul>
            </div>  

so the style for .ui-messages-error-summary:before is applied, and i don't want it to be applied when required validation occurs.

if there are any suggestions to make validation better in this case, please advise.


回答1:


Set redisplay attribute to false.

<p:messages ... redisplay="false" />

This way it won't redisplay messages which are already displayed before. One -obvious- requirement is that the <p:messages> component itself is placed after all those <h|p:message> components. If you actually need to position it visually before the <h|p:message> components, make use of CSS and perhaps JS to reposition it.




回答2:


i used the following JS function oncomplete of blur event and it works fine:

function hideRequiredMessage(){
            var ErrorMessagesSpans= document.getElementById("myform:messages").getElementsByClassName("ui-messages-error-summary");
            if(ErrorMessagesSpans!=null && ErrorMessagesSpans.length > 0){
            var ErrorMessageSpan=ErrorMessagesSpans[0];
               if(ErrorMessageSpan.innerHTML==''){
                  //ErrorMessageSpan.className = '';
                   ErrorMessageSpan.parentNode.removeChild(ErrorMessageSpan);
              }
            }
        }


来源:https://stackoverflow.com/questions/17317673/prevent-pmessages-component-from-showing-messages-which-are-already-shown-in-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!