PDA

View Full Version : Proper AJAX field validation?



IvanJ
12 Nov 2009, 12:59 PM
It seems I got this setup right, but the only problem is - when I call markInvalid() on some field, and later call form.isValid() - the form remains valid. This is my code:



// Additional field verification
Ext.apply(Ext.form.VTypes,{
password: function(val,field)
{
ValidatePassword(); // async
return true;
},
....}

function ValidatePassword()
{
PageMethods.ValidatePassword(Ext.getCmp("password").getValue(),ValidatePassword_CallBack);
Ext.getCmp("passwordConfirm").validate();
}

function ValidatePassword_CallBack(result)
{
if ( !result )
{
Ext.getCmp("password").markInvalid("Password does not match complexity rules!");
Ext.getCmp("password").isValid = false;
}
}


The field gets properly marked invalid, but form.isValid still says it's valid...

Any idea? What's the proper way of doing this?

IvanJ
12 Nov 2009, 1:06 PM
Hmm, judging by the debugger, whenever I call getForm().isValid(), it calls validate() on the field, which fires the ValidatePassword, which returns true.

I tried doing this:




password: function(val,field)
{
ValidatePassword(); // async
return field.isValid;
},


... but it still doesn't work... I guess I'll try a trigger variable.

IvanJ
12 Nov 2009, 1:23 PM
Appears isValid() is a function, not a variable, and when I fix my previous code it results in an infinite recursive loop (calling isValid() results in form validation).

IvanJ
12 Nov 2009, 1:32 PM
I'm all out of ideas. Other than using an intermediate variable to signify the "currentlyValid?" and then returning that after making AJAX call, I can't think of anything else... Is there really now way to check if the field is valid without causing it to validate itself?