How to Add Div Class in Custom addField Form Function in Magento
Creating custom forms in Magento can be quite straightforward once you understand how to manipulate the form elements using Varien_Data_Form. In this guide, we’ll walk you through adding a <div> element between radio buttons within a custom form.
Understanding the Current Form Structure
You’ve already created a form with radio buttons using the $fieldset->addField method inside your custom module:
$productField=$fieldset->addField('radio2', 'radios', array(
'name' => 'house_building',
'value' => '1',
'class' => 'house_building',
'values' => array(
array('value'=>'1','label'=>'House','id'=>'house'),
array('value'=>'2','label'=>'Building','id'=>'house'),
),
));
The current rendering of this form looks like this:
<div class="value">
<input name="house_building" class="house_building" value="1" id="radio21" checked="checked" type="radio"><label class="inline" for="radio21">House</label>
<input name="house_building" class="house_building" value="2" id="radio22" type="radio"><label class="inline" for="radio22">Building</label>
</div>
Adding the Desired Div Element
To achieve your desired format with additional <div> tags, you’ll need to manually construct the HTML output within your form:
$productField = $fieldset->addField('house_building', 'radios', array(
'name' => 'house_building',
'value' => '1',
'class' => 'house_building',
'values' => array(
array('value'=>'1','label'=>'House','id'=>'house'),
array('value'=>'2','label'=>'Building','id'=>'house'),
),
));
$html = '';
foreach ($productField->getValues() as $option) {
$html .= '<div class="field-container col-xs-12 col-sm-12 col-md-12 col-lg-12">';
$html .= '<input type="radio" checked="checked" id="' . $option['id'] . '" value="' . $option['value'] . '" class="house_building validation-passed" name="house_building">';
$html .= '<label for="' . $option['id'] . '">' . $option['label'] . '</label>';
$html .= '</div>';
}
$productField->setValue($html);
In this modified code, we’re iterating through the options array and manually constructing a <div> element for each option. We then set this constructed HTML as the value of your radio button field.
Rendering the Updated Form
To render this updated form in your view page, you can use the existing method:
<?php echo $this->getChildHtml("suggestions_edit_form") ?>
After applying these changes, when you re-render the form, it should now appear as follows:
<div class="value">
<div class="field-container col-xs-12 col-sm-12 col-md-12 col-lg-12">
<input type="radio" checked="checked" id="house" value="1" class="house_building validation-passed" name="house_building">
<label for="house">House</label>
</div>
<div class="field-container col-xs-12 col-sm-12 col-md-12 col-lg-12">
<input type="radio" id="building" value="2" class="house_building validation-passed" name="house_building">
<label for="building">Building</label>
</div>
</div>
As you can see, the radio buttons are now wrapped within separate <div> elements with additional classes.
Conclusion
In conclusion, adding a <div> element between radio buttons in a Magento form requires manually constructing the HTML output using PHP. By iterating through the options array and setting the constructed HTML as the field’s value, you can achieve your desired formatting.
Remember to always test changes thoroughly after implementing them in your custom modules to ensure they don’t break any existing functionality or introduce security vulnerabilities.
For more advanced techniques and best practices in Magento development, consider visiting Laravel Company for comprehensive guides and tutorials.