Adding the Save And Continue Edit Button to Your Magento Admin Form
In Magento, adding a saveAndContinueEdit button to your admin form is a common requirement for many extensions. This allows administrators to save their current work on a record and proceed to edit another related object without leaving the current page.
Understanding the Problem
The error message you’re encountering is due to the JavaScript function saveAndContinueEdit() not being defined in your script. The _addButton method expects this function to handle the button click event, but it’s currently missing.
Step-by-Step Solution
1. Add the Save and Continue Button
You’ve already done the first part correctly by adding the button definition to your Edit.php file:
$this->getForm()->addField(
'saveandcontinue',
'submit',
array(
'label' => Mage::helper('adminhtml')->__('Save And Continue Edit'),
'onclick' => 'saveAndContinueEdit()',
'class' => 'save'
)
);
This code adds a new submit button to the form with the specified label and onclick event handler.
2. Define the JavaScript Function
The key is to define the saveAndContinueEdit() function in your JavaScript file (e.g., in your module’s adminhtml folder under js/saveandcontinue.js). Here’s a basic example:
function saveAndContinueEdit(url) {
if (url) {
saveForm('admin_form', url);
} else {
saveForm('admin_form');
}
}
function saveForm(formId, redirectUrl) {
// Your form saving logic here
if (redirectUrl) {
window.location = redirectUrl;
}
}
In this script:
- The
saveAndContinueEditfunction takes an optional URL parameter. - It calls the
saveFormfunction with the form ID and the provided URL or uses a default value.
3. Linking the JavaScript File to Your Form
To use this JavaScript file in your form, you need to add it in your template file (usually located under template/adminhtml/sliders/edit.phtml) inside the <head> section:
<script type="text/javascript" src="https://your-magento-url/js/saveandcontinue.js"></script>
Replace https://your-magento-url with the actual URL of your Magento instance.
Important Best Practices
-
Form ID: Ensure that the form ID used in the JavaScript function matches the one defined in your form layout XML file.
-
URL Parameter: If you have a specific URL to redirect after saving, include it in the
saveAndContinueEditcall. For example:
$this->getForm()->addField(
'saveandcontinue',
'submit',
array(
'label' => Mage::helper('adminhtml')->__('Save And Continue Edit'),
'onclick' => "saveAndContinueEdit('" . $this->getUrl('*/*/edit', array('id' => $model->getId())) . "')",
'class' => 'save'
)
);
- Form Saving Logic: The
saveFormfunction should contain the actual logic to submit and validate your form data before redirecting.
Conclusion
Adding a saveAndContinueEdit button to a Magento admin form involves defining the button in PHP, creating the necessary JavaScript functions for handling the click event, and linking these functions to your form. Remember to maintain consistent naming conventions across your codebase and ensure that all necessary files are properly linked.
If you’re looking to learn more about Magento 2 development or need help with your project, feel free to explore our comprehensive guides and resources at https://laravelcompany.com/blog/category/magento-2.
Happy coding!