管理面板magento的表单操作问题

时间:2011-04-11 10:27:47

标签: forms magento panel admin

喜 伙计我对自定义模块的magento管理面板生成的表单的表单操作有问题。

这是我的文件在app / code / local / Namespace / Zipcodes / Block

下的结构
Block
|
|____Adminhtml
    |
    |____ Importblock
        |
        |__Edit
        |   |__Form.php
        |   |__Tabs.php
        |   |
        |   |__Tab
        |       |___Form.php
        |
        |__Edit.php
        |
        |
        Zipcodes
        |
        |__Edit
        |   |__Form.php   // << this file is getting called in importblock form
        |   |__Tabs.php
        |   |
        |   |__Tab
        |       |___Form.php
        |
        |__Edit.php

这是我的ZipcodesController.php

的动作方法
public function importAction()
{

    if ($data = $this->getRequest()->getPost() && isset($_FILES['csv_file']['name']) )
    {
        echo '<br> hi ! we  uploaded the file';
    }
    $this->_initAction();

    $this->_addContent($this->getLayout()->createBlock('zipcodes/adminhtml_importblock_edit'))
          ->_addLeft($this->getLayout()->createBlock('zipcodes/adminhtml_importblock_edit_tabs'));

    $this->renderLayout();
}

这是我的Block / Adminhtml / Importblock / Edit.php

<?php 
  class Namespace_Zipcodes_Block_Adminhtml_Importblock_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
    public function __construct()
   {
    parent::__construct();
    $this->_objectId = 'id';
    $this->_blockGroup = 'zipcodes';
    $this->_controller = 'adminhtml_zipcodes';
    $this->_updateButton('save', 'label', Mage::helper('zipcodes')->__('Upload file'));

}

public function getHeaderText()
{   
    return Mage::helper('zipcodes')->__('Import Zipcode data');

}

}

这是我的Block / Adminhtml / Importblock / Edit / Tab / Form.php

class Namespace_Zipcodes_Block_Adminhtml_Importblock_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
      {

    $form = new Varien_Data_Form(array(
            'id' => 'edit_form',
            'action' => $this->getUrl('*/*/import'),
            'method' => 'post',
            'enctype' => 'multipart/form-data'
        )
    );
    $this->setForm($form);
    //echo '<br>form.php bahar<pre>';print_r(get_class_methods(get_class($form))); echo '</pre>';
    $fieldset = $form->addFieldset('zipcodes_form', array('legend'
                    => Mage::helper('zipcodes')->__('Provide data file')));

    $fieldset->addField('csv_file', 'file', array(
                    'label' => Mage::helper('zipcodes')->__('CSV File'),
                    'class' => 'required-entry',
                    'required' => true,
                    'name' => 'csv_file',
            ));

    return parent::_prepareForm();
}
 }

这是我的Block / Adminhtml / Importblock / Edit / Tabs.php

class Namespace_Zipcodes_Block_Adminhtml_Importblock_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{
public function __construct()
{
    parent::__construct();
    $this->setId('zipcode_import_tabs');
    $this->setDestElementId('edit_form');
    $this->setTitle(Mage::helper('zipcodes')->__('Import Zipcodes'));
}

protected function _beforeToHtml()
{
    $this->addTab('form_section', array(
            'label' => Mage::helper('zipcodes')->__('Zipcode Info'),
            'title' => Mage::helper('zipcodes')->__('Zipcode Info'),
            'content' => $this->getLayout()
                ->createBlock('zipcodes/adminhtml_importblock_edit_tab_form')->toHtml(),
            'active'    => true
        ));

    return parent::_beforeToHtml();
}
}

&安培;最后这是我的Block_Adminhtml_Importblock_Edit_Form.php

class Namespace_Zipcodes_Block_Adminhtml_Importblock_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form(array(
                'id' => 'edit_form',
                'action' => $this->getUrl('*/*/import'),
                'method' => 'post',
            )
        );

        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

当我运行代码时,不会调用最后一个文件。因为我在代码中使用$this->_addContent($this->getLayout()->createBlock('zipcodes/adminhtml_importblock_edit'))因此,当表单被呈现时,我将表单操作操作视为/ save而不是/ import

所以我改变了Block_Adminhtml_Importblock_Edit_Tab_Form&amp;写道

$form = new Varien_Data_Form(array(
            'id' => 'edit_form',
            'action' => $this->getUrl('*/*/import'),
            'method' => 'post',
            'enctype' => 'multipart/form-data'
        )
    );

但仍然显示表单操作为/ save not / import。任何人都可以帮我这个


伙计们我找到了一条线索

Adminhtml / Zipcodes / Edit / Form.php下的文件将以importblock的形式调用 这就是为什么动作没有在运行时设置。 现在任何人都可以帮助我如何删除此错误&amp;正确引用Adminhtml / Importblock / Edit / Form.php

谢谢请它如此近距离帮助我

3 个答案:

答案 0 :(得分:5)

这有点晚了,但对于遇到此问题的其他人可能会有帮助。

在您的Namespace_Zipcodes_Block_Adminhtml_Importblock_Edit课程中:

$this->_objectId = 'id';
$this->_blockGroup = 'zipcodes';
$this->_controller = 'adminhtml_zipcodes';

但缺少某个属性:$this->_mode

$this->_mode设置为'import'将有助于您采取正确的行动。

$this->_objectId = 'id';
$this->_blockGroup = 'zipcodes';
$this->_controller = 'adminhtml_zipcodes';
$this->_mode       = 'import'

更改此设置后,您将获得表单的正确操作。

默认模式为edit

class Mage_Adminhtml_Block_Widget_Form_Container extends     Mage_Adminhtml_Block_Widget_Container
{
protected $_objectId = 'id';
protected $_formScripts = array();
protected $_formInitScripts = array();
protected $_mode = 'edit';
protected $_blockGroup = 'adminhtml';

_prepareLayout()函数:

protected function _prepareLayout()
{
    if ($this->_blockGroup && $this->_controller && $this->_mode) {
        $this->setChild('form', $this->getLayout()->createBlock($this->_blockGroup . '/' . $this->_controller . '_' . $this->_mode . '_form'));
    }
    return parent::_prepareLayout();
}

正如您所看到的那样,默认情况下您会看到edit_form块。

干杯。

答案 1 :(得分:2)

在Namespace_Zipcodes_Block_Adminhtml_Importblock_Edit文件中,更改此行:

$this->_controller = 'adminhtml_zipcodes';

$this->_controller = 'adminhtml_zipcodes_import';

这应该可以解决问题。

此致

答案 2 :(得分:1)

在致电setForm():

之前添加此行
   `$form->setUseContainer(true);` 

这是此块的_data数组的useContainer参数的魔术设置器。它用于告诉管理表单块小部件打印出带有正确操作URL的标记,以及用于验证session的隐藏输入。如果您正在侦听,则默认情况下将此键设置为true。通常,使用表单窗口小部件,您还需要一个表单标记。