Search code examples
validationjsfprimefacesjsf-2

How to invoke calendar validator on the default value at loading time


I have this piece of code. At the moment the validation is invoked by a button. But I do also want to invoke the validation if there is a default value already present there when the page loads. How can I do that? Thanks.

// ... the calendar
<div class="formBlock">
    <p class="formGroupTitle">#{msg['startEmploymentDate']}</p>
    <p:message id="startDateMessage" for="startDate" />
    <p:calendar id="startDate" required="true" pattern="dd.MM.yyyy"
            locale="de" requiredMessage="#{nts['pleaseEnterAFutureDate']}"
            mindate="#{companyJobProfileCreateStep2Bean.currentDate}"
            value="#{companyJobProfileCreateStep2Bean.jobProfile.startDate}"
            converterMessage="#{nts['pleaseEnterAFutureDate']}"
            validator="futureDateValidator" showOn="button" navigator="true">
            <p:ajax event="dateSelect" update="@(.roundedNumberRefreshable)"
                    listener="#{companyJobProfileCreateStep2Bean.updateSearchParameters()}" />
    </p:calendar>
</div>

// ... and the button
<p:commandButton
    value="#{companyJobProfileCreateStep2Bean.getJobProfile().ams ? msg['sendProposal'] : msg['accept']}"
    oncomplete="if (args &amp;&amp; !args.validationFailed) {PF('generalDetailsSlider').unselect(0);PF('paymentDetailsSlider').select(0);}"
    update="@(.adtextUpdateable) @(.hasAddressAndMessageUpdateable) startDate vacancies startDateMessage vacanciesMessage @(.generalDetailsSliderHeaderUpdateable) @(.progressBarCompany)"
    actionListener="#{companyJobProfileCreateStep2Bean.verifyHasAddresses()}"
    process="startDate vacancies" partialSubmit="true"></p:commandButton>

Solution

  • I've got an idea which might work by using a remoteCommand (A little hack). Basically you will simulate what will happen when the submit button is clicked. You can decide when you want to call your function.

    Hope this helps.

    <p:remoteCommand name="errorSubmitButton" autoRun="false/true" global="false"
                    oncomplete="if (args &amp;&amp; !args.validationFailed) {PF('generalDetailsSlider').unselect(0);PF('paymentDetailsSlider').select(0);}"
                    update="@(.adtextUpdateable) @(.hasAddressAndMessageUpdateable) startDate vacancies startDateMessage vacanciesMessage @(.generalDetailsSliderHeaderUpdateable) @(.progressBarCompany)"
                    actionListener="#{companyJobProfileCreateStep2Bean.verifyHasAddresses()}"
                    process="startDate vacancies" partialSubmit="true" />
    
    <h:outputScript rendered="yourConditionWhenToExecuteScriptIfYouHaveOne">
            $(document).ready(function () {
                errorSubmitButton();
            });
    </h:outputScript>