Search in sources :

Example 1 with EmailValidator

use of com.vaadin.data.validator.EmailValidator in project vaadin-samples by xpoft.

the class SimpleForm method PostConstruct.

@PostConstruct
public void PostConstruct() {
    User user = new User();
    setCaption("Simple form");
    setSpacing(true);
    TextField name = new TextField("Name");
    name.setRequired(true);
    name.setRequiredError("Please enter a Name!");
    name.setWidth(COMMON_FIELD_WIDTH);
    name.addValidator(new StringLengthValidator("It must be 3-25 characters", 3, 25, false));
    name.setNullRepresentation("");
    name.setImmediate(true);
    TextField email = new TextField("Email:");
    email.setRequired(true);
    email.setRequiredError("Please enter a Email");
    email.setWidth(COMMON_FIELD_WIDTH);
    email.addValidator(new EmailValidator("Not valid email"));
    email.setNullRepresentation("");
    email.setImmediate(true);
    final BeanFieldGroup<User> fieldGroup = new BeanFieldGroup<User>(User.class);
    fieldGroup.setItemDataSource(new BeanItem<User>(user));
    fieldGroup.bind(name, "name");
    fieldGroup.bind(email, "email");
    // The cancel / apply buttons
    HorizontalLayout buttons = new HorizontalLayout();
    buttons.setSpacing(true);
    Button discardChanges = new Button("Reset", new Button.ClickListener() {

        public void buttonClick(Button.ClickEvent event) {
            fieldGroup.discard();
        }
    });
    buttons.addComponent(discardChanges);
    buttons.setComponentAlignment(discardChanges, Alignment.MIDDLE_LEFT);
    Button apply = new Button("Save", new Button.ClickListener() {

        public void buttonClick(Button.ClickEvent event) {
            try {
                fieldGroup.commit();
                Notification.show("OK");
            } catch (Exception e) {
                Notification.show("Error: " + e.getMessage(), Notification.Type.WARNING_MESSAGE);
            }
        }
    });
    buttons.addComponent(apply);
    addComponent(name);
    addComponent(email);
    addComponent(buttons);
}
Also used : BeanFieldGroup(com.vaadin.data.fieldgroup.BeanFieldGroup) User(ru.xpoft.vaadin.sample.integration.beans.User) EmailValidator(com.vaadin.data.validator.EmailValidator) StringLengthValidator(com.vaadin.data.validator.StringLengthValidator) PostConstruct(javax.annotation.PostConstruct)

Aggregations

BeanFieldGroup (com.vaadin.data.fieldgroup.BeanFieldGroup)1 EmailValidator (com.vaadin.data.validator.EmailValidator)1 StringLengthValidator (com.vaadin.data.validator.StringLengthValidator)1 PostConstruct (javax.annotation.PostConstruct)1 User (ru.xpoft.vaadin.sample.integration.beans.User)1