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

0 php forms magento

这是一个管理表单,我在其中添加了一个自定义选项卡,以便向其中添加一些自定义字段。该表格工作正常。但我需要添加field dependency一些字段。

如果该字段zipbasedprice_isrange设置为yes,那么我需要显示其他两个字段;如果它设置为no,则只应显示一个字段。

我如何使用下面的表格来实现这一点?

字段依赖关系应位于zipbasedprice_isrange, zipbasedprice_zip, zipbasedprice_zip_from_zip&之间zipbasedprice_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();
}
Run Code Online (Sandbox Code Playgroud)

Sli*_*yyy 5

Specified考虑此示例,仅在选择该选项时显示文本字段。

\n\n
$form = new Varien_Data_Form();\n\n$form->addField('yesno', 'select', array(\n    'label'  => $this->__('Yes or No?'),\n    'values' => Mage::model('adminhtml/system_config_source_yesnocustom')\n        ->toOptionArray(),\n));\n\n$form->addField('custom_value', text, array(\n    'label'  => $this->__('Other'),\n));\n\n// Append dependency javascript\n$this->setChild('form_after', $this->getLayout()\n    ->createBlock('adminhtml/widget_form_element_dependence')\n        ->addFieldMap('yesno', 'yesno')\n        ->addFieldMap('custom_value', 'custom_value')\n        ->addFieldDependence('custom_value', 'yesno', 2) // 2 = 'Specified'\n);\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
<depends>\n     <field_name>1</field_name>\n</depends>\n
Run Code Online (Sandbox Code Playgroud)\n\n

上面将添加仅当字段值调用时显示当前字段的规则field_name等于1时显示当前字段的规则。

\n