Adding Custom attribute in forms

Adding Custom attribute in forms

September 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Adding Custom attribute in forms

Adding Custom Attributes in Magento EAV

As a senior developer at Laravel Company, I often encounter clients who need to add custom attributes to their Magento stores while using the EAV (Entity Attribute Value) model. In this blog post, I will guide you through the process of adding new customer attributes and making them editable from both the frontend and backend.

Creating New Customer Attributes

To create new customer attributes in your EAV module, you need to follow these steps:

  1. Create a new file named install.php inside your custom module’s etc directory. This file will contain the installation script for your custom attributes.
  2. In the install.php file, define an array of attributes with their corresponding configurations. Here is an example of adding two custom attributes: profile image and mobile number.
<?php

$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->addAttribute('customer', 'profile_image', array(
    'label'     => 'Profile Image',
    'type'      => 'varchar',
    'input'     => 'file',
    'visible'   => true,
    'required'  => false,
    'user_defined' => true,
));

$setup->addAttribute('customer', 'mobile', array(
    'label'     => 'Mobile Number',
    'type'      => 'int',
    'input'     => 'text',
    'visible'   => true,
    'required'  => false,
    'user_defined' => true,
));

$installer->endSetup();
  1. In the etc directory, create another file named config.xml. This file will define the configuration settings for your custom attributes.
  2. Inside the config.xml file, define the entity type, attribute set, and other necessary configurations. Here is an example:
<config>
    <modules>
        <Namespace_Customer>
            <version>0.1.0</version>
        </Namespace_Customer>
    </modules>

    <global>
        <models>
            <customer>
                <class>Namespace_Customer_Model</class>
                <resourceModel>Namespace_Customer_Model_Resource</resourceModel>
            </customer>
        </models>
    </global>
</config>

Adding Custom Attributes to the Customer Form

To add your custom attributes to the customer edit form in the frontend, follow these steps:

  1. Create a new file named Form.php inside your custom module’s Block/Adminhtml/ directory.
  2. In the Form.php file, extend the Mage_Adminhtml_Block_Template class and override the _prepareForm() method to add your custom attributes.
<?php

class Namespace_Customer_Block_Adminhtml_Form extends Mage_Adminhtml_Block_Template
{
    public function _prepareForm()
    {
        $form = new Varien_Data_Form();

        // Add your custom attributes here using the $form->addField($fieldName, $type) method

        return $form;
    }
}
  1. In your config.xml file, map the custom form block to the appropriate controller and action.
<admin>
    <routers>
        <customer>
            <args>
                <modules>
                    <Namespace_Customer before="Mage_Adminhtml">
                        <class>Namespace_Customer_Block_Adminhtml</class>
                    </Namespace_Customer>
                </modules>
            </args>
        </customer>
    </routers>
</admin>

Preparing the Grid for Custom Attributes

To display your custom attributes in the customer grid, you need to modify the Grid.php file of your custom module’s Block/Adminhtml/ directory.

  1. Override the _prepareCollection() method in your Grid.php file and retrieve the collection using the appropriate model.
protected function _prepareCollection()
{
    $collection = Mage::getModel('namespace_customer/eavcustomer')->getCollection();
    $this->setCollection($collection);
    return parent::_prepareCollection();
}
  1. Create a new Grid.php file inside your custom module’s Block/Adminhtml/ directory and extend the Mage_Adminhtml_Block_Widget_Grid class.
  2. In the new Grid.php file, override the _prepareColumns() method to include columns for your custom attributes.
protected function _prepareColumns()
{
    $this->addColumn('entity_id', array(
        'header' => Mage::helper('namespace_customer')->__('ID'),
        'index'  => 'entity_id',
    ));

    // Add additional columns for your custom attributes here

    return parent::_prepareColumns();
}

Conclusion

Adding custom attributes to the Magento EAV customer module can be a complex task, but with proper understanding and following these steps, you can successfully create new attributes and make them editable from both the frontend and backend. Make sure to update your codebase accordingly to ensure compatibility with your existing modules.

If you need further assistance or want to explore more advanced customizations in Magento, feel free to visit Laravel Company for expert services and solutions tailored to your specific requirements.

Database Extensions Admin Configuration

Our network