I have a requirement that I need to create a field like so:
The issue is that the field has multiple input textboxes, and that they don't populate with their values on save. The data is entered into the database correctly, but the form won't contain the values properly.
Here is my Media.php file where I create the custom field:
//custom fieldset for margins
$fieldset->addType('homepagemargin', 'Company_Homepage_Model_Varien_Data_Form_Element_HomepageMargin');
$fieldset->addField(
'margin_'.$i,
'homepagemargin',
array(
'label' => Mage::helper('homepage')->__('Margin: '),
'name' => 'margin_'.$i
)
);
There is some code further below which sets the values of the $data variable:
// We fill the form based on the session or the registered data
if (Mage::getSingleton('adminhtml/session')->getHomepageData()) {
$form->setValues($data);
Mage::getSingleton('adminhtml/session')->setHomepageData(null);
} elseif (Mage::registry('homepage_data')) {
$form->setValues($data);
}
I've done an error_log on the $data variable, and found that the values I entered into the textboxes are being stored correctly in that $data variable. However, $form->setValues($data) is not setting the values of the input fields. I believe this is because they are not exactly a part of the form, the field homepagemargin is part of the form, but not the elements contained WITHIN that field.
And then here is the custom class to create the field: HomepageMargin.php
<?php
/**
* Class Company_Homepage_Model_Varien_Data_Form_Element_HomepageImage
*/
class Company_Homepage_Model_Varien_Data_Form_Element_HomepageMargin extends Varien_Data_Form_Element_Abstract
{
/**
* Constructor for custom Homepage Box element
* @param array $data
*/
public function __construct($data)
{
parent::__construct($data);
}
/**
* @return string
*/
public function getElementHtml()
{
$number = $this->getName();
//returns digit at the end of string
$number = substr($number, -1);
$html = '';
$html .= '<table>
<tr>
<td></td>
<td>Margin Top: <br><input type="text" id="top_margin_' . $number . '" name="top_margin_' . $number . '"></td>
<td></td>
</tr>
<tr>
<td class="toppadding">Margin Left: <br><input type="text" id="left_margin_' . $number . '" name="left_margin_' . $number . '"></td>
<td><div class="box"></div></td>
<td class="toppadding">Margin Right: <br><input type="text" id="=right_margin_' . $number . '" name="right_margin_' . $number . '"></td>
</tr>
<tr>
<td></td>
<td>Margin Bottom: <br><input type="text" id="bottom_margin_' . $number . '" name="bottom_margin_' . $number . '"></td>
<td></td>
</tr>
</table>
';
$html .= '<style>
.box {
border: 3px solid black;
width: 150px;
height: 150px;
background-color: white;
margin-bottom: 0px;
}
input {
width: 100px;
}
.toppadding {
padding-top: 50px;
}
</style>';
return $html;
}
}
Now I'm of the opinion that I need to somehow pass the values that I can retrieve in the Media.php file to my HomepageMargin.php file, so that I can store the values in the textboxes within the html.
I'm also open to different ways of achieving this task, as I might not be following proper conventions here.
Thanks so much!