PDA

View Full Version : Form custom validation, help needed!



immu2k1
3 Dec 2007, 12:00 PM
Hi All,

I'm basically creating a form with 3 fields. I dont' want to allow a blank value in my "namePrimary" field IF there is a value in a subsequent field. This is why I'm using my own validation being triggered on the submit button.

The problem I'm having is that when I specifically markInvalid my namePrimary field, and the error message is shown correctly as expected, namePrimary.isValid() strangely continues to return true which it certainly shouldn't since I marked it Invalid (whew, breather).

Below are the relevant snippets:



namePrimary = new Ext.form.TextField({
fieldLabel: 'Name/Group',
name: 'namePrimary',
allowBlank: true,
msgTarget: 'under'
})


Now, I have a button which onSubmit does this:



form.addButton('Update', function()
{
validateSupportForm();

// this should be false since I just called markInvalid
// in the validateSupportForm() function! However, isValid returns true.
// Visually, the error message and error icon definitely appears next to the field.
// But.....isValid passes it
if(namePrimary.isValid())
{
Ext.MessageBox.alert('NamePrimary', 'field is valid');
}
});

var validateSupportForm = function()
{
form.markInvalid([{id: 'namePrimary', msg: 'hardcoded invalid'}]);
}


Added some comments in the area of focus.

Any help would be GREATLY appreciated

Thanks!

hendricd
3 Dec 2007, 12:06 PM
Tried:



namePrimary = new Ext.form.TextField({
fieldLabel: 'Name/Group',
name: 'namePrimary',
allowBlank: true,
msgTarget: 'under',
validator:function(value){
return yourConditionTesting(value) || 'hardcoded invalid' ; //true or bad message
}
})

immu2k1
3 Dec 2007, 12:13 PM
Tried that earlier, but it looks like the in-line validator attribute function only gets triggered when a value is typed in the field. That function doesn't get called on blank fields.

I figure I would have much greater flexibility in calling my own validator function from the submit action.

hendricd
3 Dec 2007, 12:42 PM
Yeah, but isValid calls validateValue, which calls the validator. It's skipping blank fields because you have allowBlank:true.

Why not use form.isValid();

or roll your own:




var msg, valid = true;
form.items.each(function(field){
msg = field.isValid(true);
if(msg !== true ){ //true to suppress invalidMarking
alert(field.fieldLabel + ' is invalid:' + msg );
valid = false;
}
});
return valid;

immu2k1
3 Dec 2007, 1:17 PM
Thanks for the reply Doug. Sorry, I may not have explained my situation too clearly. Basically, I want to allow blanks - ONLY - if there is a value in another one of my fields. So field "namePrimary" can be blank, only if another field has value - otherwise, "namePrimary" should be marked as Invalid.

So if I change "allowBlank: false" it doesn't give me the flexibility to check values in the other field. If I set "allowBlank: true", the in-line validator function will never get called (validateValue function's first condition gets met and this.validator never gets called).

So this is why I do a forceful markInvalid on the field, but it looks like this only manipulates the dom. Calling isValid() never does check for fields directly called with markInvalid. I guess my workaround may be to maintain my own "isValid" with a list of fields I've called markInvalid on.




Yeah, but isValid calls validateValue, which calls the validator. It's skipping blank fields because you have allowBlank:true.

Why not use form.isValid();

or roll your own:




var msg, valid = true;
form.items.each(function(field){
if(!msg = field.isValid(true)){ //true to suppress invalidMarking
alert(field.fieldLabel + ' is invalid:' + msg );
valid = false;
}
});
return valid;

hendricd
3 Dec 2007, 1:26 PM
So,:



otherfield.on('change',function(field, newVal,oldVal){
namePrimary.allowBlank = !!field.newVal.length;
}



then default validation engine works..

immu2k1
3 Dec 2007, 2:49 PM
Doug - you are the man! Works like a charm. I just made the below minor changes:

namePrimary.allowBlank = !!field.getValue().length;
namePrimary.validate();

Thanks a lot!