2

I have a custom form in Magento admin. When I submit the form with multiple click of the save button the multiple entry are inserted in my table, even if I have a form key.

if ($data = $this->getRequest()->getPost()) {       
    $request = $this->getRequest();
    $formKey = Mage::getSingleton('core/session')->getFormKey();             
    $request->setParam('form_key', $formKey);   
    $model = Mage::getModel('flpackage/packages');
    $id = $this->getRequest()->getParam('id');
    $oldPrice = '';
    if ($id) {
        $model->load($id);
        $oldPrice = $model->getData('package_price');                                                
    }
    Mage::getSingleton('adminhtml/session')->setFormData($data);
    try {
        $model->setData($data);
    } catch (Exception $e) {
        $this->__($e->getMessage());
        $this->getCoreSession()->addError($e->getMessage());
        if($this->getRequest()->getParam('id')) {
            $this->_redirect('*/*/edit', array('id' => $id));
        } else {
            $this->_redirect('*/*/edit');    
        }
        return ;
    }
    if($id) {
        $model->setId($id);  
    }
    $model->save();
}

Screen: enter image description here

Kindly suggest your answer for solving this issue.

4
+25

You can create a function in one of your javascript files like this:

function savePackage(){
    disableElements('save');
    if (editForm.submit() == false){
        enableElements('save');
    }
    varienGlobalEvents.attachEventHandler('formValidateAjaxComplete', function (){
        enableElements('save');
    });
}

Then, in your Edit.php block for your entity add this line in the _prepareLayout method:

$this->_updateButton('save', 'onclick', 'savePackage()');

This is not a hack or something I came up with.
This is how it works for saving the product attributes in the core:

  • This is the cleanest answer because it does not override core functionality. – Toon Van Dooren Dec 4 '15 at 13:10
0

Package Name has to be unique? If so, you could check before save, if item with the same name already exists:

$packageName = $this->getRequest()->getParam('package_name');
if ($model->load($packageName, 'package_name')->getId()) {
    $model->save();
}
  • This is only work for the particular module. due you have any other idea to avoid inserting the multiple entry when click the save button multiple time – senthil Dec 3 '15 at 5:37
  • i check with existing customer and newsletter module in magento admin panel.but the customer multitime form submit is working fine(i mean even i click the save button multitime the single entry only inserting) but the newsletter if i click the save button for creating template means the template are inserting multiple time. – senthil Dec 3 '15 at 5:42
0

Well that was interesting bug in core Magento. Just do one line change this way.

  1. Open file /js/mage/adminhtml/form.js
  2. Add below one line code just before form is submit.

            /*Multiple Entry Bug Fix Start*/
            document.getElementsByClassName("save")[0].disabled = true;
            /*Multiple Entry Bug Fix End*/
    

    Approx line 67 (Just after admin form validation success)

This will fix all multiple entry bug in all Magento modules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.