Regarding custom validators

Hi Everybody,

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…

Thanks and Regards,
Shankari

Hi,

It should work like that, so there must be something wrong. Seeing some code would help in saying what’s wrong with it.

This should work.

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.

Hi Marko and Henri…

Thanks for your reply…
I have attached my code with this post. It would be very helpful if it is been solved…

Thanks and regards,
Shankari G R
11896.java (2 KB)

The TextField should probably have setNullRepresentation(“”). The default null representation is string “null”, which is probably not what you want.

Alternatively, you could check in the validator if the object is a String which .equals(“”).

The default “null” value is essentially “this looks ugly - set a better value for it”.

Hi Marko

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?

Thanks and regards,
Shankari G R

Henri,

Actually it works for the default validators say for eg a postalcodevalidator…

Hi,

I havent solved this issue yet…
If anybody is able to identify kindly reply me…

Thanks and regards,
Shankari G R

Hi,

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);

See the
on-line example
.

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

Thanks & Regards
Vineel

As explained by Marko use server-side RegexpValidator. For example:

CSValidatedTextField tf = new CSValidatedTextField(“MyFieldType”);
String type_regexp = "[0-9]
{6}[±A]
[0-9]
{3}[0-9a-zA-Z]
"; //whatever your expression is

tf.setRegExp(type_regexp, “000000-000A”);
tf.setAllowInvalid(false);

// The server-side validation
tf.addValidator(new RegexpValidator(type_regexp, “Invalid data”));
tf.setRequired(true);
tf.setRequiredError(“Required”);

use CSValidatedTextFields. it may help you with that

Hi Arade,

I have a list of Strings,so if the text field contains any one of the string from the list it should work otherwise show 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.