我正在学习CQ5,但我仍然坚持创建对话框验证。我的JavaScript位于clientlibs文件夹中,我使用一个监听器来调用该函数,但它没有被调用。我看不到验证。
如果提供了相应的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>
答案 0 :(得分:0)
Panel xtype没有提前事件。该事件仅适用于dialog xtype。侦听器必须是对话节点的子节点。
将使用对话框作为参数而不是面板调用该函数。由于您在函数中使用了findByType()方法,它仍然可以工作,只需将变量重命名为对话框以便于阅读。
答案 1 :(得分:0)
我看到,增加id和url存在问题,因为在每个迭代中都有i ++导致2个额外增量,所以在第二次迭代中,值将是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;
}
}