Last active
August 29, 2015 14:22
-
-
Save KrishnaKotari/1691123f3d2f773f92a2 to your computer and use it in GitHub Desktop.
All Code related to Blogpost on Forms with Vaadin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Method 1 | |
TextField textField = fieldGroup.buildAndBind(caption, bindName, TextField.class); | |
//Configure other properties | |
//Method 2 | |
TextField textField = new TextField("Name"); | |
fieldGroup.bind(textField, bindName); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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