php自定义表单程序,php – 将自定义表单元素添加到Adminhtml表单

这是一个老帖子,但它仍然可以对某人有用:

是的你可以.

以下代码位于:

应用程序/代码/本地/ MyCompany的/ MyModule的/座/ MyForm.php

class MyCompany_MyModule_Block_MyForm extends Mage_Adminhtml_Block_Widget_Form

{

protected function _prepareForm()

{

$form = new Varien_Data_Form(array(

'id' => 'edit_form',

'action' => $this->getUrl('*/*/save'),

'method' => 'post'

));

$fieldset = $form->addFieldset('my_fieldset', array('legend' => 'Your fieldset title')));

//Here is what is interesting us

//We add a new type, our type, to the fieldset

//We call it extended_label

$fieldset->addType('extended_label','MyCompany_MyModule_Lib_Varien_Data_Form_Element_ExtendedLabel');

$fieldset->addField('mycustom_element', 'extended_label', array(

'label' => 'My Custom Element Label',

'name' => 'mycustom_element',

'required' => false,

'value' => $this->getLastEventLabel($lastEvent),

'bold' => true,

'label_style' => 'font-weight: bold;color:red;',

));

}

}

这是您的自定义元素的代码,它位于app / code / local / MyCompany / MyModule / Lib / Varien / Data / Form / Element / ExtendedLabel.php中:

class MyCompany_MyModule_Lib_Varien_Data_Form_Element_ExtendedLabel extends Varien_Data_Form_Element_Abstract

{

public function __construct($attributes=array())

{

parent::__construct($attributes);

$this->setType('label');

}

public function getElementHtml()

{

$html = $this->getBold() ? '' : '';

$html.= $this->getEscapedValue();

$html.= $this->getBold() ? '' : '';

$html.= $this->getAfterElementHtml();

return $html;

}

public function getLabelHtml($idSuffix = ''){

if (!is_null($this->getLabel())) {

$html = ''.$this->getLabel()

. ( $this->getRequired() ? ' *' : '' ).''."\n";

}

else {

$html = '';

}

return $html;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值