0

I have a form with multiple fields, of which I need one of them required to continue or a group of them. For simplicity, I'll just paste a simpler version of my code here

    <h:form id="searchForm">
        <p:growl for="searchForm" showDetail="true" showSummary="true"/>
        <p:inputText id="name"
            value="#{backingBean.name}"
            required="#{backingBean.nameRequired and backingBean.masterRequired}">
            <p:ajax />
        </p:inputText>
        <h:panelGroup id="dateForm">
            <p:inputMask id="date"
                value="#{backingBean.date}"
                mask="99/99/9999"
                required="#{backingBean.dateRequired and backingBean.masterRequired}"
                converterMessage="Please insert using format dd/mm/yyyy">
                <f:convertDateTime pattern="dd/MM/yyyy" />
                <f:validator validatorId="dateValidator" />
                <p:ajax update="dateForm" />
            </p:inputMask>
        </h:panelGroup>
        <p:selectOneMenu id="documentType" effect="fade"
            value="#{backingBean.documentType}"
            required="#{backingBean.documentRequired and backingBean.masterRequired}">
            <f:selectItem itemLabel="Select document type" noSelectOption="true" />
            <f:selectItems
                value="#{constantsController.docType.entrySet()}"
                var="entry" itemValue="#{entry.key}" itemLabel="#{entry.value}" />
            <p:ajax />
        </p:selectOneMenu>

        <p:inputText id="documentNumber"
            value="#{backingBean.documentNumber}"
            required="#{backingBean.documentRequired and backingBean.masterRequired}">
            <p:ajax />
        </p:inputText>

        <p:commandButton id="searchButton"
            value="Search"
            action="#{controllerBean.submitSearch}"
            icon="ui-icon-search">
            <p:ajax
                listener="#{backingBean.checkRequired()}" update="@form" />
        </p:commandButton>

    </h:form>

When all fields are empty, the is shown as it should. Now what I need is the inputMask date to only show the converter message and the validation message. I need it to not show the message when the requiredMessage will try to be shown.

I tried putting a <p:message for="date" rendered="#{!empty param['date']}" /> but it does not work, since it is not rendered on the first load, and ajax does not update the panelGroup, I think it is because since I gave an invalid value to the converter, it does not fire the update.

Additional information:

As you can see, I have multiple required fields, I need at least one of these:

-name

-date

-document type + document number

Now, to validate the required fields, I'm doing the following on my backing bean:

public void checkRequired() {

    nameRequired = name.equals("");
    dateRequired = date == null;
    documentRequired = documentType.equals("") || documentNumber.equals("");

    masterRequired = nameRequired && dateRequired && documentRequired;
    if(masterRequired)
    {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Incomplete form",
                "Please insert at least 1 field");
        FacesContext.getCurrentInstance().addMessage("searchForm", message);
        return;
    }
}
  • I've noticed that you have put an `ajax` tag inside the command button, doesn't it have impact on the execution of the command button, it's like having 2 `ajax`!!, the `ajax` of the command button and the `ajax` in-between the command button tags, maybe it's just the ajax of the command button that is executed. Try to use one `ajax`, put the code of the `ajax` in-between the command button tags in the built-in `ajax` of the command button and omit the `ajax` in-between the command button tags. – ziMtyth Sep 12 '17 at 10:44
  • I have 2 ajax because I need it to first check if the fields are filled so that I can set the boolean I defined in the backing bean as false, so that when it goes to action, the form can let it call the method, or else, it wouldn't even call the "action" because there were required fields. It was the solution I came up to solve this "One of many required fields". I posted it here (https://stackoverflow.com/questions/45845500/at-least-one-group-of-multiple-fields-required-in-jsf) but there weren't any solutions. – Nuno Vasconcelos Sep 12 '17 at 13:12

0 Answers0