Open vursen opened 1 year ago
I would probably implement this usecase with an internal binder in the custom field. So the validation would be displayed properly. But if you are using an internal binder in the custom field then the custom field will return only a valid value. So the main binder will always have the valid value.
So you need to create your own validator:
binder.forField(phone)
.asRequired("The field is required")
// This validator actually checks the country code sub-field.
.withValidator(
value -> phone.checkValidity(value),
"Custom field is invalid")
.bind("user.phone");
I'm wondering, as it's an internal behavior of the custom-field, if we should use the HasValidator
interface for this.
Something like:
public Validator<String> getDefaultValidator() {
return (value, context) -> {
return this.checkValidity(value);
};
}
The documentation seems to describe this as a usecase: https://github.com/vaadin/flow/blob/main/flow-data/src/main/java/com/vaadin/flow/data/binder/HasValidator.java#L44
In short use the same mechanism for the datetimepicker and a custom field.
@jcgueriaud1 I have similar experiment here https://github.com/TatuLund/ProtoTools/blob/master/src/main/java/org/vaadin/addons/tatu/prototools/Form.java I.e. sub form as a field.
By default, when invalid, CustomField doesn't visually indicate that, except for showing an error message. The recent UX testing showed that it can be difficult to spot an invalid CustomField among other fields in the form, therefore. You can solve that by overriding
setInvalid()
for a CustomField to make it propagate the invalid state to the child fields.That said, sometimes there is a need to only highlight a specific sub-field as invalid based on the validation error.
Let's say you have a PhoneField that consists of a country code and phone number sub-fields. When the CustomField value doesn't have a country code, you may expect Binder to show an error message like "Must have a country code" under the whole field but highlight only the country code sub-field as invalid.
This isn't possible at the moment unless with some dirty workarounds, especially when using Binder where you can set up errors only for the whole field.
PhoneField example
Additional context
Related to https://github.com/vaadin/platform/issues/3066