2017-01-27 63 views
2

我有這段代碼。此時驗證由一個按鈕調用。但是,如果在頁面加載時已經存在默認值,我也想調用驗證。我怎樣才能做到這一點?謝謝。如何在加載時調用默認值的日曆驗證器

// ... 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> 
+0

爲什麼不直接在你的支持bean的init方法中驗證它呢? – pablo

+0

@pablo:假設你想以這種方式驗證所有字段,那麼你正在複製功能。 – Kukeltje

+0

那麼,你已經把代碼隔離在一個特定的驗證器中,你可以重用,對嗎? – pablo

回答

0

我有一個想法,可以通過使用remoteCommand(一點點黑客)工作。基本上你會模擬點擊提交按鈕時會發生什麼。您可以決定何時調用您的功能。

希望這會有所幫助。

<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> 
+0

謝謝。有效。我做了一些微小的修改,但它完成了這項工作。 –