chore(form-core): add a better example for custom validator
This commit is contained in:
parent
b902f9df50
commit
b443ed5723
1 changed files with 11 additions and 13 deletions
|
|
@ -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:
|
All validators extend from the default `Validator` class. Below example is an example of a validator could look like:
|
||||||
|
|
||||||
```js
|
```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) {
|
execute(modelValue, param) {
|
||||||
const hasFeedback = false;
|
return modelValue !== param;
|
||||||
if (modelValue === param) {
|
|
||||||
hasFeedback = true;
|
|
||||||
}
|
|
||||||
return hasFeedback;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static get validatorName() {
|
static getMessage({ fieldName, modelValue, formControl }) {
|
||||||
return 'Required';
|
return `Please make sure values are equal`;
|
||||||
}
|
|
||||||
|
|
||||||
static getMessage({ fieldName }) {
|
|
||||||
return `Please fill in ${fieldName}`;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<validatable-el .validators="${[new MyValidator('foo')]}"></validatable-el>
|
<validatable-el .validators="${[new EqualsText('foo')]}"></validatable-el>
|
||||||
```
|
```
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue