Changeset 5021

Show
Ignore:
Timestamp:
07/03/08 10:27:53 (6 months ago)
Author:
magi@…
Message:

Fixes the main problem in #1867, but brings other problems. Needs more work.

Location:
trunk/src/com/itmill/toolkit/ui
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/com/itmill/toolkit/ui/AbstractField.java

    r4874 r5021  
    110110     */ 
    111111    private boolean required = false; 
     112     
     113    /** 
     114     * The error message for the exception that is thrown when the field is 
     115     * required but empty. 
     116     */ 
     117    private String requiredError = ""; 
    112118 
    113119    /** 
     
    611617        return true; 
    612618    } 
    613  
    614     /** 
    615      * Checks the validity of the validatable 
     619     
     620    public class EmptyValueException extends Validator.InvalidValueException { 
     621        /** 
     622         * Serial generated by eclipse. 
     623         */ 
     624        private static final long serialVersionUID = -4488988853486652602L; 
     625 
     626        public EmptyValueException(String message) { 
     627            super(message); 
     628        } 
     629         
     630    } 
     631 
     632    /** 
     633     * Checks the validity of the Validatable by validating the field with all 
     634     * attached validators. 
     635     *  
     636     * The "required" validation is a built-in validation feature. If the field 
     637     * is required, but empty, validation will throw an EmptyValueException 
     638     * with the error message set with setRequiredError(). 
    616639     *  
    617640     * @see com.itmill.toolkit.data.Validatable#validate() 
     
    621644        if (isRequired()) { 
    622645            if (isEmpty()) { 
    623                 throw new Validator.InvalidValueException(""); 
     646                throw new EmptyValueException(requiredError); 
    624647            } 
    625648        } 
     
    716739        ErrorMessage validationError = null; 
    717740        if (isValidationVisible()) { 
    718             if (!(isRequired() && isEmpty())) { 
    719                 try { 
    720                     validate(); 
    721                 } catch (Validator.InvalidValueException e) { 
    722                     validationError = e; 
    723                 } 
     741            try { 
     742                validate(); 
     743            } catch (Validator.InvalidValueException e) { 
     744                validationError = e; 
    724745            } 
    725746        } 
     
    10221043        requestRepaint(); 
    10231044    } 
     1045     
     1046    public void setRequiredError(String requiredMessage) { 
     1047        this.requiredError = requiredMessage; 
     1048        requestRepaint(); 
     1049    } 
     1050 
     1051    public String getRequiredError() { 
     1052        return requiredError; 
     1053    } 
    10241054 
    10251055    /** 
  • trunk/src/com/itmill/toolkit/ui/Field.java

    r3162 r5021  
    5454 
    5555    /** 
     56     * Sets the error message to be displayed if a required field is empty. 
     57     *  
     58     * @param requiredMessage 
     59     *            Error message. 
     60     * @since 5.2.6 
     61     */ 
     62    public void setRequiredError(String requiredMessage); 
     63 
     64    /** 
     65     * Gets the error message that is to be displayed if a required field is 
     66     * empty. 
     67     *  
     68     * @return Error message. 
     69     * @since 5.2.6 
     70     */ 
     71    public String getRequiredError(); 
     72     
     73    /** 
    5674     * An <code>Event</code> object specifying the Field whose value has been 
    5775     * changed. 
  • trunk/src/com/itmill/toolkit/ui/Form.java

    r4860 r5021  
    1818import com.itmill.toolkit.data.Validator.InvalidValueException; 
    1919import com.itmill.toolkit.data.util.BeanItem; 
     20import com.itmill.toolkit.terminal.ErrorMessage; 
    2021import com.itmill.toolkit.terminal.PaintException; 
    2122import com.itmill.toolkit.terminal.PaintTarget; 
     
    171172    } 
    172173 
     174    /** 
     175     * The error message of a Form is the error of the first field with a 
     176     * non-empty error. 
     177     *  
     178     * Empty error messages of the contained fields are skipped, because an 
     179     * empty error indicator would be confusing to the user, especially if there 
     180     * are errors that have something to display. This is also the reason why 
     181     * the calculation of the error message is separate from validation, because 
     182     * validation fails also on empty errors. 
     183     */ 
     184    @SuppressWarnings("unchecked") 
     185    @Override 
     186    public ErrorMessage getErrorMessage() { 
     187        for (final Iterator i = propertyIds.iterator(); i.hasNext();) { 
     188            try { 
     189                AbstractComponent field = (AbstractComponent)fields.get(i.next()); 
     190                ErrorMessage e = field.getErrorMessage(); 
     191                if (e != null) { 
     192                    // Skip empty errors 
     193                    if (e.toString().isEmpty()) 
     194                        continue; 
     195                    return e; 
     196                } 
     197            } catch (ClassCastException ignored) {} 
     198        } 
     199        return null; 
     200    } 
     201 
    173202    /* 
    174203     * Commit changes to the data source Don't add a JavaDoc comment here, we