4

I created a custom module and the "Save and Continue" button on the back-end grid edit page works partially. Either it saves the values and doesn't stay on the same (edit) page or it stays on the same page but doesn't save the values (this happens when I uncomment the "setLocation" line in __construct() function). How can I fix the button to both save the values and remain on the same page? Please help.

Here is the save action in controller file:

public function saveAction()
{
    //if (!$postData) return $this->getResponse()->setBody('$postData is empty');
    if ($postData = $this->getRequest()->getPost()) {
        $model = Mage::getSingleton('cpstest_productcomment/cps');
        $model->setData($postData);

        try {
            $model->save();

            Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The comment has been saved.'));

            if ($this->getRequest()->getParam('back', false)) {
                 $this->_redirect('*/*/edit', array(
                    'id' => $model->getId(),
                    '_current' => true
                ));
            } else {
                $this->_redirect('*/*/');
            }
            return;
        }
        catch (Mage_Core_Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }
        catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($this->__('An error occurred while saving this comment.'));
        }

        Mage::getSingleton('adminhtml/session')->setCpsData($postData);
        $this->_redirectReferer();
    }
}
  • Possibilities: saveAction() is not called at all, $postData is empty, keys in $postData don't match any of the fields in $model. Suggest to add debug code if ($postData = $this->getRequest()->getPost()) { ... } else { Mage::getSingleton('adminhtml/session')->addNotice('empty $postData');} – kiatng Mar 30 '17 at 4:05
  • @kiatng I think saveAction is being called, because the Save button works well. Where should I add the debug code? In controller file? Thanks for your help. – bestwebdevs Mar 30 '17 at 17:05
  • You need evidence that saveAction() is being called. The debug code can be in public function saveAction(). Simple way to do it's with Mage::getSingleton('adminhtml/session')->addNotice('some debug info'); in anywhere of the code. If you want evidence that it is being called, place that statement right at line 1 in public function saveAction() with the message 'I am here'. – kiatng Mar 31 '17 at 5:59
  • Hi @ kiatng . I added the debug code just like you said and when I press "Save" the text shows up, but when I press "Save and Continue Edit" it doesn't show up. – bestwebdevs Mar 31 '17 at 17:18
  • It means that $this->getCurrentUrl() in 'onclick' => "setLocation('{$this->getCurrentUrl()}')", doesn't return the correct URL. – kiatng Apr 3 '17 at 5:15
1

Add the below code in your admin block constructor

$this->_formScripts[] = " function saveAndContinueEdit(){
    editForm.submit($('edit_form').action+'back/edit/');
}";
0

Try adding the following function in your Edit.php

public function getCurrentUrl()
{
    return $this->getUrl('*/cps/save', array(
        '_current'  => true,
        'back'      => 'edit',
        'tab'       => '{{tab_id}}'
    ));
}

[edit1] Changed '*/*/save' to '*/cps/save'

[edit2] Add this to your controller after ..->addSuccess();

if ($this->getRequest()->getParam('back', false)) {
     $this->_redirect('*/*/edit', array(
        'id' => $model->getData('primaty_index_column_name'), // to replace with actual name
        '_current' => true
    ));
} else {
    $this->_redirect('*/*/');
}               
return;
  • Hi @kiatng. How do I know the value of "tab_id"? What should I put there? Thanks – bestwebdevs Apr 3 '17 at 17:30
  • tab_id is set in the browser's js, in function saveAndContinueEdit(). You do not need to do anything if you only have one tab. – kiatng Apr 5 '17 at 10:07
  • I added the function and it's redirecting to a blank page and again doesn't save any value. – bestwebdevs Apr 5 '17 at 21:05
  • You need to compare the URLs on the Save button and Save and Continue button in your browser. The controller and action of both should be the same. If you do not know how to construct the correct one, paste both URLs here. – kiatng Apr 7 '17 at 1:04
  • Here is the URL for the "save and continue" - formodulecheck.dev/index.php/admin/cps/edit/id/28/key/… And for the "save" button - formodulecheck.dev/index.php/admin/cps/index/key/… – bestwebdevs Apr 7 '17 at 16:28

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.