Magento Forum

   
[SOLVED] How to use Mage::throwException to display popup message
 
kiatng
Enthusiast
 
Total Posts:  875
Joined:  2008-09-03
Kuala Lumpur, Malaysia
 

In the SubscriberController.php in the Newsletter module, it uses

Mage::throwException($this->__('Please enter a valid email address'));

to display a popup message.  But when I try to use the same statement in my customized controller, the browser does not display a popup but the Error Submission Form page with the title There has been an error processing your request and with a trace window.

How to get Mage::throwException to display the popup in customized module? Is there some setting I need to do somewhere?

 
Magento Community Magento Community
Magento Community
Magento Community
 
kiatng
Enthusiast
 
Total Posts:  875
Joined:  2008-09-03
Kuala Lumpur, Malaysia
 

Using throwException to bring up the popup message is actually from the payment module. The exception is captured in opcheckout.js in the skin directory:

nextStep: function(transport){
        
if (transport && transport.responseText){
            try{
                response 
eval('(' transport.responseText ')');
            
}
            
catch (e{
                response 
{};
            
}
        }
        
/*
        * if there is an error in payment, need to show error message
        */
        
if (response.error{
            
if (response.fields{
                
var fields response.fields.split(',');
                for (var 
i=0;i<fields.length;i++) {
                    
var field null;
                    if (
field = $(fields[i])) {
                        Validation
.ajaxError(fieldresponse.error);
                    
}
                }
                
return;
            
}
            alert(response
.error)// here is the popup message
            
return;
        
}

        checkout
.setStepResponse(response);

        
//checkout.setPayment();
    
}

I modified the code above and created dhlap.js within the same directory :

var Track = Class.create();
Track.prototype {
    initialize
: function(formsuccessUrlsaveUrl){
        this
.form form;
        if ($(
this.form)) {
            
$(this.form).observe('submit', function(event){this.submitEvent();Event.stop(event);}.bind(this));
        
}       
        this
.successUrl successUrl;
        
this.saveUrl saveUrl;
        
//this.onSave = this.nextStep.bindAsEventListener(this);
        
this.onComplete this.nextStep.bindAsEventListener(this);
        
alert('form=' + $(this.form) ' succ=' this.successUrl ' save=' this.saveUrl); //$(this.form) is null
    
},

  
submitEvent: function()//this never get called, why?
       // var validator = new Validation(this.form);
       // if (validator.validate()) {
            
var request = new Ajax.Request(
                
this.saveUrl,
                
{
                    method
'post',
                    
onCompletethis.onComplete,
                    
parametersForm.serialize(this.form)
                
}
            
);
        
//}
    
},

    
nextStep: function(transport){
        
if (transport && transport.responseText){
            try{
                response 
eval('(' transport.responseText ')');
            
}
            
catch (e{
                response 
{};
            
}
        }
        
/*
        * if there is an error in backend validation, show error message
        */
        
alert('err=' response.error ' f=' response.field);
        if (
response.error{            
            alert(response
.field);
        
else {
        
//[removed]=this.successUrl + '/num=' + response.field;
        
}       
    }
//Track

In the phtml file, I inserted this:

<script type="text/javascript" src="<?php echo $this->getSkinUrl('js/dhlap.js') ?>"></script>
<script type="text/javascript">
var 
track = new Track('track-awb''<?php echo $this->getUrl('rate_card') ?>''<?php echo $this->getUrl('dhlap/track/awb') ?>');
</script>

The form element is:

<form id="track-awb" action=""
<
fieldset>
            <
legend><?php echo $this->__('Track Package'?></legend>
            <!--
label for="awb"><?php echo $this->__('Enter the AWB Number:'?></label-->
            <
input name="awb" type="text" id="awb" class="validate-awb input-text" style="margin-bottom:6px" />
            <
button type="submit" class="form-button"><span><?php echo $this->__('Track'?></span></button>
 </
fieldset>
<
script type="text/javascript">
    
//<![CDATA[
        
var trackForm = new VarienForm('track-awb');
        
Validation.add('validate-awb''Please use 9 to 12 numbers only in this field.'
           
{minLength 9,
            
maxLength 12,
            include : 
['validate-digits']});            
    
//]]>
    
</script>
</form>

But I can’t get this to work. The frontend request never get to the backend. Can anyone help?

 
Magento Community Magento Community
Magento Community
Magento Community
 
kiatng
Enthusiast
 
Total Posts:  875
Joined:  2008-09-03
Kuala Lumpur, Malaysia
 

Solved.  Declaration of the new object Track has to be placed after the form is loaded in the browser.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top