Magento admin input form fields through coding

Every Magento developer who’s ever worked with backend forms knows the need to add, edit or delete various types of fields through coding. Be it a simple text box or a drop down list, any field can be added with its attributes and values from your PHP file. Each time you create of those fields you’d need to refer to the syntax of each field. So for the benefit of all of us here’s a quick reference to the proper syntax of all field types that can be added into a form.

Here is how to add a text field to an admin form, with a list of all the options.

Time

$fieldset->addField('time', 'time', array(
          'label'     => Mage::helper('form')->__('Time'),
          'class'     => 'required-entry',
          'required'  => true,
          'name'      => 'title',
          'onclick' => "",
          'onchange' => "",
          'value'  => '12,04,15',
          'disabled' => false,
          'readonly' => false,
          'after_element_html' => '<small>Comments</small>',
          'tabindex' => 1
        ));

TextArea

$fieldset->addField('textarea', 'textarea', array(
          'label'     => Mage::helper('form')->__('TextArea'),
          'class'     => 'required-entry',
          'required'  => true,
          'name'      => 'title',
          'onclick' => "",
          'onchange' => "",
          'value'  => '<b><b/>',
          'disabled' => false,
          'readonly' => false,
          'after_element_html' => '<small>Comments</small>',
          'tabindex' => 1
        ));

Note

$fieldset->addField('note', 'note', array(
          'text'     => Mage::helper('form')->__('Text Text'),
        ));

Submit Button

$fieldset->addField('submit', 'submit', array(
          'label'     => Mage::helper('form')->__('Submit'),
          'required'  => true,
          'value'  => 'Submit',
          'after_element_html' => '<small>Comments</small>',
          'tabindex' => 1
        ));

Text Field

$fieldset->addField('title', 'text', array(
          'label'     => Mage::helper('form')->__('Title3'),
          'class'     => 'required-entry',
          'required'  => true,
          'name'      => 'title',
          'onclick' => "alert('on click');",
          'onchange' => "alert('on change');",
          'style'   => "border:10px",
          'value'  => 'hello !!',
          'disabled' => false,
          'readonly' => true,
          'after_element_html' => '<small>Comments</small>',
          'tabindex' => 1
        ));

Multiselect

$fieldset->addField('multiselect2', 'multiselect', array(
          'label'     => Mage::helper('form')->__('Select Type2'),
          'class'     => 'required-entry',
          'required'  => true,
          'name'      => 'title',
          'onclick' => "return false;",
          'onchange' => "return false;",
          'value'  => '4',
          'values' => array(
                                '-1'=> array( 'label' => 'Please Select..', 'value' => '-1'),
                                '1' => array(
                                                'value'=> array(array('value'=>'2' , 'label' => 'Option2') , array('value'=>'3' , 'label' =>'Option3') ),
                                                'label' => 'Size'    
                                           ),
                                '2' => array(
                                                'value'=> array(array('value'=>'4' , 'label' => 'Option4') , array('value'=>'5' , 'label' =>'Option5') ),
                                                'label' => 'Color'   
                                           ),                                         
                                  
                           ),
          'disabled' => false,
          'readonly' => false,
          'after_element_html' => '<small>Comments</small>',
          'tabindex' => 1
        ));

Submit Button

$fieldset->addField('radio', 'radio', array(
          'label'     => Mage::helper('form')->__('Radio'),
          'name'      => 'title',
          'onclick' => "",
          'onchange' => "",
          'value'  => '1',
          'disabled' => false,
          'readonly' => false,
          'after_element_html' => '<small>Comments</small>',
          'tabindex' => 1
        ));
	
$fieldset->addField('radio2', 'radios', array(
          'label'     => Mage::helper('form')->__('Radios'),
          'name'      => 'title',
          'onclick' => "",
          'onchange' => "",
          'value'  => '2',
          'values' => array(
                            array('value'=>'1','label'=>'Radio1'),
                            array('value'=>'2','label'=>'Radio2'),
                            array('value'=>'3','label'=>'Radio3'),
                       ),
          'disabled' => false,
          'readonly' => false,
          'after_element_html' => '<small>Comments</small>',
          'tabindex' => 1
        ));

Link

$fieldset->addField('link', 'link', array(
          'label'     => Mage::helper('form')->__('Link'),
          'style'   => "",
          'href' => 'https://indiestechtips.wordpress.com',
          'value'  => 'Magento Blog',
          'after_element_html' => ''
        ));

DropDown

$fieldset->addField('select', 'select', array(
          'label'     => Mage::helper('form')->__('Select'),
          'class'     => 'required-entry',
          'required'  => true,
          'name'      => 'title',
          'onclick' => "",
          'onchange' => "",
          'value'  => '1',
          'values' => array('-1'=>'Please Select..','1' => 'Option1','2' => 'Option2', '3' => 'Option3'),
          'disabled' => false,
          'readonly' => false,
          'after_element_html' => '<small>Comments</small>',
          'tabindex' => 1
        ));

Here is another version of the drop down

$fieldset->addField('select2', 'select', array(
          'label'     => Mage::helper('form')->__('Select Type2'),
          'class'     => 'required-entry',
          'required'  => true,
          'name'      => 'title',
          'onclick' => "",
          'onchange' => "",
          'value'  => '4',
          'values' => array(
                                '-1'=>'Please Select..',
                                '1' => array(
                                                'value'=> array(array('value'=>'2' , 'label' => 'Option2') , array('value'=>'3' , 'label' =>'Option3') ),
                                                'label' => 'Size'    
                                           ),
                                '2' => array(
                                                'value'=> array(array('value'=>'4' , 'label' => 'Option4') , array('value'=>'5' , 'label' =>'Option5') ),
                                                'label' => 'Color'   
                                           ),                                         
                                  
                           ),
          'disabled' => false,
          'readonly' => false,
          'after_element_html' => '<small>Comments</small>',
          'tabindex' => 1
        ));

Password Field

$fieldset->addField('password', 'password', array(
          'label'     => Mage::helper('form')->__('Password'),
          'class'     => 'required-entry',
          'required'  => true,
          'name'      => 'title',
          'onclick' => "",
          'onchange' => "",
          'style'   => "",
          'value'  => 'hello !!',
          'disabled' => false,
          'readonly' => false,
          'after_element_html' => '<small>Comments</small>',
          'tabindex' => 1
        ));
	
$fieldset->addField('obscure', 'obscure', array(
          'label'     => Mage::helper('form')->__('Obscure'),
          'class'     => 'required-entry',
          'required'  => true,
          'name'      => 'obscure',
          'onclick' => "",
          'onchange' => "",
          'style'   => "",
          'value'  => '123456789',
          'after_element_html' => '<small>Comments</small>',
          'tabindex' => 1
        ));

File Upload

$fieldset->addField('file', 'file', array(
          'label'     => Mage::helper('form')->__('Upload'),
          'value'  => 'Uplaod',
          'disabled' => false,
          'readonly' => true,
          'after_element_html' => '<small>Comments</small>',
          'tabindex' => 1
        ));

Multiline

$fieldset->addField('multiline', 'multiline', array(
          'label'     => Mage::helper('form')->__('Multi Line'),
          'class'     => 'required-entry',
          'required'  => true,
          'name'      => 'title',
          'onclick' => "",
          'onchange' => "",
          'style'   => "border:10px",
          'value'  => 'hello !!',
          'disabled' => false,
          'readonly' => true,
          'after_element_html' => '<small>Comments</small>',
          'tabindex' => 1
        ));

Label

$fieldset->addField('label', 'label', array(
          'value'     => Mage::helper('form')->__('Label Text'),
        ));

Image Upload

$fieldset->addField('image', 'image', array(
          'value'     => 'https://indiestechtips.wordpress.com/wp-themes/test/images/logo.gif ',
        ));

Checkbox

$fieldset->addField('checkbox', 'checkbox', array(
          'label'     => Mage::helper('form')->__('Checkbox'),
          'name'      => 'Checkbox',
          'checked' => false,
          'onclick' => "",
          'onchange' => "",
          'value'  => '1',
          'disabled' => false,
          'after_element_html' => '<small>Comments</small>',
          'tabindex' => 1
        ));

$fieldset->addField('checkboxes', 'checkboxes', array(
          'label'     => Mage::helper('form')->__('Checkboxs'),
          'name'      => 'Checkbox',
          'values' => array(
                            array('value'=>'1','label'=>'Checkbox1'),
                            array('value'=>'2','label'=>'Checkbox2'),
                            array('value'=>'3','label'=>'Checkbox3'),
                       ),
          'onclick' => "",
          'onchange' => "",
          'value'  => '1',
          'disabled' => false,
          'after_element_html' => '<small>Comments</small>',
          'tabindex' => 1
        ));

Date

$fieldset->addField('date', 'date', array(
          'label'     => Mage::helper('form')->__('Date'),
          'after_element_html' => '<small>Comments</small>',
          'tabindex' => 1,
          'image' => $this->getSkinUrl('images/grid-cal.gif'),
          'format' => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)
        ));

If you want a look at all the fields that can be created by Magento you can find all of them in here.
lib\Varien\Data\Form\Element

Hope it helps you while you’re treading you way though Magento backend forms creation. Comment here for any questions or suggestions.

Speak Your Mind

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: