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::hel开发者_开发技巧per('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
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.
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,
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.
精彩评论