1.在 Magento 中, 表单(form)可以被分为4个基本部分
1、 表单容器 => FORM Container
2、 表单 => FORM
3、 表单选项卡 => FORM Tabs
4、 真实的表单内容 => Actual Form Fields
public function newAction()
{
$this->loadLayout();
$this->_addContent($this->getLayout()->createBlock('employee/adminhtml_employee_edit'))
->_addLeft('employee/adminhtml_employee_edit_tabs');
$this->renderLayout();
}
这里可以直接看出我们在 content 和 left 区域里添加了两个 block, 主要的表单容器被加进了 content 区域,而表单选项卡被加进了 left 区域
2.表单容器
表单容器就是一个大的 div 框, 它包含了表单的所有元素和 html 代码, 为了创建一个容器, 你需要在 Block/Adminhtml/employee/ 文件下创一个 php 文件,并继承了 Mage_Adminhtml_Block_Widget_Form_Container 类
我们就把文件名命为: Edit.php
<?php
class Www_Employee_Block_Adminhtml_Employee_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
public function __construct()
{
parent::__construct();
//这个变量被应用于表单的 URL 中, 它包含了表单实体的主键
//拿删除按钮的 URL 来说: 模块/控制器/方法名/$this->_objectid/3
$this->_objectId = 'id';
//这两个变量比较重要,它们就是用来锁定表单选项卡(tabs)文件的,
//其路径应该是 {$this->_blockGroup . '/' . $this->_controller . '_' . $this->_mode . '_form'},
//当中的$this->mode含有默认值: 'edit', 那么展现出的路径是: 'employee/adminhtml_employee_edit_form'
$this->_blockGroup = 'employee';
$this->_controller = 'adminhtml_employee';
//$this->_updateButton('save','label',Mage::helper('employee')->__('Save'));
//$this->_updateButton('delete','label',Mage::helper('employee')->__('Delete'));
//$this->removeButton('reset');
$this->_addButton('saveandcontinue', array(
'label' => Mage::helper('adminhtml')->__('Save And Continue Edit'),
'onclick' => 'saveAndContinueEdit()',
'class' => 'save',
),
-100
);
}
//这个方法返回出我们想在表单头部展示出的标题, 在上图已用红色框出
public function getHeaderText()
{
return Mage::helper('employee')->__('My Form Container');
}
}
3.表单
这个 Block/Adminhtml/Employee/Edit/Form.php 文件包含真实的 <form> 标签, 代码如下:
<?php
class Www_Employee_Block_Adminhtml_Employee_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();
}
}
4.表单选项卡
<?php
class Www_Employee_Block_Adminhtml_Employee_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{
public function __construct()
{
$this->setId('employee_tabs');
$this->setDestElementId('edit_form');
$this->setTitle(Mage::helper('employee')->__('Employee Information'));
return parent::__construct();
}
protected function _beforeToHtml()
{
$this->addTab('form_section', array(
'label' => Mage::helper('employee')->__('Information'),
'title' => Mage::helper('employee')->__('Employee Information'),
'content' => $this->getLayout()
->createBlock('employee/adminhtml_employee_edit_tab_form')
->toHtml(),)
);
return parent::_beforeToHtml();
}
}
5.真实的表单内容
<?php
class Www_Employee_Block_Adminhtml_Employee_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$model = Mage::registry('web_data');
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('employee_form', array(
'legend' => Mage::helper('employee')->__('Employee information')
)
);
$fieldset->addField('title', 'text', array(
'label' => Mage::helper('employee')->__('Title'),
'class' => 'required-entry',
'required' => true,
'name' => 'title',
)
);
$fieldset->addField('active', 'select', array(
'label' => Mage::helper('employee')->__('Enabled:'),
'name' => 'active',
'class' => 'required-entry',
'required' => true,
'values' => array(
array(
'value' => 1,
'label' => Mage::helper('employee')->__('Yes'),
),
array(
'value' => 0,
'label' => Mage::helper('employee')->__('No'),
),
),
));
$form->setValues($model->getData());
return parent::_prepareForm();
}
}
http://www.sunzhenghua.com/magento-admin-module-development-part3-grid-forms-tabs-updatebutton