【问题标题】:Creating dependency between admin form fields in magento does not work在 magento 中创建管理表单字段之间的依赖关系不起作用
【发布时间】:2014-10-15 20:48:48
【问题描述】:

这是一个管理表单,我在其中添加了一个自定义选项卡,以便向其中添加一些自定义字段。表格工作正常。但我需要为我的一些字段添加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();
}

【问题讨论】:

    标签: php forms magento


    【解决方案1】:

    考虑此示例仅在选择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时才显示当前字段。

    【讨论】:

    • 这对我来说是一个新信息。为此+1。我们应该在哪里添加依赖节点?在布局文件中?
    • 这是一个解释。
    • 好的。该代码对我不起作用。我检查了表单中的代码,一切似乎都很好,但仍然无法在我的表单中实现字段依赖。有什么建议吗?
    【解决方案2】:

    我想扩展@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')
    );
    

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

    【讨论】:

      【解决方案3】:

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

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

      然后定义 someFunction() 以实现您的需要。

      this thread 中显示了另一种替代解决方案

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-09
        • 2021-12-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多