Dynamic validation and change style on error and success validation


When the user leaves a field, i would like to add a validation by default. I would like to do it for all my fields without having to add the specific commandline on each.
Is it possible ?

After that, i would like to change my field style in order to colorize the background color :

  • in green when validation is good
  • in red when it fails
    (or other style changes)

I would like to know if it is possible to customize it and where i could do it.

Thanks for you help


So is it some specific validation that you want on every field. Usually you have a little bit of different requirements on every field, which makes a global validator impossible to make.

If you have the same validator for many fields, then you could probably wrap the textfield into a class which automatically gives the validator to it. For example:

	public class LengthTextField extends TextField{
		public LengthTextField() {
			addValidator(new StringLengthValidator("Wrong length!!!",6, 20,true));

Now you can just create new LengthTextField() instead of new TextField() to get a validator to every one.

I think the default of textfield that doesn’t pass validation is that it turns the field’s background to red. It does it by giving the field an extra css identifier which specifies the background color. You can override the css to make it look any way you want. The green part is a little trickier. You could add a ValueChangeListener to the field in that class above. On a ValueChangeEvent you would check !field.isEmpty() && field.isValid(). If true then field.addStyleName(“mygreenfield”);, if false then field.removeStyleName(“mygreenfield”);. Then you add the identifier into the css that turns the field green.

Hope that helps.


Thanks for you answer.

Actually, i don’t want to do the same validation on each field but i would like to have the same result if isValid returns true (green bullet) and if it returns false (red bullet).

Is there any simple way to add the valueChangeListener to all fields without adding it for each ?