1

I am working on custom product upload form where sellers can upload their products. So i generated a form using the attribute sets which will give the related attributes, lable, type which is required for product form.

In form, I am using frontend_type as input type in a form such as text, select, textarea. However, it gives some different values like

Image
media_image  
gallery  
boolean  
weight  
price  
multiline  
multiselect  
select  
multiple

Generally, it is clear that, which input type they belongs to but how can I use that all. As I want to upload the images for product so is that possible with this frontend_type="image" input. With the select I am able to fetch the values but unable to use another frontend_type. Please let me know if I am creating a form in wrong way and if you have any other suitable idea for generating automatic product form.

Thank you all in advance :)

5

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();
2
  • @Vinai.. Thank you very much Vinai. Actually, I have checked the functionality which is for adminhtml backend file. How can we use this code for the frontend. As I have checked some threads online. It shows that it is not possible in frontend. Please check .... stackoverflow.com/questions/9116788/… ... Thanks in advance :)
    – MagentoBoy
    Jul 1 '13 at 13:20
  • Added an example in the answer.
    – Vinai
    Jul 1 '13 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.