Hi @jakecham ,
There is already a well defined framework to write dialog validations. Let me explain with your use case.
As this framework use jQuery code, Create a specific clientlibs to write validation code and load this clientlibs in only edit mode. You can add/load this clientlibs by adding property "extraClientlibs" to cq:dialog node.
extraClientlibs="[aemgeeks.components.author.editor]"
Add a property "validation" to your dialog field. Value of this property will be used to identify field in validation code. Basically this property will be used to map your dialog field and validation code. Let's assume you have dialog field with name property "fname".
<fname
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
fieldLabel="First Name"
name="./fname"
validation="geeks-firstname-validation"/>
Now write code to validate your field as per your need.
In below code selector used to identify your field.
validate is used to write your own custom code for validation. Update validate code as per your need.
/* global jQuery, Coral */
(function($, Coral) {
"use strict";
var registry = $(window).adaptTo("foundation-registry");
registry.register("foundation.validation.validator", {
selector: "[data-validation=geeks-firstname-validation]",
validate: function(element) {
let el = $(element);
let pattern=/[0-9]/;
let value=el.val();
if(pattern.test(value)){
return "Please add only characters in First name";
}
}
});
})(jQuery, Coral);
Sharing file
Dialog - https://github.com/aemgeeks1212/aemgeeks/blob/master/ui.apps/src/main/content/jcr_root/apps/aemgeeks/components/content/author/_cq_dialog/.content.xml
Validation code - https://github.com/aemgeeks1212/aemgeeks/blob/master/ui.apps/src/main/content/jcr_root/apps/aemgeeks/components/content/author/clientlibs/editor/js/author.js
If you are interested to learn framework. Here are video tutorials.
https://youtu.be/wFYU1WCkRI0
https://youtu.be/tsd8s8UMBRY
https://youtu.be/MaCb_2XhEUQ