I am new to vaadin…
Can anyone help me in using custom validators for fields…
I have implemented validator interface in a class and have added it to a field in my form and when i give form.commit() i couldnt get any error messages.
I do no what i am doing wrong here.It willbe very useful for me if anybody help me in solving this…
Where and how do you add the validator? Do the default validators work if you add them in the same location in code? Maybe e.g. the field gets generated more than once using a field factory (e.g. if the data source of the form is set multiple times) and you only set it for a field instance that is not used.
My problem is that the RequiredValidator class itself is not being called…
Can you check what wrong thing i am doing here?
If possible will you be able to share any sample program extending the validator class?
Also can you say me how to add validations for fields of a table?
The following should work at least. Notice that you may be experiencing the problem that validators are not run for empty fields. It’s a rather annoying “feature”, I know, but I’d hesitate to change it because many people use form validation and changing so basic behaviour might break a lot.
class MyValidator implements Validator {
@Override
public void validate(Object value) throws InvalidValueException {
// Simply call the isValid(). It is possible to have
// more complex logic here to also report the reason
// of the failure in better detail.
if (!isValid(value))
throw new InvalidValueException("You did not greet");
}
@Override
public boolean isValid(Object value) {
if (value instanceof String &&
((String)value).equals("hello"))
return true;
return false;
}
}
final Form form = new Form();
form.setFormFieldFactory(new FormFieldFactory() {
@Override
public Field createField(Item item, Object propertyId, Component uiContext) {
if ("hello".equals(propertyId)) {
TextField field = new TextField("Say hello");
field.setNullRepresentation("");
// Add the custom validator
field.addValidator(new MyValidator());
// Add some built-in validators
field.addValidator(new StringLengthValidator(
"Not long enough or null", 3, 100, true));
field.addValidator(new NullValidator(
"Must not be null", false));
return field;
}
return null;
}
});
form.addItemProperty("hello", new ObjectProperty<String>(""));
Button validate = new Button("Validate");
validate.addListener(new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
try {
form.setValidationVisible(true);
form.validate();
layout.addComponent(new Label("OK"));
} catch (InvalidValueException e) {
layout.addComponent(new Label("Failed"));
}
}
});
form.getFooter().addComponent(validate);
Hi Everyone,
How to add custom validator to a textfield.Suppose i want the textfield to only take specific value i.e ((abc)), if the textfield doesnt contain that string it should show the validation symbol
If your list is fixed then why dont you use combobox or dropdown list where user can choose whatever he wants? That would be easy way other than doing validation.