Hello,
suppose I have the following Regex validator in my survey:
JSON"validators": [
{
"type": "regex",
"text": {
"de": "Kein gültiger Name",
"default": "Not a valid name"
},
"regex": "^[\\w'\\-,.][^0-9_!¡?÷?¿/\\\\+=@#$%ˆ&*(){}|~<>;:[\\]]{1,}$"
}
]
I don't want to repeat the definitions of text
and regex
properties for each question.
Instead, I want to define it once and then use just by referencing the type.
The built-in validators already allow this: "validators": [ { "type": "email" } ]
.
I looked at the source code on GitHub, and since my validators are based on regular expressions, I'm re-using the email validator class:
TypeScriptclass NameValidator extends SurveyValidator {
private re = /^[\w'\-,.][^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]]{1,}$/;
constructor() {
super();
}
public getType(): string {
return "namevalidator";
}
public validate(
value: any,
name: string = null,
values: any = null,
properties: any = null
): ValidatorResult {
if (!value) return null;
if (this.re.test(value)) return null;
return new ValidatorResult(value, this.createCustomError(name));
}
protected getDefaultErrorText(): string {
return validatorError.name[parseLocale(this.getLocale())];
}
}
Serializer.addClass("namevalidator", [], () => new NameValidator(), "surveyvalidator");
The above works and I'm able to use it by entering the following in my survey definition:
"validators": [ { "type": "name" } ]
The check runs and I get my localized error message just fine.
However, this custom validator does not show up in the dropdown menu:
Any advice on how to make an available to users in the property grid?
Thanks,
Lucas