I have this form in ExtJs. If field1 is not empty, field2 must not be empty. But it is not working even though the listener is firing.
{
xtype: 'panel',
title: 'title 1',
items: [{
xtype: 'fieldset',
title: 'field A',
items: [{
xtype: 'textfield',
fieldLabel: 'Line 1',
id: 'field1',
listeners: {
change: function(f, new_val) {
if (new_val) {
//alert("change" + new_val);
f.field2.allowBlank = false;
} else {
f.field1.allowBlank = true;
}
}
}
code for field2
}]
}]
}
Form validation is a “technical process where a web-form checks if the information provided by a user is correct.” The form will either alert the user that they messed up and need to fix something to proceed, or the form will be validated and the user will be able to continue with their registration process.
Ext. create('Ext. form. Panel', { title: 'Simple Form', bodyPadding: 5, width: 350, // The form will submit an AJAX request to this URL when submitted url: 'save-form.
new BoxComponent({ qtip: "This is a tip", listeners: { rendered: function(c){ Ext. QuickTips.
Partial answer:
The reason your code doesn't work is because allowBlank is a configuration value -- it only has an effect at the time the component is created. I see this mistake all the time -- when browsing the API docs, you need to keep in mind that all those configuration options are only potent at object creation -- they are not public properties that you can set to modify behavior of an existing object.
That said, I'm surprised that there's no setAllowBlank() method in the API. I'll continue to look around and amend this answer if I'm able to find a way to do it.
EDIT: It appears your method should work, from my reading of the TextField source code. So the question is why isn't it.
EDIT2: You're probably failing to get a reference to your TextFields. f.field1
is probably not pointing at what you think it is. You can solve this by adding a ref:'field1'
to your TextField config. Then your FormPanel will have a property called field1 you can reference.
You can use the markInvalid function. For example:
if (new_val) {
if(f.field2.getValue() == ''){
f.field2.markInvalid('Cannot be empty');
}
} else {
// clears invalid flags on the field.
f.field1.clearInvalid();
}
Not saying this is the exact code to use but you can see from this how to mark invalid and clear invalid messages. There is also the capability of creating your own validators though I am not sure that would completely fit your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With