Sabarish
Sabarish

Reputation: 912

How to fetch validation error messages using FacesContext?

My xhtml contains input fields, where user can input numeric or alphabet values. Using an ajax call I am validating the input value and throwing a validation error on the . But even after entering invalid characters and error message, user may still hit the 'Save' button. Once the user hits the Save button it triggers a method in the Controller. Is there a way I can check for validation errors on p:messages inside the controller using the FacesContext.getCurrentInstance().

xhtml code

    <!-- Messages -->       

    <p:messages id="errorMessages" globalOnly="false" showDetail="true" showSummary="false" closable="true" />  
<!-- Column input field-->
    <p:column>
        <f:facet name="header">
            <h:outputText value="Input Amount" escape="false" />
        </f:facet>
        <p:cellEditor>

            <f:facet name="output">
                <h:outputText value="#{row.amount}" escape="false">
                    <f:convertNumber maxFractionDigits="3" minFractionDigits="3"
                        maxIntegerDigits="5" />
                </h:outputText>
            </f:facet>
            <f:facet name="input">
                <p:inputText id="input" value="#{row.amount}" maxlength="10"
                    size="10">
                    <f:convertNumber maxFractionDigits="3" minFractionDigits="3"
                        maxIntegerDigits="5" />
                    <f:validator validatorId="hourlyValueValidator" for="input" />
                    <p:ajax event="change" partialSubmit="true" process="input"
                        update=":#{p:component('errorMessages')}" />
                </p:inputText>
            </f:facet>
        </p:cellEditor>
    </p:column>


<!-- Save Button -->

    <p:commandButton id="btnSubmit" value="Save"        
    update=":#{p:component('tblScrollPnl')} :#{p:component('errorMessages')}"
    action="#{controller.saveValues()}" ajax="true" />

Controller in ViewScope

   public void saveValues() throws Exception {

  FacesContext context = FacesContext.getCurrentInstance();
  List<FacesMessage> errorMsgList = context.getMessageList("globalMessages");
  ......
  ......
  ......
  }

Upvotes: 0

Views: 1436

Answers (1)

Alexandre Lavoie
Alexandre Lavoie

Reputation: 8771

You don't need to get messages from the bean side for validation, as you experienced, view side validation occur but doesn't block your action.

There is couple of thing wrongs in your view, to fix your problem you need to change the process attribute of your p:commandButton, ortherwise only the button is processed (by default process="@this", so the validation is skipped.

<p:commandButton id="btnSubmit" value="Save" process="@form" update=":tblScrollPnl :errorMessages" action="#{controller.saveValues()}" />

Also note that I've removed ajax="true" since it is by default and fixed major problems in update="".

You should also rearrange your validators :

<p:inputText id="input" value="#{row.amount}" maxlength="10" size="10" validator="hourlyValueValidator">
    <f:convertNumber maxFractionDigits="3" minFractionDigits="3" maxIntegerDigits="5" />
</p:inputText>

Upvotes: 1

Related Questions