Search in sources :

Example 6 with Validator

use of com.vaadin.data.Validator in project VaadinUtils by rlsutton1.

the class DateTimePickerInline method createTextDateField.

private TextField createTextDateField() {
    final TextField displayDate = new TextField();
    // add validator to text date field
    displayDate.addValidator(new Validator() {

        private static final long serialVersionUID = 1L;

        @Override
        public void validate(Object value) throws InvalidValueException {
            try {
                Date date = dateFormatter.parse((String) value);
                datePicker.setValue(date);
            } catch (ParseException e) {
                throw new InvalidValueException(e.getMessage());
            }
        }
    });
    // add value change listener to text date field
    displayDate.addValueChangeListener(new ValueChangeListener() {

        private static final long serialVersionUID = 1L;

        @Override
        public void valueChange(Property.ValueChangeEvent event) {
            try {
                Date date = dateFormatter.parse((String) event.getProperty().getValue());
                datePicker.setValue(date);
            } catch (ParseException e) {
            // do nothing, well handle this in a validator
            }
        }
    });
    return displayDate;
}
Also used : InvalidValueException(com.vaadin.data.Validator.InvalidValueException) TextField(com.vaadin.ui.TextField) ParseException(java.text.ParseException) Property(com.vaadin.data.Property) Validator(com.vaadin.data.Validator) Date(java.util.Date)

Example 7 with Validator

use of com.vaadin.data.Validator in project VaadinUtils by rlsutton1.

the class ReportParameterDateTimeRange method createValidators.

void createValidators() {
    startfield.addValidator(new Validator() {

        private static final long serialVersionUID = 1L;

        @Override
        public void validate(Object value) throws InvalidValueException {
            if (value == null) {
                throw new InvalidValueException("Start date is invalid");
            }
            if (endfield.getValue() != null && ((Date) value).after(endfield.getValue())) {
                throw new InvalidValueException("Start date must be before the end date");
            }
        }
    });
    endfield.addValidator(new Validator() {

        private static final long serialVersionUID = 1L;

        @Override
        public void validate(Object value) throws InvalidValueException {
            if (value == null) {
                throw new InvalidValueException("End date is invalid");
            }
            if (startfield.getValue() != null && ((Date) value).before(startfield.getValue())) {
                throw new InvalidValueException("Start date must be before the end date");
            }
        }
    });
}
Also used : Validator(com.vaadin.data.Validator) Date(java.util.Date)

Aggregations

Validator (com.vaadin.data.Validator)7 InvalidValueException (com.vaadin.data.Validator.InvalidValueException)3 TextField (com.vaadin.ui.TextField)3 Date (java.util.Date)2 Property (com.vaadin.data.Property)1 StringLengthValidator (com.vaadin.data.validator.StringLengthValidator)1 ComboBox (com.vaadin.ui.ComboBox)1 PasswordField (com.vaadin.ui.PasswordField)1 ParseException (java.text.ParseException)1 DateTime (org.joda.time.DateTime)1 Duration (org.joda.time.Duration)1 Test (org.junit.Test)1