在magento中创建管理表单字段之间的依赖关系不起作用

camsedfj  于 2022-11-12  发布在  其他
关注(0)|答案(3)|浏览(136)

这是一个管理表单,我在其中添加了一个自定义选项卡,以便添加一些自定义字段到它。该表单工作正常。但我需要为我的一些字段添加field dependency
如果字段zipbasedprice_isrange被设置为yes,那么我需要显示另外两个字段&如果它被设置为no,那么只应该显示一个字段。
我如何使用下面的表单实现这一点?
字段依赖关系应介于zipbasedprice_israngezipbasedprice_zipzipbasedprice_zip_from_zipzipbasedprice_zip_to_zip之间。

public function getFormHtml() {
        $form = new Varien_Data_Form(
            array('id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post')
        );

            $this->getLayout()->createBlock('adminhtml/widget_form_renderer_element').
            $this->getLayout()->createBlock('adminhtml/widget_form_renderer_fieldset').
            $this->getLayout()->createBlock('adminhtml/widget_form_renderer_fieldset_element');

    $fieldset = $form->addFieldset('zipbasedprice_fields', array('legend' => Mage::helper('zipbasedprice')->__('Zip Based Price'))
    );

    $default_country = array('label' => 'IN', 'code' => 'India');

    $fieldset->addField('zipbasedprice_country', 'select', array(
                    'name' => 'zipbasedprice_country',
                    'label' => Mage::helper('zipbasedprice')->__('Country'),
                    'values'    => Mage::getModel('adminhtml/system_config_source_country')->toOptionArray(),
                    'required' => true,
                    'style' => 'width:275px',
                    'value' => $default_country,
                    'after_element_html' => '<p class="zipbased_comment" style="margin: 0 150px; padding: 3px;"><img style="margin-right: 4px;" src="http://zonepricing.innoexts.com/skin/adminhtml/default/default/images/note_bg.gif" /><small>select the country to apply price</small></p>',
         ));

    $regions = array();
    $regions['*'] = '*';
$regionList = Mage::getModel('directory/region')->getResourceCollection()->addCountryFilter('IN')->load();
foreach($regionList as $region){ $regions[$region['code']] = $region['default_name']; }

    $fieldset->addField('zipbasedprice_state', 'select', array(
        'name' => 'zipbasedprice_state',
        'label' => Mage::helper('zipbasedprice')->__('Region/State'),
        'values' => $regions,
        'required' => true,
        'style' => 'width:275px',
      ));

   $isRange = $fieldset->addField('zipbasedprice_isrange', 'select', array(
        'name' => 'zipbasedprice_isrange',
        'label' => Mage::helper('zipbasedprice')->__('Is Range?'),
        'values' => array(
            array(
                'value' => false,
                'label' => Mage::helper('zipbasedprice')->__('No'),
            ),
            array(
                'value' => true,
                'label' => Mage::helper('zipbasedprice')->__('Yes'),
            )
        ),
       'value' => false,
       'onchange' => 'onIsZipRangeChange()',
       'required' => false,
   'style' => 'width:275px',
      ));

    $fieldset->addField('zipbasedprice_zip', 'text', array(
        'name' => 'zipbasedprice_zip',
        'label' => Mage::helper('zipbasedprice')->__('Zip Code'),
        'class' => 'input',
        'required' => true,
    'style' => 'width:268px',
        'value' => '*',
        'maxlength' => 6,
     ));

     $fieldset->addField('zipbasedprice_zip_from_zip', 'text', array(
        'name' => 'zipbasedprice_zip_from_zip',
        'label' => Mage::helper('zipbasedprice')->__('Zip Code From'),
        'class' => 'input',
        'required' => true,
    'style' => 'width:268px',
        'value' => '*',
        'maxlength' => 6,
     ));

      $fieldset->addField('zipbasedprice_zip_to_zip', 'text', array(
        'name' => 'zipbasedprice_zip_to_zip',
        'label' => Mage::helper('zipbasedprice')->__('Zip Code To'),
        'class' => 'input',
        'required' => true,
    'style' => 'width:268px',
        'value' => '*',
        'maxlength' => 6,
     ));

    $fieldset->addField('zipbasedprice_price', 'text', array(
        'name' => 'zipbasedprice_price',
        'label' => Mage::helper('zipbasedprice')->__('Price'),
        'class' => 'input',
        'required' => true,
        'style' => 'width:268px',
        'value' => '0.00',
     ));

    $fieldset->addField('zipbasedprice_apply', 'select', array(
        'label' => Mage::helper('zipbasedprice')->__('Apply'),
        'name' => 'zipbasedprice_apply',
        'required' => false,
        'values' => array(
            array(
                'value' => 'fixed',
                'label' => Mage::helper('zipbasedprice')->__('Fixed'),
            ),
            array(
                'value' => 'percentage',
                'label' => Mage::helper('zipbasedprice')->__('Percentage'),
            )
        ),
        'required' => 1,
        'value' => 1,
        'style' => 'width:275px',
    ));

    return $form->toHtml();
}
j2qf4p5b

j2qf4p5b1#

请考虑以下示例,该示例仅在选择了Specified选项时显示文本字段。

$form = new Varien_Data_Form();

$form->addField('yesno', 'select', array(
    'label'  => $this->__('Yes or No?'),
    'values' => Mage::model('adminhtml/system_config_source_yesnocustom')
        ->toOptionArray(),
));

$form->addField('custom_value', text, array(
    'label'  => $this->__('Other'),
));

// Append dependency javascript
$this->setChild('form_after', $this->getLayout()
    ->createBlock('adminhtml/widget_form_element_dependence')
        ->addFieldMap('yesno', 'yesno')
        ->addFieldMap('custom_value', 'custom_value')
        ->addFieldDependence('custom_value', 'yesno', 2) // 2 = 'Specified'
);

depends-该节点包含当前字段对其他字段的依赖列表,该节点的结构非常简单,子节点名称是该字段依赖的字段名称,节点值是使该字段可见的值,例如这样的配置:

<depends>
     <field_name>1</field_name>
</depends>

以上将添加规则,仅当名为field_name的字段的值等于1时,才显示当前字段。

hfsqlsce

hfsqlsce2#

我想从@Slimshadddyyy扩展答案,再举一个有多个依赖值的例子。

$fieldset->addField("transfer_interval", "select", array(
    "label" => Mage::helper("core")->__("Transfer Interval"),
    "name" => "transfer_interval",
    "class" => "required-entry",
    'values' => array(
        array(
            'value' => 'daily',
            'label' => Mage::helper('core')->__('Daily'),
        ),
        array(
            'value' => 'weekly',
            'label' => Mage::helper('core')->__('Weekly'),
        ),
        array(
            'value' => 'monthly',
            'label' => Mage::helper('core')->__('Monthly'),
        )
    ),
    "required" => true,
));

$fieldset->addField("transfer_day_weekly", "select", array(
    "label" => Mage::helper("core")->__("Transfer Day"),
    "name" => "transfer_day_weekly",
    "class" => "required-entry",
    'values' => $this->getWeekDays(), // Pay attention here you need to change this for your array values
    "required" => true,
));

$fieldset->addField("transfer_day_monthly", "select", array(
    "label" => Mage::helper("core")->__("Transfer Day"),
    "name" => "transfer_day_monthly",
    "class" => "required-entry",
    'values' => $this->getMontlyDays(), // Pay attention here you need to change this for your array values
    "required" => true,
));

// Append dependency javascript
$this->setChild('form_after', $this->getLayout()
    ->createBlock('adminhtml/widget_form_element_dependence')
    ->addFieldMap('transfer_interval', 'transfer_interval') // Putting fields for mapping
    ->addFieldMap('transfer_day_weekly', 'transfer_day_weekly')
    ->addFieldMap('transfer_day_monthly', 'transfer_day_monthly')
    ->addFieldDependence('transfer_day_weekly', 'transfer_interval', 'weekly') // Pay attetion below as you can add more than one dependence
    ->addFieldDependence('transfer_day_monthly', 'transfer_interval', 'monthly')
);

在本例中,当您选择“每周”时,它将显示带有每周选项的字段,因此如果您选择“每月”选项。

ukdjmx9f

ukdjmx9f3#

有不同的方法可以做到这一点。最容易和最简单的方法是将一个onclick属性添加到另一个字段所依赖的字段中。即

$fieldset->addField('zipbasedprice_isrange', 'select', array(
    'name' => 'zipbasedprice_isrange',
    'label' => Mage::helper('zipbasedprice')->__('Is Range?'),
    ......
    'onclick' => someFunction();

然后定义someFunction()以实现所需功能。
this thread中显示了另一种替代解决方案

相关问题