ng-admin icon indicating copy to clipboard operation
ng-admin copied to clipboard

`validator` function isn't called when typing?

Open jpetitcolas opened this issue 9 years ago • 3 comments

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.

jpetitcolas avatar Apr 01 '15 09:04 jpetitcolas

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 :

  1. immediate (as you type)
  2. onFocusChange ( when you move away from the field )
  3. onSubmission ( when submitting the form ) - this is the current behavior

ramSeraph avatar Apr 30 '15 20:04 ramSeraph

:+1: with @ramSeraph. I would just add the possibility to return false for a failure.

jpetitcolas avatar May 04 '15 07:05 jpetitcolas

I would just change the option names to onBlur and onSubmit.

jpetitcolas avatar May 04 '15 07:05 jpetitcolas