1

I have a requirement that I need to create a field like so:

enter image description here

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!

0

I figured out the answer, I can pass in values to the $data variable in the constructor like so from my Media php file:

$fieldset->addType('homepagemargin', 'FactoryX_Homepage_Model_Varien_Data_Form_Element_HomepageMargin');
                $fieldset->addField(
                    'margin_'.$i,
                    'homepagemargin',
                    array(
                        'label'     => Mage::helper('homepage')->__('Margin: '),
                        'name'     => 'margin_'.$i,
                        'values'     => array(
                            'bottom_margin' => $data['bottom_margin_'.$i], 'top_margin' => $data['top_margin_'.$i], 'left_margin' => $data['left_margin_'.$i], 'right_margin' => $data['right_margin_'.$i])
                    )
                );

From here I can access them in my constructor:

public function __construct($data)
    {
        parent::__construct($data);
        $this->values = $data['values'];

    }

Then I can set the values in my getElementHtml function:

 public function getElementHtml()
    {
        $number = $this->getName();

        //returns digit at the end of string
        $number = substr($number, -1);
        $values = $this->values;
        $bottom = $values['bottom_margin'];
        $top = $values['top_margin'];
        $right = $values['right_margin'];
        $left = $values['left_margin'];

        $html = '';
        $html .= '<table>
            <tr>
                <td></td>
                <td align="center">Margin Top: <br><input type="text" value="' . $top . '" id="top_margin_' . $number . '" name="top_margin_' . $number . '"></td>
                <td></td>
            </tr>
            <tr>
                <td class="toppadding">Margin Left: <br><input type="text" value="' . $left . '" id="left_margin_' . $number . '" name="left_margin_' . $number . '"></td>
                <td><div class="box"></div></td>
                <td class="toppadding">Margin Right: <br><input type="text" value="' . $right . '" id="=right_margin_' . $number . '" name="right_margin_' . $number . '"></td>
            </tr>
            <tr>
                <td></td>
                <td align="center">Margin Bottom: <br><input type="text" value="' . $bottom . '" 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;
    }

Hope this is helpful to someone!

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.