Complex validation but isValid limitation

Hello,

I’m going to explain my need and problems i have :

I have 2 fields in a form.
Those 2 fields are not required but if no one is set, i want to display both of them in error.
My validation is done on blur events.

Here is my algorithm :

  • blur on a field without any text in it (length : 0)
  • validation autoexecuted by BlurListener : isValid ==> my field has a custom validator. This validator allows to validate many fields (kind of ValidIfAnotherFieldsValidatorsAreValid)
  • the method isValid of AbstractField check if my field is empty then if it is required before launching validators. As it isn’t required, it doesn’t execute validation after.

I understand why the AbstractField manage isValid method like this (as explained here : http://dev.vaadin.com/ticket/2817)

But in my need, i would like to override those mecanism with a property like : setExecuteValidatorIfNull(boolean).
By this feature, i would be able to tell AbstractField to do this mecanism instead :


  public boolean isValid() {

        if (isEmpty()) {
            if (isRequired()) {
                return false;
            } else {
              /* PILEROU NEW CODE */
              if(! getExecuteValidatorIfNull() ) {
                 return true;
              }
              /* PILEROU END OF NEW CODE */
            }
        }

        if (validators == null) {
            return true;
        }

        final Object value = getValue();
        for (final Iterator<Validator> i = validators.iterator(); i.hasNext();) {
            if (!(i.next()).isValid(value)) {
                return false;
            }
        }

        return true;
    }


I haven’t seen another way to do what i want to do.
Is there another tip ?

I’ve few times skipped whole Vaadin built-in validation and done all validation by myself. You can do that by saying setValidationVisible(false) to that field and the run those validators against your fields manually by some method when blur happens.


for (Validator v: f.getValidators()) {
	v.validate(f.getValue());
}

Hi Mauno

Thanks for your answer.
Your tip helped me to display error message by setting componentError. The error message wasn’t displayed.
I overrided like you validation mecanism. It is now working.