ng-admin
ng-admin copied to clipboard
`validator` function isn't called when typing?
I want to restrict a field to use only alphabetic characters. I tried to configure my field like the following:
nga.field('service').validation({
required: true,
maxLength: 255,
validator: function(name) {
return !name.match(/^[A-Za-z]+$/g);
}
})
I may misunderstand how this validator works. I am expecting it to be called after each key, updating the field feedback icon. Yet, it seems to be called only when sending the form. And, even in this case, I have to throw an error, as returning false
doesn't work.
nga.field('service').validation({
required: true,
maxLength: 255,
validator: function(name) {
if (!name.match(/^[A-Za-z]+$/g)) {
throw new Error('Service name should contain only alphabetic characters (no punctuation or numbers).');
}
}
})
But in this case, the feedback icon on the field is not updated.
I have run into this as well.
I can see how throwing an error is better (as a way to say what is wrong). But arguably it could be cleaner to just return true on success and return a failure message string on failure
Will it also be possible to take an extra parameter which will tell when to trigger the validation? Options being :
- immediate (as you type)
- onFocusChange ( when you move away from the field )
- onSubmission ( when submitting the form ) - this is the current behavior
:+1: with @ramSeraph. I would just add the possibility to return false
for a failure.
I would just change the option names to onBlur
and onSubmit
.