Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExtJS - form failure messagebox

Im building an app in ExtJS and i have a form with a submit failure function like this:

buttons:[{
 text: 'Bestil',
 id:'button_bestil',
 width:85,
 handler: function(){
  create.getForm().submit({
   success: function(f,a){
    //do stuff!
   },
   failure: function(f,a){
    Ext.Msg.alert('Fejl', 'Error');
   }
  });                     
 }
}] 

Now what i want is to show the reason for the error from the fields that where not filled in correct.

F.x. i have a textfield with the following:

vtypeText:'Please type in valid email',
vtype:'email'

I found out that i could use a.resultType. It returns "client". Now how do i get the actual error message.

Hope this makes sense

/Sune

like image 391
sunebrodersen Avatar asked May 31 '26 23:05

sunebrodersen


1 Answers

From what I gather - you want to report back to the user a list of all invalid fields and the reason for each?

This can be done with the code:

YOUR_FORM.getForm().items.each(function( item ) {
   if(item.getActiveError()){
    alert('Field: '+item.name+ ' Error: ' + item.getActiveError());
   }
});

Where YOUR_FORM is the name of the formpanel component encapsulating your form.

What this does is loop through each field in the form and if there is an error (it is invalid) it will report this to the user. You may, for the users sanity, wish to add each error line to a variable and then produce a single message at the end instead of reporting each individually..

i.e:

var ERROR_STRING;
YOUR_FORM.getForm().items.each(function( item ) {
    if(item.getActiveError()){
  ERROR_STRING=ERROR_STRING+"The field '"+item.name+ "' is invalid, reason: " + item.getActiveError() + "<br />"; 
    }
});
if(ERROR_STRING.length>0){
    Ext.MessageBox.alert('Error',ERROR_STRING);
}
like image 158
SW4 Avatar answered Jun 03 '26 11:06

SW4