There is a simple sample with column validation:
function requiredFieldValidator(value) {
if (value == null || value == undefined || !value.length) {
return {valid: false, msg: "This is a required field"};
} else {
return {valid: true, msg: null};
}
}
and to validate column it is just needed to put this option: validator: requiredFieldValidator
But how can I use regex function if I need to pass extra parameter - regex string.
The best way to approach this, in my view anyways, is to code your own editor which you'll add into
slick.editor.js
as another new custom editor. This file is made for that too. I did implement the regex test and it works great.Here's my new editor which is not only working for Regex but also for various condition types, for example an option of
min:2
would required a minimum number of 2, while aminLength:2
would required the input to be a String of at least 2 chars, etc... Now for the one you're really looking for, that would be in my code definition thepattern
type. So basically, you'll have to include this code inside theslick.editor.js
:Then inside my SlickGrid column definition I'm calling that new editor which I defined and passing some options which I decided to pass into an
editorOptions
as an Object and that gives me more flexibility to add any options I want, pattern, msg, minLength, etc... all at one. My example is for an email regex pattern validation.And voilà, works like a charm!!! I'm barely using the
editor:TextCellEditor
anymore, since my newConditionalCellEditor
editor gives me a lot more flexibility. Hope it helps and let me know how it goes...By default, you cannot pass more parameters into the
validator
method, however you can easily edit the source to allow it.in
slick.editors.js
look for:change:
var validationResults = args.column.validator($input.val());
to:
var validationResults = args.column.validator($input.val(), $input);
this will change your validator method signature to something like:
With that, you can get whatever attributes you want out of input with
input.attr('validation-expression')
orinput.data...
or whatever.In order to extend the answer of @mike-gwilt, you can use the cellAttrs column options to specify (in your column definition) the regular expressions and the message to report, like this:
Then the html generated looks something like this:
Finally, as you can access the input element inside your validation function, define it like this:
This was very helpful. I am creating different input types for every type of possible entry - email, phone, zip and so forth. To do this with JSON, you have to modify your slick.grid.js file to evaluate the entries to make them an object call.
}
Make your JSON columns have this update:
Make youre slick.editors.js look like this: