Custom forms in Magento


Can anyone provide a dummy guide \ code snippets on how to create a front end form in Magento that posts data to a controller action.

Im trying to write a variant of the contact us from. (I know its easy to modify the contact us form, as outlined here). I'm trying to also create a feedback form with additional fields.

Given this basic form:

<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 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>

What are the basic step I need to take to get inputted name to a controller action for processing?

9/25/2015 8:10:29 AM

Accepted Answer

If any one is interested, I solved this by building my own module which was heavily based on the Magento_Contacts module.

Here are some links that helped me figure things out.

9/16/2015 12:51:45 PM

To make $this->getFormAction() return the URL to your custom controller, you have two options:

  1. call setFormAction() somewhere else on the block.
  2. use a custom block type that implements getFormAction().

(1) is what happens in Mage_Contacts_IndexController::indexAction(), but (2) is the cleaner approach and I'm going to explain it in detail:

Create a custom module


<?xml version="1.0"?>


<?xml version="1.0"?>

This configuration registers the stack_form block alias for own blocks and the feedback front name for own controllers.

Create custom block


class Stack_Form_Block_Form extends Mage_Core_Block_Template
    public function getFormAction()
        return $this->getUrl('stack_form/index/post`);

Here we implemented getFormAction() to generate the URL for our custom controller (the result will be BASE_URL/feedback/index/post).

Create custom controller


class Stack_Form_IndexController extends Mage_Contacts_IndexController
    public function postAction()
        // your custom post action

If the form should behave exactly like the contact form, just with a different email template and additional form fields, there are two solutions that I have outlined at where only one of them actually requires a custom controller action to send the form:

If you look at the contacts controller used in the form action, you will find that

  1. the transactional template is taken directly from the configuration
  2. all POST data is passed to the template (as template variable data), so that you can add any additional fields to the form template and use them in the email template. But validation is hard coded for "name", "comment", "email" and "hideit".

So, if you need a completely different email template or additional/changed input validation, your best bet is to create a custom controller with a modified copy of the postAction of Mage_Contacts_IndexController.

But there is another solution that is a bit limited but without any custom code involved:

  • create a hidden input that determines the type of the form. It could be just <input type="hidden" name="custom" value="1" />.
  • in the contact transactional email template, use the if directive to show different content based on the form type:

    {{if data.custom}}
        ... custom contact form email ...
        ... standard contact form email ...

How to use this custom block

You can add the form anywhere in the CMS using this code (CMS directive):

{{block type="stack_form/form" template="path/to/your/form.phtml"}}

If you do this, you need to add "stack_form/form" to the block whitelist under System > Permissions > Blocks!

Or in the layout using this code (layout XML):

<block type="stack_form/form" name="any_unique_name" template="path/to/your/form.phtml" />

Solution without custom module

If you use the solution without custom controller and a single email template mentioned above, you can set the form action using layout XML as well.

To achieve this, we use the feature to call helpers as parameters for block actions. Unfortunately, the core helper does not have a public method to get a URL but the helper from Mage_XmlConnect has, so you can use that one:

<block type="core/template" name="any_unique_name" template="path/to/your/form.phtml">
    <action method="setFormAction">
        <param helper="xmlconnect/getUrl">

In the CMS directive you cannot use helpers, so there you would need to put the actual URL:

{{block type="stack_form/form" template="path/to/your/form.phtml" form_action="/feedback/index/post"}}

Since you probably have different CMS pages/blocks in different store views, this should not be a big problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow