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

SAM*_*SAM 2 forms admin panel magento

嗨大家我有一个问题与由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
Run Code Online (Sandbox Code Playgroud)

这是我的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();
}
Run Code Online (Sandbox Code Playgroud)

这是我的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');

}
Run Code Online (Sandbox Code Playgroud)

}

这是我的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();
}
 }
Run Code Online (Sandbox Code Playgroud)

这是我的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();
}
}
Run Code Online (Sandbox Code Playgroud)

最后这是我的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();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,不会调用最后一个文件.正如我$this->_addContent($this->getLayout()->createBlock('zipcodes/adminhtml_importblock_edit'))在代码中使用的那样,当表单被渲染时,我将表单操作操作视为/ save而不是/ import

所以我改变了Block_Adminhtml_Importblock_Edit_Tab_Form并写了

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

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


伙计们,我找到了一条线索

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

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

Flo*_*his 5

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

在你的Namespace_Zipcodes_Block_Adminhtml_Importblock_Edit课堂上你有:

$this->_objectId = 'id';
$this->_blockGroup = 'zipcodes';
$this->_controller = 'adminhtml_zipcodes';
Run Code Online (Sandbox Code Playgroud)

但是有一处遗产: $this->_mode

设置$this->_mode'import'将帮助您获得正确的操作.

$this->_objectId = 'id';
$this->_blockGroup = 'zipcodes';
$this->_controller = 'adminhtml_zipcodes';
$this->_mode       = 'import'
Run Code Online (Sandbox Code Playgroud)

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

默认模式是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';
Run Code Online (Sandbox Code Playgroud)

_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();
}
Run Code Online (Sandbox Code Playgroud)

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

干杯.