ember-cp-validations
ember-cp-validations copied to clipboard
Common validation across all the models and all the attributes
Is there any option or how can i overwrite the mixin to have a one common validation for all the attributes in each and every model ,
Example : I don't want to validate or show error message until the attribute is dirty Onload i have an empty even that is validating and throwing , How can i stop this ?
One common rule for the validatons ?
So if I understand correctly, you'd like to not show the errors right away but only after the user has interacted with the field, right?
If so, you can do something similar to what I do in the R&R app.
In the controller:
export default Controller.extend({
showErrors: computed('_showErrors', {
get() {
return this._showErrors || { email: false, password: false };
},
set(key, value) {
this.set('_showErrors', value);
return this._showErrors;
}
}),
(...)
});
And in the template:
{{#if showErrors.email}}
<div>{{v-get model 'email' 'message'}}</div>
{{/if}}
{{input type="email" value=model.email focus-out=(action (mut showErrors.email) true)}}
That'd only show the email validation error when the user leaves the field (if there is an error, of course)
To generalize it, you can have a function that creates such computed properties (for the controllers) and you can have a form builder (contextual) component that makes it easier to build for the templates.
showErrors: computed('_showErrors', { get() { return this._showErrors || { email: false, password: false }; }, set(key, value) { this.set('_showErrors', value); return this._showErrors; } }), focus-out=(action (mut showErrors.email) This is all repeating the code that is the reason i want some mixin so that i can use it in every model when it is dirty attributes are present
You can do that, too, just have a property that contains the fields you want to validate (in my example, email
and password
) and then use them in the mixin:
export default Model.extend(ValidateWhenDirty, {
validationFields: ['email', 'password'],
});