reactive_forms icon indicating copy to clipboard operation
reactive_forms copied to clipboard

setErros and validate return type missmatch

Open vasilich6107 opened this issue 4 years ago • 7 comments

Hi @joanpablo

setErrors accepts non nullable value https://github.com/joanpablo/reactive_forms/blob/master/lib/src/models/models.dart#L545

but the built on validators return nullable value https://github.com/joanpablo/reactive_forms/blob/master/lib/src/validators/email_validator.dart#L13

vasilich6107 avatar Aug 05 '21 14:08 vasilich6107

Hi @vasilich6107,

Mmm yes, you are right, In my opinion, you should not use the method setError to set a null value. If the idea is to clear all errors then you can just pass an empty object setError({}) or maybe we should have a removeAllErrors public method.

joanpablo avatar Aug 05 '21 14:08 joanpablo

Hi @joanpablo The main issue is that I cannot use

someField.setErrors(EmailValidator.validate(someField));

I have to write

someField.setErrors(EmailValidator.validate(someField) ?? {});

Why not to make those types identical?

vasilich6107 avatar Aug 08 '21 19:08 vasilich6107

This is one of the things that could be improvedwith stricter typing and getting rid of dynamic.

kuhnroyal avatar Aug 08 '21 20:08 kuhnroyal

@kuhnroyal im 100% with you Dynamic is pain

vasilich6107 avatar Aug 09 '21 04:08 vasilich6107

Mmm, I see @vasilich6107,

But, what does this supposed to do?

someField.setErrors(null);

1-) Remove all errors from the field? 2-) Don't do anything at all?

And at the end:

someField.setErrors(EmailValidator.validate(someField) ?? {});

I don't think it looks too bad.

I can understand that I need to pass the results from EmailValidator.validate(someField) or empty object in case of null, it is clear to me. but this expression: someField.setErrors(null); is not clear to me.

joanpablo avatar Aug 09 '21 06:08 joanpablo

Also AbstractControl.errors is not nullable, I mean you will always get an instance of a Map, not a null value, that's why the setError does not allows setting a null value. If you change setErrors to accept a null then you will need to change the data type of AbstractControl.errors, and I don't think it really gives an advantage.

joanpablo avatar Aug 09 '21 07:08 joanpablo

@joanpablo i will write my thoughts a little bit later. While you are here could you comment on this https://github.com/joanpablo/reactive_forms/issues/200#issuecomment-894694053

vasilich6107 avatar Aug 09 '21 07:08 vasilich6107