themisir / form-validator

Simplest form validation for flutter form widgets
https://pub.dev/packages/form_validator
MIT License
78 stars 41 forks source link

Add isEmpty on required string #27

Open morrowyn opened 3 years ago

morrowyn commented 3 years ago

ValidationBuilder().required("").build() only works on strings that are null. However when a string is empty "" the validator says that it's valid. I expected an invalid error.

image

TheJosh commented 3 years ago

Yeah, it appears that when using a TextEditingController, the value is often silently converted from a null into an empty string, so as a result this required check never hits, even though the fields are empty.

Workaround is adding minLength(1) but this is a bit silly and also gives a not very helpful error message.

TheJosh commented 3 years ago

Another workaround is an extension method:

extension CustomValidationBuilder on ValidationBuilder {
  notEmpty() => add((value) {
    return value.isEmpty ? 'This field is required' : null;
  });
}