JSF : Better way to check for existence of

后端 未结 3 1323
眼角桃花
眼角桃花 2021-02-02 11:26

I have a form in which validation error message needs to be displayed below the input elements. The error needs to be highlighted by showing an error bubble around the error mes

相关标签:
3条回答
  • 2021-02-02 11:56

    I use bootstrap and also needed this... solved it by creating a Custom component wrapper.

    <composite:interface>
        <composite:attribute name="casoCustomFieldValue" required="true" />
        <composite:attribute name="casoOpen" default="true" />
    </composite:interface>
    
    <composite:implementation>
    
        <h:panelGroup styleClass="#{not empty facesContext.getMessageList(''.concat(cc.clientId).concat(':valortext')) ? 'form-group has-error' : 'form-group'}"
                      rendered="#{cc.attrs.casoCustomFieldValue.customField.fieldTypeId.fieldTypeId eq 'TEXT'}" >
            <p:inputText id="valortext" value="#{cc.attrs.casoCustomFieldValue.valor}" styleClass="form-control input-sm"
                     required="#{cc.attrs.casoCustomFieldValue.customField.required}" requiredMessage="Se necesita un valor." disabled="#{not cc.attrs.casoOpen}"/>
            <h:panelGroup rendered="#{not empty facesContext.getMessageList(''.concat(cc.clientId).concat(':valortext'))}" styleClass="help-block">
                <h:message for="valortext" />
            </h:panelGroup>
        </h:panelGroup>
    
    
    
    </composite:implementation>
    
    0 讨论(0)
  • 2021-02-02 12:10
    Iterator<FacesMessage> pkit = fctxt.getMessages(); // check all existing messages from the FacesContext         
    while(pkit.hasNext()){ 
        System.out.println(pkit.next().getDetail()); // get message text
        System.out.println(pkit.next().getSummary());
    }
    

    If the message you are looking for is found in the while loop, you can take appropriate action

    0 讨论(0)
  • 2021-02-02 12:14

    The error needs to be highlighted by showing an error bubble around the error message ...

    Just use <h:message> with a styleclass wherein you define the desired style.

    <h:inputText id="foo" />
    <h:message for="foo" styleClass="error" />
    

    with

    .error {
        border: 1px solid red;
        background: pink;
    }
    

    ...and the input text.

    Just check if UIInput#isValid() is true. Since JSF 2.0 the current component is available by implicit EL variable #{component}.

    <h:inputText styleClass="#{!component.valid ? 'error' : 'none'}" />
    

    As to the actual question about checking if there's a message for a certain client ID, then you may find this answer interesting. But I don't think that this is applicable in your particular case.


    Update: as per the comments, you seem to want to style the containing component instead of the invididual components. In that case, do as follows:

    <h:panelGroup styleClass="#{!foo.valid ? 'error' : 'none'}">
        <h:inputText id="foo" binding="#{foo}" />
        <h:message for="foo" />
    </h:panelGroup>
    
    0 讨论(0)
提交回复
热议问题