I'm using ember-cp-validations on models and forms. I want a validation to be active only when certain conditions are met. Like, when a route is active or when a component is used in a particular context.
To illustrate, I have a basic email
model that has typical validations on the model itself. When that model is being used in the context of a user account, I want an additional validator to be active (a username-exists
validator, used to tell if they are trying to update their email address to one that exists on another account).
My email model:
import DS from 'ember-data';
import { validator, buildValidations } from 'ember-cp-validations';
const { attr } = DS;
const Validations = buildValidations({
email: {
validators: [
validator('presence', { presence: true, description: 'E-Mail'}),
validator('format', {
type: 'email'
}),
validator('length', {
max: 200,
})
]
},
});
export default DS.Model.extend(Validations, {
email: attr('string'),
optin: attr('boolean', { defaultValue: false })
});
I already have a username-exists custom validator. I just can't figure out how to add that validation conditionally.
The simplest solution would be adding the validated with a disabled
option that keeps it disabled unless a condition is met.
validator('username-available', {
debounce: 300,
disabled: computed.bool(what is the dependent key??)
})
But I have no idea what the dependent key would be. I don't think the route name is available in the model definition.
Or should I add the conditional validator to the component itself? Is there a way to append a validator to the validators
array from the component js?
I think I am probably missing something obvious ;)
Aucun commentaire:
Enregistrer un commentaire