Search code examples
extjsextjs4

How to submit a form when validation fails


I need to submit a form which contains some fields that may be invalid. For example, there may be some fields that have attribute allowBlank set to false. I tried to do this before submitting:

myform.getForm().getFields().each(function(field){
   if(field.allowBlank==false) field.allowBlank=true; // <- has no effect
});

myform.getForm().submit({ .... // <- still not submitted

But it has no effect, the form is not submitted, even though myform.getForm().isValid() returns true.

EDIT

To prevent voting down I want to clarify. I know, that this is normal behaviour, but I need a workaround for some special functionality. I submit the form not to insert values into the database, but to check current values in the form's fields. And in some case, depending on the values, I want to hide some fields. This is necessary when I have a parent object with some child objects that share their parent's fields. But these child objects may differ from one another. So, when a field object type has a value child one, I want to hide certain fields that pertain to child two. But to check this, I need to submit the form, and this is where the problem comes out.


Solution

  • You can take advantage of Vtypes in Ext which I think will solve your problem.

    Ext.apply(Ext.form.field.VTypes, {
        firstName: function(val, field) {
            // Use a condition if you need to, otherwise just return true value
            if (val == '123') {
                return true;   
            }
    
            return false;
        }
    });
    
    Ext.create('Ext.form.Panel', {
        title: 'Sample Form'
        ,width: 300
        ,bodyPadding: 10
        ,renderTo: Ext.getBody()
        ,items: [{
            xtype: 'textfield'
            ,name: 'first name'
            ,fieldLabel: 'First Name'
            ,vtype: 'firstName' // <-- vtype here
            ,allowBlank: false
        }]
    });
    

    Even if you have allowBlank: false and you decided to return true value in your vtype method, you will still have a valid field that will allow you to submit your form.