上次写到的Magento后台模块,在Block/Adminhtml/Count/Edit/Tab/Form.php中,有这样如下代码:
protected function _prepareForm()
{
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('count_form', array('legend'=>Mage::helper('count')->__('Item information')));
$fieldset->addField('Model_Name', 'text', array(
'label' => Mage::helper('count')->__('Model_Name'),
'class' => 'required-entry',
'required' => true,
'name' => 'Model_Name',
));
$fieldset->addField('Name', 'text', array(
'label' => Mage::helper('count')->__('Name'),
'class' => 'required-entry',
'required' => true,
'name' => 'Name',
));
$fieldset->addField('Serial_Number', 'text', array(
'label' => Mage::helper('count')->__('Serial_Number'),
'class' => 'required-entry',
'required' => true,
'name' => 'Serial_Number',
));
$fieldset->addField('Email', 'text', array(
'label' => Mage::helper('count')->__('Email'),
'class' => 'required-entry',
'required' => true,
'name' => 'Email',
));
$fieldset->addField('Date_Purchased', 'date', array(
'label' => Mage::helper('count')->__('Date_Purchased'),
'class' => 'required-entry',
'required' => true,
'name' => 'Data_Purchased',
'format' => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
));
$fieldset->addField('Messages_Source', 'select', array(
'label' => Mage::helper('count')->__('Messages_Source'),
'class' => 'required-entry',
'required' => true,
'name' => 'Messages_Source',
'values' => array(
array(
'value' => 1,
'label' => Mage::helper('count')->__('web Search'),
),
array(
'value' => 2,
'label' => Mage::helper('count')->__('Online Review'),
),
array(
'value' => 3,
'label' => Mage::helper('count')->__('Online Ad'),
),
array(
'value' => 4,
'label' => Mage::helper('count')->__('Other'),
),
),
));
$fieldset->addField('Content', 'textarea', array(
'label' => Mage::helper('count')->__('Content'),
'title' => Mage::helper('count')->__('Content'),
'class' => 'required-entry',
'required' => true,
'style' => 'width:278px; height:60px;',
'name' => 'Content',
'wysiwyg' => false,
));
if ( Mage::getSingleton('adminhtml/session')->getcountData() )
{
$form->setValues(Mage::getSingleton('adminhtml/session')->getcountData());
Mage::getSingleton('adminhtml/session')->setcountData(null);
} elseif ( Mage::registry('count_data') ) {
$form->setValues(Mage::registry('count_data')->getData());
}
return parent::_prepareForm();
}
对应的后台页面如图所示:
其中用到$fieldset->addField(),我们来查看Magento的源代码,追踪,可以看到在Fieldset.php中有如下代码:
public function addField($elementId, $type, $config, $after=false)
{
$element = parent::addField($elementId, $type, $config, $after);
if ($renderer = Varien_Data_Form::getFieldsetElementRenderer()) {
$element->setRenderer($renderer);
}
return $element;
}
$type即表示对应的类型,那么Magento的Form表单元素又有哪些添加方法呢?
1.input文本类型 type=text
$fieldset->addField('title', 'text', array(
'label' => Mage::helper('form')->__('Title′),
'class' => 'required-entry',
'required' => true,
'name' => 'title',
'onclick' => “alert('on click');”,
'onchange' => “alert('on change');”,
'style' => “border:10px”,
'value' => 'hello !!',
'disabled' => false,
'readonly' => true,
'after_element_html' => 'Comments',
'tabindex' => 1
));
2.Time时间类型 type=time
$fieldset->addField('time', 'time', array(
'label' => Mage::helper('form')->__('Time'),
'class' => 'required-entry',
'required' => true,
'name' => 'title',
'onclick' => "",
'onchange' => "",
'value' => '12,04,15′,
'disabled' => false,
'readonly' => false,
'after_element_html' => ”,
'tabindex' => 1
));
3.文本区域类型 type=textarea
$fieldset->addField('textarea', 'textarea', array(
'label' => Mage::helper('form')->__('TextArea'),
'class' => 'required-entry',
'required' => true,
'name' => 'title',
'onclick' => "",
'onchange' => "",
'value' => '<b><b/>',
'disabled' => false,
'readonly' => false,
'after_element_html' => '<small>Comments</small>',
'tabindex' => 1
));
4.input按钮类型 type=submit
$fieldset->addField('submit', 'submit', array(
'label' => Mage::helper('form')->__('Submit'),
'required' => true,
'value' => 'Submit',
'after_element_html' => ”,
'tabindex' => 1
));
5.下拉框选项类型 type=select option
$fieldset->addField('select', 'select', array(
'label' => Mage::helper('form')->__('Select'),
'class' => 'required-entry',
'required' => true,
'name' => 'title',
'onclick' => "",
'onchange' => "",
'value' => '1′,
'values' => array('-1′=>'Please Select..','1′ => 'Option1′,'2′ => 'Option2′, '3′ => 'Option3′),
'disabled' => false,
'readonly' => false,
'after_element_html' => ”,
'tabindex' => 1
));
6.带optgroup的select
$fieldset->addField('select2′, 'select', array(
'label' => Mage::helper('form')->__('Select Type2′),
'class' => 'required-entry',
'required' => true,
'name' => 'title',
'onclick' => "",
'onchange' => "",
'value' => '4′,
'values' => array(
'-1′=>'Please Select..',
'1′ => array(
'value'=> array(array('value'=>'2′ , 'label' => 'Option2′) , array
('value'=>'3′ , 'label' =>'Option3′) ),
'label' => 'Size'
),
'2′ => array(
'value'=> array(array('value'=>'4′ , 'label' => 'Option4′) , array
('value'=>'5′ , 'label' =>'Option5′) ),
'label' => 'Color'
),
),
'disabled' => false,
'readonly' => false,
'after_element_html' => ”,
'tabindex' => 1
));
7.单选框input type=radio
$fieldset->addField('radio', 'radio', array(
'label' => Mage::helper('form')->__('Radio'),
'name' => 'title',
'onclick' => "",
'onchange' => "",
'value' => '1′,
'disabled' => false,
'readonly' => false,
'after_element_html' => ”,
'tabindex' => 1
));
$fieldset->addField('radio2′, 'radios', array(
'label' => Mage::helper('form')->__('Radios'),
'name' => 'title',
'onclick' => "",
'onchange' => "",
'value' => '2′,
'values' => array(
array('value'=>'1′,'label'=>'Radio1′),
array('value'=>'2′,'label'=>'Radio2′),
array('value'=>'3′,'label'=>'Radio3′),
),
'disabled' => false,
'readonly' => false,
'after_element_html' => ”,
'tabindex' => 1
));
8.Password元素 input type=password
$fieldset->addField('password', 'password', array(
'label' => Mage::helper('form')->__('Password'),
'class' => 'required-entry',
'required' => true,
'name' => 'title',
'onclick' => "",
'onchange' => "",
'style' => "",
'value' => 'hello !!',
'disabled' => false,
'readonly' => false,
'after_element_html' => ”,
'tabindex' => 1
));
$fieldset->addField('obscure', 'obscure', array(
'label' => Mage::helper('form')->__('Obscure'),
'class' => 'required-entry',
'required' => true,
'name' => 'obscure',
'onclick' => "",
'onchange' => "",
'style' => "",
'value' => '123456789′,
'after_element_html' => ”,
'tabindex' => 1
));
9.Note
$fieldset->addField('note', 'note', array(
'text' => Mage::helper('form')->__('Text Text'),
));
10.复选框 type=Multiselect
$fieldset->addField('multiselect2′, 'multiselect', array(
'label' => Mage::helper('form')->__('Select Type2′),
'class' => 'required-entry',
'required' => true,
'name' => 'title',
'onclick' => “return false;”,
'onchange' => “return false;”,
'value' => '4′,
'values' => array(
'-1′=> array( 'label' => 'Please Select..', 'value' => '-1′),
'1′ => array(
'value'=> array(array('value'=>'2′ , 'label' => 'Option2′) , array
('value'=>'3′ , 'label' =>'Option3′) ),
'label' => 'Size'
),
'2′ => array(
'value'=> array(array('value'=>'4′ , 'label' => 'Option4′) , array
('value'=>'5′ , 'label' =>'Option5′) ),
'label' => 'Color'
),
),
'disabled' => false,
'readonly' => false,
'after_element_html' => ”,
'tabindex' => 1
));
11.Multiline
$fieldset->addField('multiline', 'multiline', array(
'label' => Mage::helper('form')->__('Multi Line'),
'class' => 'required-entry',
'required' => true,
'name' => 'title',
'onclick' => "",
'onchange' => "",
'style' => “border:10px”,
'value' => 'hello !!',
'disabled' => false,
'readonly' => true,
'after_element_html' => ”,
'tabindex' => 1
));
12.链接类型 type=Link
$fieldset->addField('link', 'link', array(
'label' => Mage::helper('form')->__('Link'),
'style' => "",
'href' => 'www.excellencemagentoblog.com',
'value' => 'Magento Blog',
'after_element_html' => ”
));
13.普通Label类型 type=label
$fieldset->addField('label', 'label', array(
'value' => Mage::helper('form')->__('Label Text'),
));
14.Image Upload图片上传
$fieldset->addField('image', 'image', array(
'value' => 'http://www.excellencemagentoblog.com/wp-content/themes/excelltheme/images/logo.png',
));
15.File Upload文件上传
$fieldset->addField('file', 'file', array(
'label' => Mage::helper('form')->__('Upload'),
'value' => 'Uplaod',
'disabled' => false,
'readonly' => true,
'after_element_html' => ”,
'tabindex' => 1
));
16.日期类型 type=date
$fieldset->addField('date', 'date', array(
'label' => Mage::helper('form')->__('Date'),
'after_element_html' => ”,
'tabindex' => 1,
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'format' => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)
));
17.多选框 type=checkbox
$fieldset->addField('checkbox', 'checkbox', array(
'label' => Mage::helper('form')->__('Checkbox'),
'name' => 'Checkbox',
'checked' => false,
'onclick' => "",
'onchange' => "",
'value' => '1′,
'disabled' => false,
'after_element_html' => ”,
'tabindex' => 1
));
$fieldset->addField('checkboxes', 'checkboxes', array(
'label' => Mage::helper('form')->__('Checkboxs'),
'name' => 'Checkbox',
'values' => array(
array('value'=>'1′,'label'=>'Checkbox1′),
array('value'=>'2′,'label'=>'Checkbox2′),
array('value'=>'3′,'label'=>'Checkbox3′),
),
'onclick' => "",
'onchange' => "",
'value' => '1′,
'disabled' => false,
'after_element_html' => ”,
'tabindex' => 1
));
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。