在Zend控制器中添加表单的最佳方法是什么?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Zend控制器中添加表单的最佳方法是什么?相关的知识,希望对你有一定的参考价值。
我想添加一个我在控制器中创建的表单。但该页面没有显示任何内容。
这是表单代码:
class Advancedsms_Form_ChangePassword extends Engine_Form {
//put your code here
public function init() {
parent::init();
$this->setTitle(Zend_Registry::get('Zend_Translate')->_('Change Password'))
->setDescription(Zend_Registry::get('Zend_Translate')->_('Enter your new password'))
-> setAttrib('id', 'change_password')
-> setAttrib('enctype', 'multipart/form-data')
->setAction(Zend_Controller_Front::getInstance()->getRouter()->assemble([]));
$this->addElement('Password', 'password', [
'label'=> Zend_Registry::get('Zend_Translate')->_('New Password'),
'description' => Zend_Registry::get('Zend_Translate')->_('Enter your new password'),
'required' => true,
]);
$this->addElement('Password', 'password_confirm', [
'label' => Zend_Registry::get('Zend_Translate')->_('Confirm your new password'),
'description' => Zend_Registry::get('Zend_Translate')->_('Confirm your new password'),
'required' => true,
]);
$this->addElement('submit', 'submit', [
'label'=> Zend_Registry::get('Zend_Translate')->_('Submit'),
'required' => true,
]
);
$this->setAction(Zend_Controller_Front::getInstance()->getRouter()->assemble(array('controller' => 'api', 'action' => 'changepassword'), 'change_password'));
}
}
我使用的路线是http://example.com/advancedsms/api/changepassword
定义如下:
'change_password' => [
'route' => 'advancedsms/:controller/:action/*',
'defaults' => [
'module' => 'advancedsms',
'controller' => 'api',
'action' => 'changepassword',
],
'reqs' => [
'controller' => 'D+',
'action' => 'D+',
]
],
我在控制器类中的动作代码:
class Advancedsms_ApiController extends Core_Controller_Action_Standard {
/**
* This handles password change
*/
public function changepasswordAction() {
}
}
如何从控制器中插入表单?
答案
将表单对象传递给视图,然后在那里渲染。
[控制器:内部动作]
$this->view->form = $form = new Advancedsms_Form_ChangePassword();
if(!$this->getRequest()->isPost()) { //ends action() flow, and renders view.
return;
}
if($form->isValid($this->getRequest()->getPost())) { // rules added in form definition gets validated here.
// process the post data here.
}
[视图]
echo $this->form->render();
以上是关于在Zend控制器中添加表单的最佳方法是什么?的主要内容,如果未能解决你的问题,请参考以下文章
使用 Zend Framework 模型定义表关系方法的最佳代码位置是啥?