DEV Community

Cristian Prochnow
Cristian Prochnow

Posted on

Success from Form not working ExtJS v4

Issue

While working with a requisition in ExtJS, I found myself in a strange situation. My PHP backend was returning correct response, like this:

{
  "success": true,
  "id": "<some_id>"
}
Enter fullscreen mode Exit fullscreen mode

For success

And...

{
  "success": false,
  "msg": "<error_msg>"
}
Enter fullscreen mode Exit fullscreen mode

For failure

But, even with something like this, in correct structure, my code was not working. In submit function from form, the form to wait for requisition is putting success and failure listeners in configuration object from function, like this:

form.getForm().submit({
  ...,
  success : function(me, action){
    // code...
  },
  failure : function(me, action){
    // code...             
  }
});
Enter fullscreen mode Exit fullscreen mode

Solution

And, what is the answer for this big question? It is simple!

The form request listeners work specifically with response format. So, if request is success, insert "success": true in your JSON response object. Otherwise, insert "success": false, that function will recognize that must call what is passed in failure configuration.

Conclusion

In forms like this, simply follow this principle:

To call what have been passed in success attribute in the configuration object from submit():

{
  "success": true,
  ...
}
Enter fullscreen mode Exit fullscreen mode

To call what have been passed in failure attribute in the configuration object from submit():

{
  "success": false,
  ...
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)