* Required field.
*/
private boolean required = false;
+
+ /**
+ * The error message for the exception that is thrown when the field is
+ * required but empty.
+ */
+ private String requiredError = "";
/**
* Is automatic validation enabled.
return true;
}
+
+ public class EmptyValueException extends Validator.InvalidValueException {
+ /**
+ * Serial generated by eclipse.
+ */
+ private static final long serialVersionUID = -4488988853486652602L;
+
+ public EmptyValueException(String message) {
+ super(message);
+ }
+
+ }
/**
- * Checks the validity of the validatable
+ * Checks the validity of the Validatable by validating the field with all
+ * attached validators.
+ *
+ * The "required" validation is a built-in validation feature. If the field
+ * is required, but empty, validation will throw an EmptyValueException
+ * with the error message set with setRequiredError().
*
* @see com.itmill.toolkit.data.Validatable#validate()
*/
if (isRequired()) {
if (isEmpty()) {
- throw new Validator.InvalidValueException("");
+ throw new EmptyValueException(requiredError);
}
}
// required fields, as in those cases user is aware of the problem.
ErrorMessage validationError = null;
if (isValidationVisible()) {
- if (!(isRequired() && isEmpty())) {
- try {
- validate();
- } catch (Validator.InvalidValueException e) {
- validationError = e;
- }
+ try {
+ validate();
+ } catch (Validator.InvalidValueException e) {
+ validationError = e;
}
}
this.required = required;
requestRepaint();
}
+
+ public void setRequiredError(String requiredMessage) {
+ this.requiredError = requiredMessage;
+ requestRepaint();
+ }
+
+ public String getRequiredError() {
+ return requiredError;
+ }
/**
* Is the field empty?
*/
public void setRequired(boolean required);
+ /**
+ * Sets the error message to be displayed if a required field is empty.
+ *
+ * @param requiredMessage
+ * Error message.
+ * @since 5.2.6
+ */
+ public void setRequiredError(String requiredMessage);
+
+ /**
+ * Gets the error message that is to be displayed if a required field is
+ * empty.
+ *
+ * @return Error message.
+ * @since 5.2.6
+ */
+ public String getRequiredError();
+
/**
* An <code>Event</code> object specifying the Field whose value has been
* changed.
import com.itmill.toolkit.data.Validator;
import com.itmill.toolkit.data.Validator.InvalidValueException;
import com.itmill.toolkit.data.util.BeanItem;
+import com.itmill.toolkit.terminal.ErrorMessage;
import com.itmill.toolkit.terminal.PaintException;
import com.itmill.toolkit.terminal.PaintTarget;
}
}
+ /**
+ * The error message of a Form is the error of the first field with a
+ * non-empty error.
+ *
+ * Empty error messages of the contained fields are skipped, because an
+ * empty error indicator would be confusing to the user, especially if there
+ * are errors that have something to display. This is also the reason why
+ * the calculation of the error message is separate from validation, because
+ * validation fails also on empty errors.
+ */
+ @SuppressWarnings("unchecked")
+ @Override
+ public ErrorMessage getErrorMessage() {
+ for (final Iterator i = propertyIds.iterator(); i.hasNext();) {
+ try {
+ AbstractComponent field = (AbstractComponent)fields.get(i.next());
+ ErrorMessage e = field.getErrorMessage();
+ if (e != null) {
+ // Skip empty errors
+ if (e.toString().isEmpty())
+ continue;
+ return e;
+ }
+ } catch (ClassCastException ignored) {}
+ }
+ return null;
+ }
+
/*
* Commit changes to the data source Don't add a JavaDoc comment here, we
* use the default one from the interface.