Magento: How to add onsubmit to form

Magento: How to add onsubmit to form

October 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: How to add onsubmit to form

Magento: How to Add onsubmit Event to Form

As a senior developer specializing in e-commerce platforms like Magento, I frequently encounter challenges related to form submissions and JavaScript integration. In this blog post, we will address the issue of adding an onsubmit event to a custom form in Magento.

Firstly, let’s understand the context provided by the user:

  • The user has already added a JavaScript file containing functions through the local.xml configuration.
  • A custom field type has been created and is being used in a form similar to a multiselect option.
  • The goal is to select all options from this custom field before submitting the form on the onsubmit event.

Preparing the Form in Magento

The user has provided the code for the form file where they are trying to add the onsubmit event:

class MyNamespace_MyModule_Block_Adminhtml_Form_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    //...

    protected function _prepareForm()
    {
        $form = new Varien_Data_Form(array(
            'id' => 'edit_form',
            'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
            'method' => 'post',
            'onsubmit' => "selectAllOptions(document.getElementById('MY_FORM_FIELD_NAME'))",
        ));

        //...

        return parent::_prepareForm();
    }
}

The user is correctly using the Varien_Data_Form constructor to initialize the form with an id, action, method, and attempting to add a custom onsubmit function.

The SelectAllOptions Function

The user has also provided the JavaScript function selectAllOptions:

function selectAllOptions(obj) {
    if (!obj.options.length) {
        return;
    }
    for (var i = 0; i < obj.options.length; i++) {
        obj.options[i].selected = true;
    }
}

This function iterates through the options of the selected element and sets each option to be selected.

The Challenge: The Magento Button Overrides

The issue arises from how Magento handles form submission buttons. By default, Magento uses an inline JavaScript click event handler for its save button:

<button style="" onclick="editForm.submit();" class="scalable save" type="button" title="Test">
    <span><span><span>Save</span></span></span>
</button>

The onclick attribute calls the editForm.submit() function, which bypasses the native form submission event. This override prevents the custom onsubmit function from being executed when the save button is clicked.

The Solution: Extending Magento’s Default Form

To resolve this issue and execute the custom onsubmit function, we need to extend Magento’s default form behavior without breaking the existing functionality.

Here are the steps to achieve this:

  1. Create a new file called local.xml in your custom module configuration directory (if it doesn’t exist already).

  2. Add the following XML code as a child node of the <config> element:

<reference name="adminhtml.form.edit">
    <action method="setHtmlAttributes">
        <params>
            <id>edit_form</id>
            <onsubmit type="text">selectAllOptions(document.getElementById('MY_FORM_FIELD_NAME'));</onsubmit>
        </params>
    </action>
</reference>
  1. Replace 'MY_FORM_FIELD_NAME' with the actual ID of your custom field.

  2. Run mage:compile or php bin/magento setup:upgrade to apply the changes.

This configuration updates Magento’s default form behavior by setting a custom onsubmit function for the specified form ID. The new XML node extends the adminhtml.form.edit block and uses the setHtmlAttributes action to add the desired onsubmit attribute with the provided JavaScript code.

Conclusion

In this blog post, we have examined the challenge of adding an onsubmit event to a custom form in Magento. By understanding how Magento overrides its default form submission behavior and extending it through configuration XML files, we can successfully execute our custom JavaScript functions before the actual form submission occurs.

Remember that proper documentation and following best practices are crucial for maintaining a stable and easy-to-update e-commerce platform like Magento. Always refer to the official documentation and consider seeking expert assistance if you encounter complex or recurring issues during your development process.

For more in-depth guides on Magento development, visit our technical blog at https://laravelcompany.com. We provide comprehensive articles, tutorials, and best practices for building and maintaining successful e-commerce solutions.

Extensions Admin Configuration Errors

Our network