PDA

View Full Version : TextField value problem



hossaindoula
1 Feb 2010, 10:44 AM
I have a form which contains several TextFields. Whenever I want to get value from the fields it returns null only when it is not inside any kind of EventHandler... But if I want to get the value from any kind of Action Event Handler... Then I can get the value... Just Have a Look at the example...

private void createFormComponents() {
ContentPanel cp = new ContentPanel();
cp.setHeading("INFO");
cp.setFrame(true);
cp.setSize(650,750);
cp.setLayout(new RowLayout(Orientation.HORIZONTAL));

String phone = "";

final FormPanel panel = new FormPanel();
panel.setHeaderVisible(false);
panel.setEncoding(FormPanel.Encoding.MULTIPART);
panel.setMethod(Method.POST);
FormLayout layout = new FormLayout();

FieldSet fieldSetCap = new FieldSet();
final TextField<String> telephone = new TextField<String>();
telephone.setFieldLabel("Telephone");
fieldSetCap.add(telephone);
panel.add(fieldSetCap);

Button crBtn = new Button("Add");
panel.addButton(crBtn);
FormButtonBinding binding = new FormButtonBinding(panel);
binding.addButton(crBtn);
panel.setButtonAlign(HorizontalAlignment.RIGHT);
cp.add(panel);

phone = telephone.getValue(); // phone return the ""


crBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
phone = telephone.getValue(); // phone return the typed value
});
}


What could be the problem??? :-? :-?:-?

Arno.Nyhm
3 Feb 2010, 9:45 AM
What could be the problem??? :-? :-?:-?

there are no problems


phone = telephone.getValue(); // phone return the ""


thats ok that it is null at this place. because at this point it is not rendered and nobody has entered some text.




crBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
phone = telephone.getValue(); // phone return the typed value
});
}


at this point someone has entered the text and after this you read the value.


if you make breakpoints in your code you can see when this lines are executed.

hossaindoula
3 Feb 2010, 10:01 AM
OK I understand what you explained... But the problem is I am trying to validate Confirm Password field... Whenever user types text that doesn't match with the password field, it should validate... So, in that case how can I retrieve the value after typing the password and going to confirm passwod field... Because there is no event except the onblur... :-? Here is the thing I was trying to do...


final TextField<String> password = new TextField<String>();
password.setPassword(true);
password.setFieldLabel("Password");
password.setAllowBlank(false);
fieldSetCap.add(password);

final TextField<String> passwordCnf = new TextField<String>();
passwordCnf.setPassword(true);
passwordCnf.setFieldLabel("Confirm Password");
passwordCnf.setAllowBlank(false);
fieldSetCap.add(passwordCnf);
//String valPwd = password.toString();
passwordCnf.setValidator(new PasswordConfirmValidate()); // How can I retrieve the password value because here "password.getValue()" will not work...:-/
panel.add(fieldSetCap);

Arno.Nyhm
3 Feb 2010, 10:24 AM
how looks your PasswordConfirmValidate()?

if you search the forum for setValidator you can find many examples how to write a validator...

hossaindoula
3 Feb 2010, 10:34 AM
I know how to write validator... But the problem is how I can get the other field value when I validate a field... Because based upon that other field value I am validating... Here is the code... Have a look...



public class PasswordConfirmValidate implements Validator{
String valPwd = "";

public PasswordConfirmValidate(){
valPwd = ???? // Here I have to get the "password" field's value
}
public String validate(Field<?> field, String value){
String res = null;
if(!valPwd .equals(value)){
res = " Password does not match " ;
}
return res;
}


:)

Arno.Nyhm
4 Feb 2010, 1:49 AM
if you search the forum for setValidator you can find many examples how to write a validator...

here was the same question - validation of 2 Fields (but with version 1.0)
http://www.extjs.com/forum/showthread.php?p=203377#post203377



valPwd = ???? // Here I have to get the "password" field's value


why you try to access the value in the constructor?

there are 2 ways



public class PasswordConfirmValidate implements Validator{

public PasswordConfirmValidate(){
}
public String validate(Field<?> field, String value){
String res = null;
String valPwd = mySecondField.getValue(); // your mySecondField need be final
if(!valPwd .equals(value)){
res = " Password does not match " ;
}
return res;
}
but better make it more generic:



public class PasswordConfirmValidate implements Validator{
TextField<String> passwordCnf;

public PasswordConfirmValidate(TextField<String> passwordCnf){
this.passwordCnf = passwordCnf;

}
public String validate(Field<?> field, String value){
String res = null;
if(!passwordCnf.getValue().equals(value)){
res = " Password does not match " ;
}
return res;
}
in this case we only store a reference to the second field and get a "fresh" value for compare at the validation time.

hossaindoula
4 Feb 2010, 3:29 AM
public class PasswordConfirmValidate implements Validator{

public PasswordConfirmValidate(){
}
public String validate(Field<?> field, String value){
String res = null;
String valPwd = mySecondField.getValue(); // Always it is getting null
if(!valPwd .equals(value)){
res = " Password does not match " ;
}
return res;
}







public class PasswordConfirmValidate implements Validator{
TextField<String> passwordCnf;
public PasswordConfirmValidate(TextField<String> passwordCnf){
this.passwordCnf = passwordCnf; // Here also the value is getting null
}
public String validate(Field<?> field, String value){
String res = null;
if(!passwordCnf.getValue().equals(value)){
res = " Password does not match " ;
}
return res;
}



Look I have done same as you have instructed, have a look at my code:





public class PasswordConfirmValidate implements Validator{
String valPwd = "";
public PasswordConfirmValidate(TextField<String> pwdFld){
this.valPwd = pwdFld.getValue();
}
public String validate(Field<?> field, String value){
String res = null;
if(!valPwd.equals(value)){
res = valPwd + " Password does not match " + value;
}
return res;
}
}


=====================the Form ======================
final TextField<String> password = new TextField<String>();
password.setPassword(true);
password.setFieldLabel("Password");
password.setAllowBlank(false);

final TextField<String> passwordCnf = new TextField<String>();
passwordCnf.setPassword(true);
passwordCnf.setFieldLabel("Confirm Password");
passwordCnf.setAllowBlank(false);

passwordCnf.setValidator(new PasswordConfirmValidate(password)); // the first value is "password" fields value and I am validating on the "passwordCnf" value...

Arno.Nyhm
4 Feb 2010, 8:17 AM
public PasswordConfirmValidate(TextField<String> passwordCnf){
this.passwordCnf = passwordCnf; // Here also the value is getting null
}
yes... at this point the value should be null because it is in the CONSTRUCTOR... and there no user can input values at this moment because it is not rendered at this time.


i think you look at the wrong places when you debug.


but maybe you miss this point: you need to use setAutoValidate(true) to see the error while the user is typing. otherwise the user see the error only if he leave the TextField.


here is my full TestCase with my second validator and it works fine for me (pls post the next time a full example which have all elements in one file to avoid other side effects from other parts you dont copy into your codesnippet):

PS: in my example it only updates if you type in something in the second textbox. you need to add also something to trigger the validation if you enter something in the first textbox.



package com.mycompany.myapplication.client.forumsnippets;

import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Method;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.core.client.EntryPoint;

/**
* http://www.extjs.com/forum/showthread.php?p=433866#post433866
* @author anonym
*/
public class ForumThread433866PasswordValidator implements EntryPoint {

public void onModuleLoad() {
Window window = new Window();
window.setSize(600, 600);
window.add(createFormComponents());
window.show();
}

private ContentPanel createFormComponents() {
ContentPanel cp = new ContentPanel();
cp.setHeading("INFO");
cp.setFrame(true);
cp.setSize(650, 750);
cp.setLayout(new RowLayout(Orientation.HORIZONTAL));


final FormPanel panel = new FormPanel();
panel.setHeaderVisible(false);
panel.setEncoding(FormPanel.Encoding.MULTIPART);
panel.setMethod(Method.POST);
cp.add(panel);

final TextField<String> password = new TextField<String>();
password.setPassword(true);
password.setFieldLabel("Password");
password.setAllowBlank(false);
panel.add(password);

final TextField<String> passwordCnf = new TextField<String>();
passwordCnf.setPassword(true);
passwordCnf.setFieldLabel("Confirm Password");
passwordCnf.setAllowBlank(false);
passwordCnf.setAutoValidate(true);
panel.add(passwordCnf);

passwordCnf.setValidator(new PasswordConfirmValidate(password));

return cp;
}

public class PasswordConfirmValidate implements Validator {

TextField<String> passwordCnf;

public PasswordConfirmValidate(TextField<String> passwordCnf) {
this.passwordCnf = passwordCnf;

}

public String validate(Field<?> field, String value) {
String res = null;
if (!passwordCnf.getValue().equals(value)) {
res = " Password does not match ";
}
return res;
}
}
}

Arno.Nyhm
4 Feb 2010, 8:27 AM
PS: in my example it only updates if you type in something in the second textbox. you need to add also something to trigger the validation if you enter something in the first textbox.


if you add this snippet, then it shows for the second textfield also the validation if you change the values in the first textfield:



// new start
password.setValidator(new Validator() {

public String validate(Field<?> field, String value) {
if (passwordCnf.getValue() != null) {
passwordCnf.isValid();
}
return null;

}
});
// new end
passwordCnf.setValidator(new PasswordConfirmValidate(password));

hossaindoula
4 Feb 2010, 10:18 AM
It worked... Actually the problem was in initialization of the value in the constructor...:)