slickgrid使用正则表达式验证列 [英] slickgrid validate column with regex
问题描述
有一个带有列验证的简单示例:
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};
}
}
并且要验证列只需要放置此项选项:验证器:requiredFieldValidator
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.
推荐答案
默认情况下,您无法将更多参数传递到验证器
方法,但是您可以轻松编辑源代码以允许它。
By default, you cannot pass more parameters into the validator
method, however you can easily edit the source to allow it.
在 slick.editors.js
中寻找:
this.validate = function () {
if (args.column.validator) {
var validationResults = args.column.validator($input.val());
if (!validationResults.valid) {
return validationResults;
}
}
return {
valid: true,
msg: null
};
};
更改: 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:
function requiredFieldValidator(value, input)
有了这个,您可以使用 input.attr('validation-expression')
或<获取您想要的任何输入属性code> input.data ... 或者其他什么。
With that, you can get whatever attributes you want out of input with input.attr('validation-expression')
or input.data...
or whatever.
这篇关于slickgrid使用正则表达式验证列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!