chore(form-core): add a better example for custom validator

This commit is contained in:
Thijs Louisse 2021-11-25 15:23:07 +01:00 committed by Thijs Louisse
parent b902f9df50
commit b443ed5723

View file

@ -113,27 +113,25 @@ As you can see the 'validators' property expects a map (an array of arrays). So,
All validators extend from the default `Validator` class. Below example is an example of a validator could look like:
```js
class MyValidator extends Validator {
class EqualsText extends Validator {
static validatorName = 'EqualsText';
/**
* Returns true when 'activated' (c.q. in error/warning/info state)
* @param {string} modelValue
*/
execute(modelValue, param) {
const hasFeedback = false;
if (modelValue === param) {
hasFeedback = true;
}
return hasFeedback;
return modelValue !== param;
}
static get validatorName() {
return 'Required';
}
static getMessage({ fieldName }) {
return `Please fill in ${fieldName}`;
static getMessage({ fieldName, modelValue, formControl }) {
return `Please make sure values are equal`;
}
}
```
```html
<validatable-el .validators="${[new MyValidator('foo')]}"></validatable-el>
<validatable-el .validators="${[new EqualsText('foo')]}"></validatable-el>
```
## Installation