gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:20:19 25 4
gpt4 key购买 nike

嗨伙计们,我对自定义模块的 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的action方法

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')) 因为当表单被渲染时我看到了表单 Action Action 作为/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'
)
);

但它仍然以/s​​ave 而非/import 的形式显示 Action 。谁能帮我解决这个问题


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

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

谢谢,请帮帮我

最佳答案

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

在您的 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'

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

默认模式是编辑:

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 block ,而不是您的 block 。

干杯。

关于forms - 管理面板 magento 的表单操作问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5619979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com