@NotNull field with default "" value fires validation error

Hello.
This is DTO field:

@NotNull String type

this is UI form:

<TextField {...field(model.type)} />

The default value of the field while read() is “”.
If I don’t click the field - form submitting is ok.
If I click the field - form fires validation error.
How to fix it?

Hilla 24.8.3

As a workaround, removed @NotNull and replace null value as “” string.

There is probably a bug here: the behavior is not coherent. I’ll open an issue for that.

What’s your use case? If you simply want to avoid null values and "" is OK for you, just use org.jspecify.annotations.NonNull instead.

Does org.jspecify.annotations.NonNull works different from jakarta.validation.constraints.NotNull?

jakarta.validation.constraints.NotNull is a validation constraint, so it is only considered when running validation.

org.jspecify.annotations.NonNull tells tools such as IDEs or the Hilla generator that they can expect the field to never have null as a value.

In any case, if you want a Hilla form to automatically initialize a non-null field with an empty string, then use one of the annotations listed here: How to manage type nullability in Hilla | Vaadin

I have hit this issue too when using the Form within Auto Crud - a field, such as a nullable UUID field, will be initialised as null, however clicking on it changes it to an empty string, at which point form submission fails validation on the server.

The TextField’s default value is an empty string. On my experience is not capable to be set null. When a value change event is triggered, the value will never be null or undefined, even when the field is not required.