Save button not working in custom module form

Save button not working in custom module form

August 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Save button not working in custom module form

Solving Button Redirect Issues in Magento Uicomponent Forms

Introduction

You’ve encountered an issue where your custom module’s save button isn’t redirecting to the specified action page after being clicked. Instead, it reloads the current edit page with a mysterious parameter appended to the URL.

Let’s dive into this problem and find a solution that ensures your buttons work as expected in your Magento admin form.

Understanding the Issue

The core of your issue lies in how Magento processes and handles button actions within its uicomponent architecture. When you click on a button, Magento expects certain data structures to be properly configured for the redirection process to function correctly.

Analyzing Your Current Setup

The Button Configuration

In your XML configuration file, you’ve defined three buttons: ‘back’, ‘delete’, and ‘save’. Each is linked to a corresponding PHP class that should handle its respective action. This setup looks correct.

<item name="buttons" xsi:type="array">
    <item name="back" xsi:type="string"><Vendor>\<Module>\Block\Adminhtml\Listings\Edit\BackButton</item>
    <item name="delete" xsi:type="string"><Vendor>\<Module>\Block\Adminhtml\Listings\Edit\DeleteButton</item>
    <item name="save" xsi:type="string"><Vendor>\<Module>\Block\Adminhtml\Listings\Edit\SaveButton</item>
</item>

The SaveButton Class

Your SaveButton class extends a GenericButton, which provides some useful functionality. However, there are a couple of points to note:

  1. You’ve overridden the getButtonData() method in your SaveButton class. This is correct; it allows you to specify the button’s properties, including its URL.

  2. The URL generated for the save action uses the ‘properties/listings/update’ route and passes the property_id as a parameter. This looks good on first glance, but let’s examine it more closely.

The GenericButton Class

This class is responsible for building several shared functions used by multiple buttons in your module. It includes methods to fetch the ID of the current record being edited (using Magento’s registry) and generate URLs using Magento’s built-in URL builder.

Nothing seems amiss here initially, but let’s explore a potential issue.

The Potential Problem

The Registry Usage

In your getId() method within the GenericButton class:

$data = $this->registry->registry('property_data');
return $data['property_id'];

You’re fetching data from Magento’s registry using the key ‘property_data’. This is likely because you’ve set this data in a controller or an earlier page action.

However, there are two potential issues here:

  1. Timing: If your save button is being processed before the registry has been updated with the correct property_id (maybe due to timing issues between the form submission and the button processing), it might read an old value or even return a null value if no data exists yet.

  2. Data Consistency: If you’ve set ‘property_data’ in a controller but aren’t properly updating the registry after each save or update operation, the button could be working with stale data.

A Potential Solution

Ensuring Registry Data Freshness

To resolve this issue, you should ensure that whenever the property_id is updated or created successfully, you’re also updating the ‘property_data’ in Magento’s registry. This can be done directly from your controller’s action.

Here’s an example:

public function execute()
{
    // Assume $newPropertyId is your saved property_id after a successful save.
    $propertyData = ['property_id' => $newPropertyId, 'other_data' => 'some_data'];
    $this->registry->register('property_data', $propertyData);

    // Continue with the rest of your logic
    // ...
}

Double-Checking Data Before Redirect

In your SaveButton class, you could add a check to ensure that the property_id is not null before generating the URL. If it’s null or empty, you can redirect to a default error page or handle it according to your application’s logic.

public function getButtonData()
{
    $propertyId = $this->getId();
    if ($propertyId === null) {
        // Handle the case where property_id is null. Redirect to an error page, for example.
        return [
            'label' => __('Save Property'),
            'class' => 'save primary',
            'sort_order' => 90,
        ];
    }

    $url = $this->getUrl('properties/listings/update', ['property_id' => $propertyId]);
    // ... rest of your code
}

Conclusion

Debugging redirection issues in Magento admin forms can be challenging, especially when dealing with uicomponent architecture. The key lies in ensuring that the data used for redirection is always fresh and properly fetched.

By double-checking the registry data before generating URLs and updating the registry after every successful save operation, you should be able to resolve the redirect issue. Always keep a close eye on your controllers’ actions to ensure they’re properly handling your module’s data flow.

Remember to thoroughly test your solution in different scenarios to guarantee it works reliably across all cases.

Happy coding! If you found this helpful, feel free to check out Laravel Company for more in-depth tutorials and guides on Magento development.

Extensions Admin Configuration Errors

Our network