form-validator icon indicating copy to clipboard operation
form-validator copied to clipboard

Add isEmpty on required string

Open morrowyn opened this issue 4 years ago • 2 comments
trafficstars

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

morrowyn avatar May 18 '21 16:05 morrowyn

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 avatar May 24 '21 02:05 TheJosh

Another workaround is an extension method:

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

TheJosh avatar May 24 '21 02:05 TheJosh