PDA

View Full Version : Problem with vtype validation + AJAX



ang89
24 Aug 2009, 2:13 AM
Hi there, I want to call an AJAX request inside a VType validation function. Basically, I want to check whether the given input already exists in database. If it does, the field will be marked as invalid.

The code look like this:
Ext.apply(Ext.form.VTypes,
{
// Check if the given nomor_sj already exists in database
nomor_sj: function (value, field)
{
var validator = this;

Ext.Ajax.request(
{
url: SITE_URL + '/inventory/json_check_nomor_sj/' + value,
method: 'post',
scope: validator,
callback: function(options, success, response)
{
if (! success)
{
// Connection failure, just mark the field as valid.
// It will be double-checked in the server anyway.
return true;
}

var status = Ext.util.JSON.decode(response.responseText);
return (! status.exists);
}
});
},

nomor_sjText: 'Nomor SJ sudah terdaftar di database.'
});

But the field keep marked as invalid, even if status.exists is false.

And then I found out that it is because the nomor_sj function already returned (with no return value) even before the AJAX request is finished. Is there any way to wait for the AJAX call, or any better solution for my problem here?

Animal
24 Aug 2009, 2:37 AM
Quick job interview type question for web developers:

What does the "A" of Ajax stand for?

ang89
24 Aug 2009, 2:48 AM
Quick job interview type question for web developers:

What does the "A" of Ajax stand for?
Okay, okay I got it. . So waiting for AJAX call is not an option. Any suggestion for my problem here?

Animal
24 Aug 2009, 2:53 AM
You will have to programmatically mark the field as invalid or valid when the response returns.

ang89
24 Aug 2009, 3:08 AM
You will have to programmatically mark the field as invalid or valid when the response returns.
Yeah that's it. Thanks !

quii
21 Sep 2009, 1:03 AM
Hello, I am tackling a very similar problem to this

I cant seem to find in the API how to set a TextField to be valid.

Any help would be appreciated

Animal
21 Sep 2009, 3:04 AM
call clearInvalid to clear it's displayed status.

But if it's values IS actually invalid in that it does not pass validations that YOU have configured for it, then you can't "set" it to be valid. (Unless you make those validations accessible susceptible to programmatic firkling)

robw
21 Sep 2009, 3:30 AM
I think it's better to assume the field is valid by returning true from your vtype function. Then if the async validation fails call field.markInvalid().

You could also show a loading indicator or something while waiting for the async response to let the user know the validation is in progress.

quii
21 Sep 2009, 3:31 AM
Yeah I think I prefer Rob's approach here, thanks.