Created
May 8, 2014 20:18
-
-
Save branflake2267/9a6ce00004c0fe2365ce to your computer and use it in GitHub Desktop.
Validating a group of fields
This file contains hidden or 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
| import com.google.gwt.event.shared.EventHandler; | |
| import com.google.gwt.event.shared.GwtEvent; | |
| import com.google.gwt.event.shared.HasHandlers; | |
| import com.google.web.bindery.event.shared.HandlerRegistration; | |
| public class ValidChangeEvent extends GwtEvent<ValidChangeEvent.ChangeHandler> { | |
| private boolean valid; | |
| public ValidChangeEvent(boolean valid) { | |
| this.valid = valid; | |
| } | |
| public static void fire(HasHandlers source, boolean valid) { | |
| ValidChangeEvent eventInstance = new ValidChangeEvent(valid); | |
| source.fireEvent(eventInstance); | |
| } | |
| public static void fire(HasHandlers source, ValidChangeEvent eventInstance) { | |
| source.fireEvent(eventInstance); | |
| } | |
| public interface HasChangeHandlers extends HasHandlers { | |
| HandlerRegistration addChangeHandler(ChangeHandler handler); | |
| } | |
| public interface ChangeHandler extends EventHandler { | |
| public void onChange(ValidChangeEvent event); | |
| } | |
| private static final Type<ChangeHandler> TYPE = new Type<ChangeHandler>(); | |
| public static Type<ChangeHandler> getType() { | |
| return TYPE; | |
| } | |
| @Override | |
| public Type<ChangeHandler> getAssociatedType() { | |
| return TYPE; | |
| } | |
| @Override | |
| protected void dispatch(ChangeHandler handler) { | |
| handler.onChange(this); | |
| } | |
| public boolean isValid() { | |
| return valid; | |
| } | |
| } |
This file contains hidden or 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
| import com.google.gwt.user.client.ui.RootPanel; | |
| import com.sencha.gxt.test.client.validators.ValidChangeEvent.ChangeHandler; | |
| import com.sencha.gxt.widget.core.client.button.TextButton; | |
| import com.sencha.gxt.widget.core.client.container.HBoxLayoutContainer; | |
| import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer; | |
| import com.sencha.gxt.widget.core.client.form.TextArea; | |
| import com.sencha.gxt.widget.core.client.form.TextField; | |
| public class ValidGroupTest { | |
| public ValidGroupTest() { | |
| TextField name = new TextField(); | |
| TextArea description = new TextArea(); | |
| // add some default valudatiors | |
| name.setAllowBlank(false); | |
| //name.addValidator(validator); // add custome validators | |
| description.setAllowBlank(false); | |
| //description.addValidator(validator); // add custom validators | |
| name.setAutoValidate(true); | |
| description.setAutoValidate(true); | |
| final TextButton save = new TextButton("Save"); | |
| save.setEnabled(false); | |
| HBoxLayoutContainer controls = new HBoxLayoutContainer(); | |
| controls.add(save); | |
| VerticalLayoutContainer vlc = new VerticalLayoutContainer(); | |
| vlc.add(name); | |
| vlc.add(description); | |
| vlc.add(controls); | |
| final ValidHandlingGroup validators = new ValidHandlingGroup(); | |
| validators.add(name, "Fill out the name"); | |
| validators.add(description, "Fill out the description"); | |
| validators.addValidChangeHandler(new ChangeHandler() { | |
| @Override | |
| public void onChange(ValidChangeEvent event) { | |
| if (validators.isValid()) { | |
| save.setEnabled(true); | |
| } else { | |
| save.setEnabled(false); | |
| } | |
| } | |
| }); | |
| RootPanel.get().add(vlc, 100, 100); | |
| } | |
| } |
This file contains hidden or 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
| import java.util.ArrayList; | |
| import java.util.List; | |
| import com.google.gwt.event.shared.EventBus; | |
| import com.google.gwt.event.shared.HandlerRegistration; | |
| import com.google.gwt.event.shared.SimpleEventBus; | |
| import com.sencha.gxt.widget.core.client.event.InvalidEvent; | |
| import com.sencha.gxt.widget.core.client.event.InvalidEvent.InvalidHandler; | |
| import com.sencha.gxt.widget.core.client.event.ValidEvent; | |
| import com.sencha.gxt.widget.core.client.event.ValidEvent.ValidHandler; | |
| import com.sencha.gxt.widget.core.client.form.Field; | |
| public class ValidHandlingGroup { | |
| private class Valid { | |
| private boolean valid; | |
| private String message; | |
| public Valid(final EventBus eventBus, Field<?> field, String message) { | |
| field.addValidHandler(new ValidHandler() { | |
| @Override | |
| public void onValid(ValidEvent event) { | |
| valid = true; | |
| eventBus.fireEvent(new ValidChangeEvent(valid)); | |
| } | |
| }); | |
| field.addInvalidHandler(new InvalidHandler() { | |
| @Override | |
| public void onInvalid(InvalidEvent event) { | |
| valid = false; | |
| eventBus.fireEvent(new ValidChangeEvent(valid)); | |
| } | |
| }); | |
| } | |
| public boolean isValid() { | |
| return valid; | |
| } | |
| public String getMessage() { | |
| return message; | |
| } | |
| } | |
| private EventBus eventBus; | |
| private List<Valid> valids; | |
| public void add(Field<?> field, String errorMessage) { | |
| if (valids == null) { | |
| valids = new ArrayList<Valid>(); | |
| eventBus = new SimpleEventBus(); | |
| } | |
| Valid valid = new Valid(eventBus, field, errorMessage); | |
| valids.add(valid); | |
| } | |
| public boolean isValid() { | |
| if (valids == null) { | |
| return false; | |
| } | |
| for (Valid valid : valids) { | |
| if (!valid.isValid()) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| public List<String> getErrors() { | |
| if (valids == null) { | |
| return null; | |
| } | |
| List<String> errors = new ArrayList<String>(); | |
| for (Valid valid : valids) { | |
| if (!valid.isValid()) { | |
| errors.add(valid.getMessage()); | |
| } | |
| } | |
| return errors; | |
| } | |
| public HandlerRegistration addValidChangeHandler(ValidChangeEvent.ChangeHandler handler) { | |
| return eventBus.addHandler(ValidChangeEvent.getType(), handler); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment