Php 在magento中创建管理表单字段之间的依赖关系无效

Php 在magento中创建管理表单字段之间的依赖关系无效,php,forms,magento-1.x,Php,Forms,Magento 1.x,这是一个管理表单,我在其中添加了一个自定义选项卡,以便向其中添加一些自定义字段。这个表格很好用。但是我需要为我的一些字段添加字段依赖项。我爱你 如果字段zipbasedprice\u isrange设置为yes,则我需要显示其他两个字段&如果设置为no,则只应显示一个字段 如何使用下面的表单实现此功能 字段相关性应介于zipbasedprice\u-isrange,zipbasedprice\u-zip,zipbasedprice\u-zip\u-from\u-zip和zipbasedprice

这是一个管理表单,我在其中添加了一个自定义选项卡,以便向其中添加一些自定义字段。这个表格很好用。但是我需要为我的一些字段添加
字段依赖项。我爱你

如果字段
zipbasedprice\u isrange
设置为
yes
,则我需要显示其他两个字段&如果设置为
no
,则只应显示一个字段

如何使用下面的表单实现此功能

字段相关性应介于
zipbasedprice\u-isrange
zipbasedprice\u-zip
zipbasedprice\u-zip\u-from\u-zip
zipbasedprice\u-zip\u-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();
}
公共函数getFormHtml(){
$form=新变量数据表格(
数组('id'=>'编辑表单','action'=>$this->getData('action'),'method'=>'post'))
);
$this->getLayout()->createBlock('adminhtml/widget\u form\u renderer\u element')。
$this->getLayout()->createBlock('adminhtml/widget\u form\u renderer\u fieldset')。
$this->getLayout()->createBlock('adminhtml/widget_表单_呈现器_字段集_元素');
$fieldset=$form->addFieldset('zipbasedprice_fields',array('legend'=>Mage::helper('zipbasedprice')->\uu('Zip-Based Price'))
);
$default_country=array('label'=>'IN','code'=>'India');
$fieldset->addField('zipbasedprice_country','select',数组(
'name'=>'zipbasedprice_country',
'label'=>Mage::helper('zipbasedprice')->,
'values'=>Mage::getModel('adminhtml/system\u config\u source\u country')->toOptionArray(),
“必需”=>true,
'样式'=>'宽度:275px',
“值”=>$default\u国家/地区,
“在元素之后”=>”

getResourceCollection()->addCountryFilter('IN')->load(); foreach($regionList as$region){$regions[$region['code']]=$region['default_name']];} $fieldset->addField('zipbasedprice_state','select',数组( 'name'=>'zipbasedprice_state', 'label'=>Mage::helper('zipbasedprice')->(Region/State'), “值”=>$regions, “必需”=>true, '样式'=>'宽度:275px', )); $isRange=$fieldset->addField('zipbasedprice\u isRange','select',数组( 'name'=>'zipbasedprice_isrange', 'label'=>Mage::helper('zipbasedprice')->, '值'=>数组( 排列( “值”=>false, 'label'=>Mage::helper('zipbasedprice')->, ), 排列( “值”=>true, 'label'=>Mage::helper('zipbasedprice')->, ) ), “值”=>false, “onchange”=>“onIsZipRangeChange()”, “必需”=>false, '样式'=>'宽度:275px', )); $fieldset->addField('zipbasedprice_-zip','text',数组( 'name'=>'zipbasedprice_-zip', 'label'=>Mage::helper('zipbasedprice')->(邮编), “类”=>“输入”, “必需”=>true, '样式'=>'宽度:268px', '值'=>'*', “maxlength”=>6, )); $fieldset->addField('zipbasedprice\u zip\u from\u zip','text',数组( 'name'=>'zipbasedprice\u-zip\u-from\u-zip', 'label'=>Mage::helper('zipbasedprice')->\uuu('Zip Code From'), “类”=>“输入”, “必需”=>true, '样式'=>'宽度:268px', '值'=>'*', “maxlength”=>6, )); $fieldset->addField('zipbasedprice\u zip\u to\u zip','text',数组( 'name'=>'zipbasedprice\u zip\u to\u zip', 'label'=>Mage::helper('zipbasedprice')->\uuu('Zip Code To'), “类”=>“输入”, “必需”=>true, '样式'=>'宽度:268px', '值'=>'*', “maxlength”=>6, )); $fieldset->addField('zipbasedprice','text',数组( “名称”=>“zipbasedprice”, 'label'=>Mage::helper('zipbasedprice')->, “类”=>“输入”, “必需”=>true, '样式'=>'宽度:268px', “值”=>“0.00”, )); $fieldset->addField('zipbasedprice_apply','select',数组( 'label'=>Mage::helper('zipbasedprice')->, 'name'=>'zipbasedprice_apply', “必需”=>false, '值'=>数组( 排列( '值'=>'固定', 'label'=>Mage::helper('zipbasedprice')->, ), 排列( “值”=>“百分比”, 'label'=>Mage::helper('zipbasedprice')->, ) ), “必需”=>1, “值”=>1, '样式'=>'宽度:275px', )); 返回$form->toHtml(); }


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

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


另一种替代解决方案如图所示。有不同的方法可以实现这一点。最简单的方法是向另一个字段所依赖的字段添加
onclick
属性。即

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


另一种替代解决方案如

所示。考虑此示例,仅当选择了指定的
选项时,才显示文本字段

$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'
);
依赖关系
–此节点包含当前字段与其他字段的依赖关系列表。这个节点的结构非常简单。子节点名称是的名称
$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')
);