Custom forms in Magento

Custom forms in Magento

July 28, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Custom forms in Magento

Creating Custom Forms in Magento: A Step-by-Step Guide

As an experienced Magento developer, I understand the importance of creating custom forms to enhance user engagement and gather relevant data. In this guide, we’ll walk through the process of creating a front-end form that posts data directly to a controller action.

Understanding the Default Contact Form

Before diving into our custom form, let’s briefly review how Magento handles the default contact us form. You can modify this form as outlined in the official Magento documentation (https://docs.magento.com/user-guide/stores/attributes-contact.html). This involves adding new attributes to the ‘Contact Us’ page.

Creating a New Custom Form

For our example, we’ll create a feedback form with an additional field (besides name) that posts data to a controller action. The basic structure of this form is provided in your question:

<form action="<?php echo $this->getFormAction(); ?>" id="feedbackForm" method="post">
    <div class="input-box">
        <label for="name"><?php echo Mage::helper('contacts')->__('Name') ?> <span class="required">*</span></label><br />
        <input name="name" id="name" title="<?php echo Mage::helper('contacts')->__('Name') ?>" value="<?php echo $this->htmlEscape($this->helper('contacts')->getUserName()) ?>" class="required-entry input-text" type="text" />
    </div>

    <!-- Add your new fields here -->

    <div class="button-set">
        <p class="required"><?php echo Mage::helper('contacts')->__('* Required Fields') ?></p>
        <button class="form-button" type="submit"><span><?php echo Mage::helper('contacts')->__('Submit') ?></span></button>
    </div>
</form>

Step 1: Create the Custom Form Template

Create a new file in your custom theme directory (e.g., app/design/frontend/default/[yourtheme]/template/contacts/form.phtml). Copy and paste this basic form structure into the new file.

Step 2: Define the Controller Action

Next, you need to create an action that will handle the POST data from our form. In your custom module (LaravelCompany recommends using modules for organization), create a new controller under the controllers directory:

class FeedbackController extends Mage_Core_Controller_Front_Action
{
    public function postAction()
    {
        $post = $this->getRequest()->getPost();
        if ($post) {
            // Process your form data here

            // Example: Save data to a database or send an email
        }
    }
}

Step 3: Map the Form Action to the Controller

To map our custom form action to the controller, we need to modify the action attribute in the form template. Update the line:

<?php echo $this->getFormAction(); ?>

to point to your new controller action:

feedback/index/post

This assumes that you have created a new router in your module’s config.xml file:

<frontend>
    <routers>
        <feedback>
            <use>standard</use>
            <args>
                <module>LaravelCompany_Feedback</module>
                <frontName>feedback</frontName>
            </args>
        </feedback>
    </routers>
</frontend>

Step 4: Add Your New Form Field

Now, let’s add a new field to our form template. For instance, we’ll include an ‘Email’ field:

<div class="input-box">
    <label for="email"><?php echo Mage::helper('contacts')->__('Email') ?> <span class="required">*</span></label><br />
    <input name="email" id="email" title="<?php echo Mage::helper('contacts')->__('Email') ?>" value="" class="required-entry input-text validate-email" type="text" />
</div>

Make sure to include the validate-email class for basic email validation.

Step 5: Fetch Form Data in the Controller

In your controller’s postAction, you can now access the form data using:

$name = $this->getRequest()->getPost('name');
$email = $this->getRequest()->getPost('email');

// Process or save these values as needed

Conclusion

Creating custom forms in Magento involves several steps, from creating a new template to mapping it to the appropriate controller action. By following this guide, you can create customized feedback forms with additional fields that post data directly to your controller for processing.

Remember to test your form thoroughly and ensure that it behaves as expected across different browsers and devices. If you need further assistance or want to explore more advanced Magento development techniques, feel free to visit LaravelCompany for expert guidance and resources.

Happy coding!

Import/Export

Our network