Skip to content

Instantly share code, notes, and snippets.

@KrishnaKotari
Last active August 29, 2015 14:22
Show Gist options
  • Save KrishnaKotari/1691123f3d2f773f92a2 to your computer and use it in GitHub Desktop.
Save KrishnaKotari/1691123f3d2f773f92a2 to your computer and use it in GitHub Desktop.
All Code related to Blogpost on Forms with Vaadin
@NotBlank(message = "Employee should have a name")
private String name;
@NotBlank(message = "Select a Dept from the drop down")
private String dept;
private String city;
private boolean isPermanentEmployee;
@NotNull(message = "Date of birth cannot be empty")
// TODO create a new constraint for
private Date dateOfBirth;
//other fields
//Getter & Setters
// Method 1
TextField textField = fieldGroup.buildAndBind(caption, bindName, TextField.class);
//Configure other properties
//Method 2
TextField textField = new TextField("Name");
fieldGroup.bind(textField, bindName);
try {
fieldGroup.commit();
} catch (CommitException e) {
// Get all in valid fields and make validation messages visisble
Map<Field<?>, InvalidValueException> invalidFields = e.getInvalidFields();
// TODO handle it in a better way
for (Map.Entry<Field<?>, InvalidValueException> invalidField : invalidFields.entrySet()) {
((AbstractField<?>) invalidField.getKey()).setValidationVisible(true);
}
if (invalidFields.isEmpty()) {
// TODO Handle this error
e.printStackTrace();
Notification.show("Save failed, Please try again");
}
}
fieldGroup.addCommitHandler(new CommitHandler() {
/**
*
*/
private static final long serialVersionUID = 2695561987381248384L;
@Override
public void preCommit(CommitEvent commitEvent) throws CommitException {
// Check for validations
fieldGroup.isValid();
}
@Override
public void postCommit(CommitEvent commitEvent) throws CommitException {
//TODO save the bean to the database or any other ops
Employee bean = fieldGroup.getItemDataSource().getBean();
Notification.show("Record with Name "+bean.getName()+" was saved Successfully");
}
});
//Sets the datasource to the FieldGroup which then shows the values
// in the form automcatically
fieldGroup.setItemDataSource(employee);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment