我正在与 Zend_dojo_form 战斗。
这是(部分)代码:
class Application_Form_RegistrationForm extends Zend_Dojo_Form {
private $_user;
private $_admin;
private $_teamadmin;
private $_newuser;
private $_redirect;
public function __construct($admin = false, $user = null, $redirect = '') {
//blablabla
parent::__construct();
}
public function init() {
Zend_Dojo::enableForm($this);
$this->setMethod('post');
$this->setAttribs(array(
'id' => 'formRegistration',
'name' => 'formRegistration',
));
//some decorators
if ($this->_admin) {
$this->addElement(
//blabla + inline javascript
'onChange' => "if(this == ".E_UserRole::OPERATOR.") dojo.query(\".perms\").style({ display:\"block\" }); else dojo.query(\".perms\").style({ display:\"none\" }); "
)
);
}
if (Application_Manager_Login::hasRole(E_UserRole::ADMIN)) {
//add some display:none elements
$permission_decorators = array(
"DijitElement",
"Errors",
array(array("data" => "HtmlTag"), array("tag" => "td", "class" => "perms", "style"=> "display:none", )),
array("Label", array("tag" => "td", "class" => "perms", "style"=> "display:none", 'escape' => false, 'requiredSuffix' => ' <span class="red">*</span>')),
array(array("row" => "HtmlTag"), array("tag" => "tr"))
);
//hidden element
$this->addElement(
'CheckBox',
'permission_content',
array(
'decorators' => $permission_decorators,
'label' => 'Gestione contenuti',
'checkedValue' => true,
'uncheckedValue' => false,
'checked' => $this->_user->permission_content,
)
);
}
//submit button and other stuff
}
}
如您所见,当 user_role 更改时,我已经放置了一些内联 javascript 来显示/隐藏一些选项。
现在情况有点复杂。我可以继续编写内联 javascript,但我想在开始时声明一个 js 函数,然后从 onchange 事件中调用它。
由于此表单由多个控制器调用,因此我不会每次手动添加此功能
new RegistrationForm();
叫做。