The frontend_input
renderers are rendered by pre-made classes, you don't have to build the form elements yourself.
A good reference is the method Mage_Adminhtml_Block_Widget_Form::_setFieldset()
, which takes an array of attribute models and a fieldset instance to automatically generate the form for those attributes.
What it comes down to, is the method
public function addField($elementId, $type, $config, $after=false)
{
if (isset($this->_types[$type])) {
$className = $this->_types[$type];
}
else {
$className = 'Varien_Data_Form_Element_'.ucfirst(strtolower($type));
}
$element = new $className($config);
$element->setId($elementId);
$this->addElement($element, $after);
return $element;
}
You can see that the actual classes used for the elements can be found in lib/Varien/Data/Form/Element/
Those classes then render their output by calling the method getHtml()
.
You can also specify custom renderers by calling $form->addType('myType', 'My_Class_Name')
where My_Class_Name
would be a class in your module extending Varien_Data_Form_Element_Abstract
.
The whole system is only used in the backend form widget system by native Magento instances, but since it's actually part of the Varien library, there is no reason not to use it on the frontend, too.
Update:
You have to bypass the Mage_Adminhtml_Block_Widget_Form
, and use Varien_Data_Form
directly. For example:
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('catalog/test/save',
array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post',
'enctype' => 'multipart/form-data',
'use_container' => true,
));
$fieldset = $form->addFieldset('testfieldset', array('legend' => 'Test'));
$fieldset->addField('test', 'text', array('label' => 'Test', 'name' => 'test'));
echo $form->toHtml();