
In vaadin framework when a validation error occurred, it was not clear what the problem was. None of them thought about hovering their mouse over the error indicator (if they even noticed the indicator) to get the precise error message.I read in the Book of Vaadin that the placement of the error indicator is managed by the layout in which the component is contained. However, it doesn't seem to say anything about directly showing the error message.So i decided to do that.
For example here i am taking as simple example user object to validate
Model:
public class User extends BaseIsisEntity {
private Integer id;
private String territoryCode;
private boolean disabled;
private String title;
private String firstName;private String middleName;
private String lastName;
private String homeTelephone;
private String workTelephone;
private String mobile1;
private String mobile2;
private String fax;
private String emailAddress;
private String website;
//Getter and setter
View:
public class EditUserView extends View{
private BeanFieldGroup<User> userBeanFieldGroup;
private Label errorLbl;
VerticalLayout userFormLayout=new VerticalLayout();
errorLbl = new Label("", ContentMode.HTML);
private Label errorLbl;
VerticalLayout userFormLayout=new VerticalLayout();
errorLbl = new Label("", ContentMode.HTML);
userFormLayout.addComponent(errorLbl);
TextField territoryCode = new TextField("Territory Code");
territoryCode.setRequired(true);
territoryCode.setRequiredError("Territory Code Required");
userBeanFieldGroup.bind(territoryCode, "territoryCode");
TextField firstnameField = new TextField("First Name");
firstnameField.setRequired(true);
firstnameField.setRequiredError("First Name Required");
userBeanFieldGroup.bind(firstnameField, "firstName");
userFormLayout.addComponent(firstnameField);
TextField lastNameTxt = new TextField("Last Name");
lastNameTxt.setRequired(true);
lastNameTxt.setRequiredError("Last Name Required");
userBeanFieldGroup.bind(lastNameTxt, "lastName");
userFormLayout.addComponent(lastNameTxt);
TextField middleNameTxt = new TextField("Middle Name");
userBeanFieldGroup.bind(middleNameTxt, "middleName");
userFormLayout.addComponent(middleNameTxt);
TextField homeTelephoneText = new TextField("Home Telephone");
homeTelephoneText.setRequired(true);
homeTelephoneText.setRequiredError("Home Telephone is required");
userBeanFieldGroup.bind(homeTelephoneText, "homeTelephone");
userFormLayout.addComponent(homeTelephoneText);
TextField workTelePhoneTxt = newTextField("Work Telephone");
workTelePhoneTxt.setRequired(true);
workTelePhoneTxt.setRequiredError("Work Telephone is required");
userBeanFieldGroup.bind(workTelePhoneTxt, "workTelephone");
userFormLayout.addComponent(workTelePhoneTxt);
TextField mobile1Txt = new TextField("Mobile1");
mobile1Txt.setRequired(true);
mobile1Txt.setRequiredError("Mobile1 is required");
userBeanFieldGroup.bind(mobile1Txt, "mobile1");
userFormLayout.addComponent(mobile1Txt);
TextField mobile2Txt = new TextField("Mobile2");
mobile2Txt.setRequired(true);
mobile2Txt.setRequiredError("Mobile2 is required");
userBeanFieldGroup.bind(mobile2Txt, "mobile2");
userFormLayout.addComponent(mobile2Txt);
TextField emailAddressTxt = new TextField("Email Address");
emailAddressTxt.setRequired(true);
emailAddressTxt.setRequiredError("Email Address is required");
userBeanFieldGroup.bind(emailAddressTxt, "emailAddress");
userFormLayout.addComponent(emailAddressTxt);
HorizontalLayout buttonLayout=new HorizontalLayout();
buttonLayout.addComponent(new Button("Save", new Button.ClickListener() {
@Override
public void buttonClick(Button.ClickEvent event) {
try {
userBeanFieldGroup.setBuffered(true);
userBeanFieldGroup.commit();
if (editObj.getId() == null) {
userService.add(editObj);
}
} catch (FieldGroup.CommitException e) {
//This line is very important
errorLbl.setValue(ErrorUtils.showComponentErrors(userBeanFieldGroup.getFields()));
}
}
}));
@Override
public void buttonClick(Button.ClickEvent event) {
try {
userBeanFieldGroup.setBuffered(true);
userBeanFieldGroup.commit();
if (editObj.getId() == null) {
userService.add(editObj);
}
} catch (FieldGroup.CommitException e) {
//This line is very important
errorLbl.setValue(ErrorUtils.showComponentErrors(userBeanFieldGroup.getFields()));
}
}
}));
}
Util class:
public class ErrorUtils {
public static String showComponentErrors(final AbstractComponent[] componentArray) {
List<String> errorList = ErrorUtils.getComponentError(componentArray);
String error = StringUtils.join(errorList, "\n");
return error;
}
public static String showComponentErrors(
final Collection<?> componentCollection) {
AbstractComponent[] componentArray = componentCollection
.toArray(new AbstractComponent[] {});
return ErrorUtils.showComponentErrors(componentArray);
}
}
Thanks guys,
If you have any issue please don't hesitate to contact me. I am always happy to help....
head titanium ti s6
ReplyDeleteHead Titanium Titanium Ti. Features. titanium joes 3G audio apple watch titanium vs aluminum technology. titanium jewelry piercing Excellent flexibility and sensitivity of composite materials. The design is perfect for many titanium chainmail types polished titanium of
lfjco881 Stockx outlet,stockxireland,stockx ireland m1otidk596
ReplyDelete