PDA

View Full Version : How to validate a text field remotely?



kkven
17 Aug 2007, 8:55 PM
Hi all,
I am a newbie kinda, and i have been scratching my head to fix this problem for the last three hours. i would greatly appreciate a response. I have a text field that i need to validate through server. I am struggling to pass the response from Data.connection request back to the validator. Any help would be greatly appreciated.
Here is the code:



form.add(
new Ext.form.TextField({
fieldLabel: 'Email',
validator: validateEmail,
name: 'email',
width:120,
vtype: 'email',
allowBlank:false
}),
);
function validateEmail(value){
var conn = new Ext.data.Connection();
return conn.request({
url : 'q_validate_user.php',
params : { e : escape(value) },
method: 'GET',
scope: this,
callback: function(opts, success, response) {
if (!success) {
Ext.MessageBox.alert("Error",
success ? response.responseText :
"Error saving data - try again");
return;
}
// validRes = response.responseText;
return response.responseText;
}
});
}

I try to grab the response and pass it to the validator but for some reason i can't make it.

Thanks for your help in advance,

Kkven

ToNiC
20 Aug 2007, 2:59 PM
At first glance, the validateEmail function is returning a number while it's supposed to return a boolean as per the API.

The Ext.Form.form class already has methods that implement what your trying to achieve. Take a look at the API, specifically the submit(...) and actioncomplete(...) methods.

Also take a look at server return formats, http://extjs.com/learn/Manual:Forms:Result_Format


Hope that helps

Thylia
29 Aug 2007, 7:07 AM
bump

I have same problem

Thylia
29 Aug 2007, 7:38 AM
[CODE]
...

field = new Ext.form.TextField({
fieldLabel:record.data['LabCha'],
name:record.data['CodCha'],
width:record.data['widthfield'],
validator:TestExist,
validationEvent:"blur",
maxLengthText:record.data['Lenght']
});

...

function TestExist(Val){
this.clearInvalid();
var record = Exemple.SaiGriStore.getById(this.name);

var obj = {
result : null,
data : record.data,
val: Val,
field: this
}

Ext.Ajax.request({
url:'get-tstexist.php',
params:{
'Tab':obj.data['TabTst'],
'Cod':obj.data['CodCha'],
'Val':obj.val
},
method:'GET',
scope:obj,
callback:function(options, success, response){
return obj.result;
},
success:function(response, options){
try{
var responseArray = Ext.util.JSON.decode(response.responseText);
if ( !responseArray.success ) {
obj.field.invalidText = responseArray.errors;
obj.result = false;
}
else
obj.result = true;
}catch(e){
}
},
failure:function(){
Ext.MessageBox.alert("TestExist", "Le serveur ne r