Sometimes it is desirable to have a particular input widget in a CQ5 WCM dialog to be fitted with a custom validation. Imagine you have two password fields and you would like to validate that the passwords match prior to saving the dialog.
The Dialog
The following is a simple dialog that features two input widgets for passwords:
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:Dialog"
xtype="panel">
<items jcr:primaryType="cq:WidgetCollection">
<password
jcr:primaryType="cq:Widget"
fieldLabel="Password"
name="./password"
allowBlank="false"
xtype="password"/>
<passwordConfirm
jcr:primaryType="cq:Widget"
fieldLabel="Confirm Password"
name="./password"
allowBlank="false"
validator="function(value) { verifyPasswords(value) }"
xtype="password"/>
</items>
</jcr:root>
Notice the validator property on the passwordConfirm input. It specifies a function that is evaluated and itself references a function, that you can easily define in a custom JavaScript file included in the head of your page in authoring mode.
(more…)