我想添加我在控制器内创建的表单。但是页面没有显示任何内容。
这是表单代码:
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() {
}
}
如何从控制器中插入表单?
答案 0 :(得分:1)
将表单对象传递给视图,并在那里渲染。
[控制器:内部动作]
$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();