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

发布于 2024-10-31 07:09:21 字数 5085 浏览 4 评论 0原文

你好 伙计们,我对由 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();
}
}

&最后这是

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();
    }
}

我运行代码时的 Block_Adminhtml_Importblock_Edit_Form.php 最后一个文件不会被调用。因为我在代码中使用 $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'
        )
    );

但它仍然显示表单操作为/保存而不是/导入。任何人都可以帮我解决这个


问题吗?我发现了更多线索,

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

谢谢,请它如此接近帮助我

hi
Guys I have a problem with form action of a form generated by admin panel of magento for a custom module.

This is the structure of my files under 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

This is my action method of 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();
}

This is my 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');

}

}

This is my 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();
}
 }

this is my 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();
}
}

& last this is my 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();
    }
}

when I run the code The last file doesn't gets called. as I am using $this->_addContent($this->getLayout()->createBlock('zipcodes/adminhtml_importblock_edit')) in code due to this when form gets rendered I see the form action action as /save instead of /import

So I changed the Block_Adminhtml_Importblock_Edit_Tab_Form & wrote

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

But still its showing form action as /save not /import. Can anybody help me with this


Guys I have found one more clue

the file under Adminhtml/Zipcodes/Edit/Form.php is getting called in importblock's form
thats why the action is not getting set at runtime.
Now can anyone help me how to remove this error & make the correct reference to Adminhtml/Importblock/Edit/Form.php

Thanks Please its so close help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

[旋木] 2024-11-07 07:09:21

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

在您的 Namespace_Zipcodes_Block_Adminhtml_Importblock_Edit 类中,您有:

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

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

$this->_mode 设置为 < code>'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 块,而不是您的块。

干杯。

This comes a bit late but might be helpful for others that get this issue.

In your Namespace_Zipcodes_Block_Adminhtml_Importblock_Edit class you have:

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

but there is a property missing: $this->_mode

Setting $this->_mode to 'import' will help you get the right action.

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

After changing this you will get the right action for your form.

The default mode is 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';

the the function for _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();
}

As you can see instead of your block, default you get the edit_form block.

Cheers.

苍景流年 2024-11-07 07:09:21

在您的 Namespace_Zipcodes_Block_Adminhtml_Importblock_Edit 文件中,将此行:更改

$this->_controller = 'adminhtml_zipcodes';

$this->_controller = 'adminhtml_zipcodes_import';

这样应该可以解决问题。

问候,

In your Namespace_Zipcodes_Block_Adminhtml_Importblock_Edit file, change this line:

$this->_controller = 'adminhtml_zipcodes';

to

$this->_controller = 'adminhtml_zipcodes_import';

That should do the trick.

Regards,

笑红尘 2024-11-07 07:09:21

在调用 setForm() 之前添加此行:

   `$form->setUseContainer(true);` 

这是此块的 _data 数组的 useContainer 参数的神奇设置器。它用于告诉管理表单块小部件打印出具有正确操作 URL 的标签,以及用于验证会话的隐藏输入。Varien,如果您正在监听,请默认设置此键为 true。通常,对于表单小部件,您还需要一个表单标签。

Add this line before calling setForm():

   `$form->setUseContainer(true);` 

This is a magic setter for the useContainer parameter of the _data array for this block. It's used to tell the admin form block widget to print out a tag with the correct action url, as well as the hidden input used to verify the session.Varien, if you're listening, make this key true by default. Usually, with a form widget, you'd want a form tag to go with it as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文