从 Zend 控制器中添加表单的最佳方式是什么?

What is the best way to add a form from within a Zend controller?

我想添加一个我在控制器中创建的表单。但是该页面不显示任何内容。

这是表单代码:

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:

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();