Hi.
I have problem/question. I have my own component (PhoneNumberField), which extends TextField. I want to autovalidate it after somebody writes a wrong number. This component is then bind to binder.
I know that the simplest solution is to add Validator to binder for that component, but then I will need to add this Validator to every binding where I use PhoneNumberField. I don’t want to do this. I dont want to use patterns too, because number is a complex string, which changes due to selected country.
Somebody have a solution for this? Am I missing something?
Michał Lessner:
I know that the simplest solution is to add Validator to binder for that component, but then I will need to add this Validator to every binding where I use PhoneNumberField. I don’t want to do this.
But I don’t want to add validator whenever I use PhoneNumberField, because I will have to add this in every place, because this Validation algorithm will never change for this field.
I don’t know how you can add a server side validation inside the component because since vaadin 8 the validator/converter has been moved to the binder.
You don’t want client side validation ? (perhaps it’s not enough but as a user I like it)
Vilius Kukanauskas:
I actually do not understant the “I will have to add this in every place” part.
Let’s imagine a scenario where I have 3 separate views. Each one of them at some point uses my custom component defined as follows:
PhoneNumberField extends TextField{
...
@Override
boolean isInvalid() {
return /*validate value with phone number format*/;
}
}
Currently binder disregards isInvalid method implemented in my custom textfield, so in each of the 3 views I will have to call:
PhoneNumberField phoneNumber = new PhoneNumberField();
binder.forField(phoneNumber).withValidator(new PhoneNumberValidator()).bind(getter, setter);
What i would like to achieve is for binder to, in addition to all explicitly defined validators, include a call to isInvalid() method of the phoneNumber upon validation. So the resulting code utilizing my PhoneNumberField should look like this:
PhoneNumberField phoneNumber = new PhoneNumberField();
binder.forField(phoneNumber).bind(getter, setter);
Using .withValidator(new PhoneNumberValidator()). becomes optional.
I haven’t tested this myself but you may do something like this -
(1) Create a class named InternalPhoneNumberField with appropriate isInvalid() method for validating the phone number.
(2) Create PhoneNumberField class by extending AbstractCompositeField with InternalPhoneNumberField as its content.