Today I tried to create a simple edit form field in Magento.
But I have a problem, all form fields are appear under form tabs (form fields appear in left sidebar, below form tabs instead of on the right) I've googled and found some people who had same problem like me, but their solutions didn't fix my problem.
Unable to add form in content section magento admin custom extension
admin edit form content showing in left
This is my code
Form container (Edit.php)
class Magestore_GiftCard_Block_Adminhtml_GiftCard_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
public function __construct()
{
parent::_construct();
$this->_objectId = 'id';
$this->_blockGroup = 'giftcard';
$this->_controller = 'adminhtml_index';
$this->_updateButton('save', 'label', Mage::helper('giftcard')->__('Save'));
$this->_updateButton('delete', 'label', Mage::helper('giftcard')->__('Delete'));
$this->_addButton('saveandcontinue', array(
'label' => Mage::helper('adminhtml')->__('Save And Continue Edit'),
'onclick' => 'saveAndContinueEdit()',
'class' => 'save',
), -100);
}
public function getHeaderText()
{
if (Mage::registry('giftcard_data') && Mage::registry('giftcard_data')->getId()) {
return 'Editer la reference ' . $this->htmlEscape(
Mage::registry('giftcard_data')->getTitle()) . '<br />';
} else {
return 'Add a gift card';
}
}
}
Form tag (Edit/Form.php)
class Magestore_GiftCard_Block_Adminhtml_GiftCard_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form(array(
'id'=> 'edit_form',
'action'=> $this->getUrl('*/*/save', array('id'=> $this->getRequest()->getParam('id'))),
'method'=> 'post',
'enctype'=> 'multipart/form-data'
));
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}
Form tabs (Edit/Tabs.php)
class Magestore_GiftCard_Block_Adminhtml_GiftCard_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{
public function __construct()
{
parent::__construct();
$this->setId('form_tabs');
$this->setDestElementId('edit_form');
$this->setTitle(Mage::helper('giftcard')->__('Gift Card Information'));
}
protected function _beforeToHtml()
{
$this->addTab('form_section', array(
'label' => Mage::helper('giftcard')->__('General'),
'title' => Mage::helper('giftcard')->__('Item Information'),
'content' => $this->getLayout()->createBlock('giftcard/adminhtml_giftcard_edit_tab_form')->toHtml()
));
return parent::_beforeToHtml();
}
}
Form fields (Edit/Tab/Form.php)
class Magestore_GiftCard_Block_Adminhtml_GiftCard_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('giftcard_form', array('legend' => Mage::helper('giftcard')->__('General')));
$fieldset->addField('gift_code', 'text', array(
'label' => Mage::helper('giftcard')->__('Gift Code'),
'class' => 'required-entry',
'required' => true,
'name' => 'gift_code',
));
$fieldset->addField('gift_code_value', 'text', array(
'label' => Mage::helper('giftcard')->__('Gift Code Value'),
'class' => 'required-entry',
'required' => true,
'name' => 'gift_code_value',
));
$fieldset->addField('status', 'text', array(
'label' => Mage::helper('giftcard')->__('Status'),
'class' => 'required-entry',
'required' => true,
'name' => 'status',
));
$dateFormatIso = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
$fieldset->addField('expired_time', 'date', array(
'label' => Mage::helper('giftcard')->__('Expired Time'),
'class' => 'required-entry',
'required' => true,
'name' => 'expired_time',
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'input_format' => $dateFormatIso,
'format' => $dateFormatIso,
'time' => true
));
$fieldset->addField('notes', 'text', array(
'label' => Mage::helper('giftcard')->__('Notes'),
'class' => 'required-entry',
'required' => true,
'name' => 'notes',
));
if (Mage::getSingleton('adminhtml/session')->getGiftCardData()) {
$form->setValues(Mage::getSingleton('adminhtml/session')->getGiftCardData());
Mage::getSingleton('adminhtml/session')->setGiftCardData(null);
} elseif (Mage::registry('giftcard_data')) {
$form->setValues(Mage::registry('giftcard_data')->getData());
}
return parent::_prepareForm();
}
}
Is there anything wrong? After googling for half a day, I still can't find the solution.
Thank you very much.
EDIT 1
This is my controller for editAction
public function editAction()
{
$id = $this->getRequest()->getParam("id");
$model = Mage::getModel("giftcard/giftcard")->load($id);
if ($model->getId() || $id == 0) {
$data = Mage::getSingleton('adminhtml/session')->getFormData(true);
if (!empty($data)) {
$model->setData($data);
}
Mage::register("giftcard_data", $model);
$this->loadLayout();
$this->_setActiveMenu("giftcard/items");
$this->_addBreadcrumb(Mage::helper("adminhtml")->__('Item Manager'), Mage::helper('adminhtml')->__('Item Manager'));
$this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));
$this->_addContent($this->getLayout()
->createBlock('giftcard/adminhtml_giftcard_edit'))
->_addLeft($this->getLayout()
->createBlock('giftcard/adminhtml_giftcard_edit_tabs')
);
$this->renderLayout();
} else {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('giftcard')->__('Gift card does not exist'));
$this->_redirect('*/*/');
}
}