"Required" field can be empty, with no validation error (undefined is not the same as an empty string?)

When I declare a field to be required, I want the user to enter a non-empty value. If the user clicks the “X” icon at the right of the field to clear it, the form fails validation (and the property in my data object is undefined). But if the user deletes all of the characters in the field, resulting in an empty string for that property, it does NOT fail validation. Shouldn’t validation fail if a string field has an empty value?

[original thread by Kevin Ilsen]

Hi! Internally we also had the discussion on what to do with an empty text field, i.e. whether to store the empty string or undefined in the data. We then settled on storing the empty string, but to introduce the clear button for cases where people would rather have undefined.

If you don’t like the behavior and would prefer to always have undefined for the empty string you can register a custom renderer which does exactly that. Then you also don’t need the clear button.

If you just want a validation error for both the undefined and the empty string case, then you can add the required attribute to check whether any string exists (as you already do) and add a minLength attribute to specify that it should be at least one (or more) characters. Also you might want to define a pattern to also check for that the single string is not a space.