Reality is a perception. Perceptions are not always based on facts, and are strongly influenced by illusions. Inquisitiveness is hence indispensable

Friday, March 18, 2011

GXT Nuggets: Preventive form validation

Bananas for the code monkey


It is always a good idea to prevent users from doing unwarranted things. Thats the whole idea of client side validation. (Client-side validation is one good way of catching errors before the code hits server, so....blah blah).

The general notion in UI is to accept user input using forms.(XSS, URL mangling etc left aside). In GXT, we can actually mandate/direct the hasty users to enter the correct data without cluttering the UI with error messages. The notion is simple, prevent the buttons from being enabled until all the validations are honored.

This is achieved using a FormButtonBinding object. The actual code:
private FormPanel createLabelSearchPanel(String formHeader, String keywordLabel, String submitLabel) {
FormPanel formPanel = new FormPanel();

formPanel.setLabelAlign(FormPanel.LabelAlign.RIGHT);
formPanel.setHeading(formHeader);
formPanel.setFieldWidth(DashboardConstants.FIELD_WIDTH);
formPanel.setLabelWidth(DashboardConstants.LABEL_WIDTH);
formPanel.setFrame(true);
formPanel.setAutoHeight(true);
FormData formData = new FormData("-10");

TextField<BaseModelData> textField = TextFieldFactory.getDefault().getTextField(keywordLabel, "(Enter Search Key)", "");
formPanel.add(textField, formData);

Button searchButton = new Button(submitLabel, new SubmitButtonListener(formPanel));
formPanel.getButtonBar().add(searchButton);

FormButtonBinding binding = new FormButtonBinding(formPanel);
binding.addButton(searchButton);
formPanel.setButtonAlign(HorizontalAlignment.CENTER);

return formPanel;
}
------------------------------------------

private TextField<BaseModelData> createTextField(final String fieldLabel, final String defaultText, final String defaultValue) {
final TextField<BaseModelData> textField = new TextField<BaseModelData>();
textField.setFieldLabel(fieldLabel);
textField.setEmptyText(defaultText);
textField.setSelectOnFocus(true);
textField.setAllowBlank(true);
return textField;
}

No comments:

Popular Posts

Labels

About Me

Well for a start, I dont' want to!. Yes I am reclusive, no I am not secretive; Candid? Yes; Aspergers? No :). My friends call me an enthusiast, my boss calls me purist, I call myself an explorer, to summarise; just an inquisitive child who didnt'learn to take things for granted. For the sake of living, I work as a S/W engineer. If you dont' know what it means, turn back right now.