2015-06-22 121 views
2

我正在學習CQ5,並且我被困在創建對話框驗證。我的JavaScript位於clientlibs文件夾中,我正在使用偵聽器來調用該函數,但它沒有被調用。我看不到驗證。CQ5對話框不工作

我想提供相應的ID所需的URL字段。

以下是我的JS代碼。

var fieldsetCheck = {}; 

fieldsetCheck.chkBlankFields = function chkBlankFields(panel) { 

    var fieldSets = panel.findByType('dialogfieldset'); 
    var fLength = fieldSets.length; 

    for (var i = 0; i < fLength; i++) { 
     var fieldSet = fieldSets[i]; 
     var Id = panel.getComponent('Id'+(i++)); 
     var Url = panel.getComponent('Url'+(i++)); 

     if(Id.getValue().trim() !== ""||Url.getValue().trim()!=="") { 
      campId.allowBlank = false; 
      campUrl.allowBlank = false; 
     } else { 
      campId.allowBlank = true; 
      campUrl.allowBlank = true; 
     } 
    }  
} 

,這是我dialog.xml

<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0"zzzmlns:jcr="http://www.jcp.org/jcr/1.0" jcr:primaryType="cq:Dialog" height="500" id="textNavEditDialog" title="Test Configuration Dialog" width="600" xtype="dialog"> 
    <items jcr:primaryType="cq:WidgetCollection"> 
     <tabs jcr:primaryType="cq:TabPanel" activeTab="{Long}0" title="Test Configuration" xtype="tabpanel"> 
      <items jcr:primaryType="cq:WidgetCollection"> 
       <campaigns jcr:primaryType="cq:Widget" title="Links" xtype="panel"> 
        <items jcr:primaryType="cq:WidgetCollection"> 
          <fieldsetDialog1 jcr:primaryType="cq:Widget" xtype="dialogfieldset" border="{Boolean}true" collapseFirst="{Boolean}true" collapsed="{Boolean}false" collapsible="{Boolean}false" hideLabel="{Boolean}false" id="fieldset1"> 
           <items jcr:primaryType="cq:WidgetCollection"> 

            <Id1 jcr:primaryType="cq:Widget" fieldDescription="Enter segment only (exclude seg=)" fieldLabel="Segment Number 1" allowBlank="{Boolean}false" maxLength="12" name="./sc:Id1" nanText="Please enter a numeric value" xtype="numberfield" ItemId ="Id1"/> 
            <Url1 jcr:primaryType="cq:Widget" fieldDescription="Path to Campaign 1" fieldLabel="Campaign URL 1" allowBlank="{Boolean}false" name="./sc:Url1" predicate="hierarchy" suffix="/_jcr_content/par.html" typeAhead="{Boolean}false" xtype="pathfield" ItemId ="Url1"/> 
           </items> 
          </fieldsetDialog1> 

          <fieldsetDialog2 jcr:primaryType="cq:Widget" xtype="dialogfieldset" border="{Boolean}true" collapseFirst="{Boolean}true" collapsed="{Boolean}false" collapsible="{Boolean}false" hideLabel="{Boolean}false" id="fieldset2"> 
           <items jcr:primaryType="cq:WidgetCollection"> 

            <Id2 jcr:primaryType="cq:Widget" fieldDescription="Enter segment only (exclude seg=)" fieldLabel="Segment Number 2" maxLength="12" name="./sc:Id2" nanText="Please enter a numeric value" xtype="numberfield" ItemId ="Id2"/> 
            <Url2 jcr:primaryType="cq:Widget" fieldDescription="Path to Campaign 2" fieldLabel="Campaign URL 2" name="./sc:Url2" predicate="hierarchy" suffix="/_jcr_content/par.html" typeAhead="{Boolean}false" xtype="pathfield" ItemId = "Url2" /> 
           </items> 

          </fieldsetDialog2> 

          <fieldsetDialog3 jcr:primaryType="cq:Widget" xtype="dialogfieldset" border="{Boolean}true" collapseFirst="{Boolean}true" collapsed="{Boolean}false" collapsible="{Boolean}false" hideLabel="{Boolean}false" id="fieldset3"> 
           <items jcr:primaryType="cq:WidgetCollection"> 

            <Id3 jcr:primaryType="cq:Widget" fieldDescription="Enter segment only (exclude seg=)" fieldLabel="Segment Number 3" maxLength="12" name="./sc:Id3" nanText="Please enter a numeric value" xtype="numberfield" ItemId ="Id3" /> 
            <Url3 jcr:primaryType="cq:Widget" fieldDescription="Path to Campaign 3" fieldLabel="Campaign URL 3" name="./sc:Url3" predicate="hierarchy" suffix="/_jcr_content/par.html" typeAhead="{Boolean}false" xtype="pathfield" ItemId ="Url3"/> 
           </items> 
          </fieldsetDialog3> 
        </items> 
        <listeners jcr:primaryType="nt:unstructured" beforesubmit="function(this) { fieldsetCheck.chkBlankFields(this);}" /> 
       </campaigns> 
      </items> 
     </tabs> 
    </items> 
</jcr:root> 
+1

JavaScript不是Java,所以我已經從你的帖子中刪除了[tag:java]標籤。 – hexafraction

+0

謝謝!認爲CQ5具有Java框架及其相關 – maddy

回答

0

Panel的xtype沒有beforesubmit事件。該事件僅適用於dialog xtype。監聽器必須是對話節點的子節點。

該函數將作爲參數調用,而不是面板。由於在函數中使用findByType()方法,它仍然可以工作,只需將變量重命名爲對話框即可。

+0

對不起,沒有得到這部分「只是重命名變量對話框的可讀性應該做的。」 – maddy

+0

@maddy因爲變量實際上是引用對話框而不是面板,所以最好稱它爲對話框。如果他們試圖維持代碼 –

+0

可能會混淆別人,我根據您的意見嘗試,它顯示我l.fireFn.apply不是一個函數錯誤。請指導 – maddy

0

我看到,在增加id和url方面存在問題,因爲在每次迭代中都有2個額外增量的語句中都有i ++,所以在第二次迭代中值將是3而不是1,依此類推。所以,我建議下面的代碼

for (var i = 0; i < fLength; i++) 
    { 
     var fieldSet = fieldSets[i]; 
     var Id = panel.getComponent('Id'+(i + 1)); 
     var Url = panel.getComponent('Url'+(i + 1)); 

     if(Id.getValue().trim() !== ""||Url.getValue().trim()!=="") 
     { 
      campId.allowBlank = false; 
      campUrl.allowBlank = false; 
     } 
     else 
     { 
      campId.allowBlank = true; 
      campUrl.allowBlank = true; 
     } 
    }