PDA

View Full Version : Form Validation marks the fields Invalid at loading



shaab01
20 Nov 2009, 4:31 AM
I have a form with couples of TextFields with setAllowBlank(false) . Now when this form is loaded it marks these TextFileds Invalid, Which I do not want as it looks bad. Idealy when the user clicks at these fields an leave them blank, then only the fields should be marked "Invalid" and this is the Expected behavior. I get this expected behavior but the problem is when I have Field binding on these fields. Without binding, it works fine, but with binding, the fields are initialized with the Model object and fields are marked invalid.

Can any one help me to make sure the fields are not marked invalid at loading, even if I use Bindings on the fields.

Here is code :



import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.EntryPoint;


import com.google.gwt.user.client.ui.RootPanel;

/**
*
* @author shaab01
*/
public class MainEntryPoint implements EntryPoint {
private BaseModel computerData;

/** Creates a new instance of MainEntryPoint */
public MainEntryPoint() {
}

/**
* The entry point method, called automatically by loading a module
* that declares an implementing class as an entry-point
*/
public void onModuleLoad() {

final ContentPanel cp = new ContentPanel();
cp.setSize("500", "500");
computerData = new BaseModel();
cp.add(createNetworkIdentificationForm());

cp.layout();
RootPanel.get().add(cp);
}


private FormPanel createNetworkIdentificationForm() {

FormData formData = new FormData("");
FormPanel form2 = new FormPanel();


FormBinding binding = new FormBinding(form2);
binding.bind(computerData);

form2.setHeaderVisible(false);
form2.setLayout(new FormLayout());

FormLayout layout = new FormLayout();
layout.setLabelWidth(120);

TextField<String> hostName = new TextField<String>();
hostName.setName("HostName");
hostName.setFieldLabel("Host Name*");
hostName.setAllowBlank(false);

TextField<String> macAddress = new TextField<String>();
macAddress.setName("macAddress");
macAddress.setFieldLabel("Mac Address*");
macAddress.setAllowBlank(false);

form2.add(hostName, formData);
form2.add(macAddress, formData);


// manually add bindings
binding.addFieldBinding(new FieldBinding(hostName, "hostName"));
binding.addFieldBinding(new FieldBinding(macAddress, "macAddress"));



hostName.clearInvalid();
//hostName.setValue(null);

return form2;
}



}

sven
20 Nov 2009, 4:32 AM
Add a Bind eventlistener to the formbinding and call clearInvalid on all fields.

FireGlow
3 Feb 2010, 8:24 AM
I have something like this:

for ( FieldBinding fieldBinding : binding.getBindings() ) {
DeferredCommand.addCommand( new ClearInvalidDeferredCommand( fieldBinding ) );
}

But that is really ugly and the user sees for a short time, that something is going on. Is there any new progress in that issue? Any possibility to have a valid form after creating it?

For my case I add my form into a Window, and at the beginning all fields are invalid.

foxcet
18 Mar 2010, 11:55 PM
Add a Bind eventlistener to the formbinding and call clearInvalid on all fields.

Can you give the example, i get confused on how add listener, how can i know what kind of listener and eventype?
Which event type? and wich listener, i always get confused with this
bindingForm.addListener(EventType.?????, Listener..????)

Help will be aprecciate. Thanks!!

shaab01
19 Mar 2010, 1:01 AM
This works for me.

binding.addListener(Events.Bind, new Listener<BindingEvent>() {
public void handleEvent(BindingEvent be) {
hostName.clearInvalid();
networkAddress.clearInvalid();
macAddress.clearInvalid();
}
});

edcon
21 Mar 2010, 1:22 AM
thanks