-
5 Mar 2009 6:50 AM #1
Question about form binding and validating
Question about form binding and validating
Hi, I've got two questions:
1. Is it possible to run an event after a model has been bound to the form? I want to do a selectAll() on the first textfield in the form; this will need to occur after the form has been bound to the model so the form contains the text to be selected. The binding happens asynchronously (using DeferredCommand) so I can't just place selectAll call after the bind call in the code.
2. Is it possible to (temporarily) disable validation on a form, so the form does not immediately validate after binding? To let the user enter a new employee (instead of an existing one), I bind the form to an BeanModel with an empty employee bean. But then the form immediately validates and most of the fields are invalid (because they are empty). I want the validation to happen after the user clicks the "save" button for the first time.
Thanks,
Jaap.
-
11 Mar 2009 6:53 AM #2
I had the same problem with the validation of an empty model.
I just cleared the invalid-state of the fields after the binding. But you have to do it in a DeferredCommand, because the setting of the validation-message is also done in such a command.
Code:
binding.bind(model);
DeferredCommand.addCommand(new Command() {
field.clearInvalid();
});
-
12 Mar 2009 1:59 AM #3
I also had some problems with deferred binding. In next release, Ext team will fire an event when bind is over : http://extjs.com/forum/showthread.ph...746#post293746
-
12 Mar 2009 2:07 AM #4
We 'improved' our solution for the unsetting of the error-messages.
We extended the FieldMapping by overwriting the bind()-Method. After the super-call to bind(), we can easily get the field and invalid all the errors.
public void bind(ModelData model) {
super.bind(model);
getField().clearInvalid();
}
So, you don't have to be aware on doing everything in DeferredCommands. It will just be done in the same command like the binding.
-
12 Mar 2009 2:34 AM #5
I've overriden FormBinding and add an immediateBind method, but I would not recommend this solution because each time you upgrade Ext GWT, you'll have to check consistency.
Code:protected static class InternalFormBinding extends FormBinding { public InternalFormBinding(final FormPanel panel, final boolean autoBind) { super(panel, autoBind); } /** * Should be the same than {@link FormBinding#bind(ModelData)} but do not use a DeferredCommand */ public void immediateBind(final ModelData model) { if (this.model != null) { unbind(); } this.model = model; for (FieldBinding binding : bindings.values()) { binding.bind(model); } for (FieldBinding b : getBindings()) { b.setStore(store); } } }
-
18 Mar 2009 3:07 AM #6
Thanks for the replies. For now, I've overriden the FormBinding class. I realize the disadvantages of that approach, but it was the easiest way of both clearing the invalid status and getting the first text field to have focus after the binding.
Code:public class InternalFormBinding extends FormBinding { public InternalFormBinding(FormPanel panel) { super(panel); } public InternalFormBinding(FormPanel panel, boolean autoBind) { super(panel, autoBind); } @Override public void bind(final ModelData modelData) { DeferredCommand.addCommand(new Command() { @SuppressWarnings("unchecked") public void execute() { if (model != null) { unbind(); } model = modelData; for (FieldBinding binding : bindings.values()) { binding.bind(modelData); binding.getField().clearInvalid(); } for (Field field : panel.getFields()) { if (field instanceof TextField) { field.focus(); ((TextField) field).selectAll(); break; } } } }); for (FieldBinding b : getBindings()) { b.setStore(store); } } }


Reply With Quote