'How can I dynamically change validator options after creating in ember-cp-validations?
I need to change 'presence' option from 'true' to 'false' depending on dynamic form state. If formState == 'a' 'name' field is required, if == 'b' then not, and if I dynamically toggled formState, validator should change its option.
I tried do that, but it didn't help:
//form/component.js
init() {
this._super(...arguments);
const Validations = buildValidations({
name: validator('presence', {
presence: Ember.computed('formState', function() {
return this.get('formState') == 'a';
})
}),
});
// I use ember-validated-form-buffer for buffering changes
this.set('formValues', formBufferProperty('model', Validations));
}
Is there any way to solve it?
Solution 1:[1]
Just disable this validator by doing so disabled: Ember.computed.equal('model.formState', 'b')
Solution 2:[2]
The answer is to sync state through the model.
//form/component.js
init() {
this._super(...arguments);
const Validations = buildValidations({
name: validator('presence', {
presence: Ember.computed.equal('model.formState', 'a')
})
});
// I use ember-validated-form-buffer for buffering changes
this.set('formValues', formBufferProperty('model', Validations));
}
But for those who use ember-validated-form-buffer: it is sensitive to imports order and should be above the ember-cp-validations, at least in current versions:
"ember-validated-form-buffer": "0.0.1",
"ember-cp-validations": "3.1.2",
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | xoma |
| Solution 2 | elatonsev |
