Zend framework 无法呈现Zend_表单

Zend framework 无法呈现Zend_表单,zend-framework,zend-form,Zend Framework,Zend Form,我正在尝试使用Zend_表单呈现登录表单,我希望通过调用www.site.com/login或www.site.com/account/login来呈现它,但是一旦我完成了下面的所有步骤,并尝试从浏览器中调用/login或/account/login,我就会得到一个HTTP 500(内部服务器错误)。。即使网站的其他部分工作得很好。请帮我找出哪里错了 (请注意,我使用的是Zend Framework 1.11) (1) 通过ZF创建模型 zf创建模型FormLogin (2) 在applicat

我正在尝试使用Zend_表单呈现登录表单,我希望通过调用www.site.com/login或www.site.com/account/login来呈现它,但是一旦我完成了下面的所有步骤,并尝试从浏览器中调用/login或/account/login,我就会得到一个HTTP 500(内部服务器错误)。。即使网站的其他部分工作得很好。请帮我找出哪里错了

(请注意,我使用的是Zend Framework 1.11)

(1) 通过ZF创建模型

zf创建模型FormLogin

(2) 在application/models/FormLogin.php中编辑新创建的模型

class Application_Model_FormLogin extends Zend_Form
{
     public function __construct($options = null)
    {

         parent::__construct($options);

         $this->setName('login');
         $this->setMethod('post'); // GET O POST

         $this->setAction('/account/login'); // form action=[..]

         $email = new Zend_Form_Element_Text('email');
         $email->setAttrib('size', 35);

         $pswd = new Zend_Form_Element_Password('pswd');
         $pswd->setAttrib('size', 35);

         $submit = new Zend_Form_Element_Submit('submit'); // submit button

         $this->setDecorators( array( array('ViewScript', array('viewScript' => '_form_login.phtml'))));
         $this->addElements(array($email, $pswd, $submit));

       } 

}
(3) 将登录操作添加到帐户控制器

class AccountController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {

    }

    public function loginAction()
    {
        $form = new Application_Model_FormLogin();

          $this->view->form = $form;

    }


}
(4) 在application/views/scripts/account/login.phtml中创建视图


Application\u Model\u FormLogin
的类定义更改为以下内容:

<?php

class Application_Model_FormLogin extends Zend_Form
{
    public function init()
    {
        $this->setName('login');
        $this->setMethod('post'); // GET O POST

        $this->setAction('/account/login'); // form action=[..]

        $email = new Zend_Form_Element_Text('email');
        $email->setAttrib('size', 35);

        $pswd = new Zend_Form_Element_Password('pswd');
        $pswd->setAttrib('size', 35);

        $submit = new Zend_Form_Element_Submit('submit'); // submit button

        $this->setDecorators( array( array('ViewScript', array('viewScript' => '_form_login.phtml'))));
        $this->addElements(array($email, $pswd, $submit));
    }
}

drew010
init()
而不是
\uu-construct()
中设置表单有一个很好的观点

我只是花了几个小时与viewScript方法斗争,这就是我如何使它工作的(到目前为止)

  • $this->view->form=$form
    分配给视图

  • 然后在您的视图中,您需要使用类似于

  • 使用此方法时,我没有使用
    $this->setDecorators(数组(数组('ViewScript',数组('ViewScript'=>'_form_login.phtml'))代码行

正确的方法是使用viewScript装饰器,但是我还不能让它工作。我确实发现,使用这个decorator,我必须使用
$this->element->elementname
访问元素,但我找不到任何方法来访问该方法或操作
getMethod()
getAction()
都返回了错误。

[编辑] 好的,我让它工作了:

  • 使用
    init()

  • $form->setDecorators(数组)(
    数组('ViewScript',数组('ViewScript'=>'\u yourScript.phtml'))
    ));我想把它添加到控制器中

  • 在viewScript中,使用
    $this->element
    而不是
    $this->form
    访问表单对象

  • 将表单分配给视图
    $this->view->form=$form

  • 正常呈现表单

这就是我最后开始工作的例子

<form action="<?php echo $this->element->getAction() ?>"
      method="<?php echo $this->element->getMethod() ?>">
    <table>
        <tr>
            <td><?php echo $this->element->weekend->renderLabel() ?></td>
            <td><?php echo $this->element->weekend->renderViewHelper() ?></td>
            <td><?php echo $this->element->bidlocation->renderLabel() ?></td>
            <td><?php echo $this->element->bidlocation->renderViewHelper() ?></td>
            <td><?php echo $this->element->shift->renderLabel() ?></td>
            <td><?php echo $this->element->shift->renderViewHelper() ?></td>
        </tr>
        <tr>
            <td colspan="6"><?php echo $this->element->submit ?></td>
        </tr>
    </table>
</form>

我认为您不需要为默认模块的
控制器/操作定义任何路由。即使我删除了路由,它也不起作用它应该起作用,正如错误所说,这是一个“内部服务器错误”,对我来说,您的代码没有问题。请查看您的Apache错误日志以了解错误的实际情况。请参阅此答案,了解一种无需执行viewscript装饰器即可单独呈现表单元素的方法:$this->element在使用viewscript装饰器时是正确的$使用$this->render(veiwScript)时,此->表单是正确的,谢谢我没有注意到它被用于视图脚本装饰器中。我认为,因为它呈现的是完整的表单,所以视图代码就在视图脚本中。我现在删除了那个部分。你的解决方案成功了,谢谢。但现在它揭示了另一个问题,致命错误:在第18行的Application\controllers\AccountController.php中找不到类'Application\u Model\u FormLogin',所以基本上Zend不加载我的FormLogin.php模型,我如何加载它?我想我在引导程序中遗漏了一些东西。。再次感谢您如果您使用Zend应用程序,应设置自动加载程序以自动查找该文件。确保您的目录结构遵循。另一方面,由于该类是一个表单,您可能希望将其移动到
forms
目录中,并将该类重命名为
Application\u form\u Login
。它可能会解决这个问题,但当您创建一个模型时,听起来好像无法加载。
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    public function _initRoutes()
    {

        $frontController = Zend_Controller_Front::getInstance();
        $router = $frontController->getRouter();


        $route = new Zend_Controller_Router_Route_Static (
            'login',
            array('controller' => 'Account', 'action' => 'login')
        );

        $router->addRoute('login', $route);

        $route = new Zend_Controller_Router_Route (
            'games/asin/:asin/',
            array('controller' => 'Games',
                  'action'     => 'view',
                   'asin'       => 'B000TG530M' // default value
                 )
        );
        $router->addRoute('game-asin-view', $route);



    }
}
<?php

class Application_Model_FormLogin extends Zend_Form
{
    public function init()
    {
        $this->setName('login');
        $this->setMethod('post'); // GET O POST

        $this->setAction('/account/login'); // form action=[..]

        $email = new Zend_Form_Element_Text('email');
        $email->setAttrib('size', 35);

        $pswd = new Zend_Form_Element_Password('pswd');
        $pswd->setAttrib('size', 35);

        $submit = new Zend_Form_Element_Submit('submit'); // submit button

        $this->setDecorators( array( array('ViewScript', array('viewScript' => '_form_login.phtml'))));
        $this->addElements(array($email, $pswd, $submit));
    }
}
<form action="<?php echo $this->element->getAction() ?>"
      method="<?php echo $this->element->getMethod() ?>">
    <table>
        <tr>
            <td><?php echo $this->element->weekend->renderLabel() ?></td>
            <td><?php echo $this->element->weekend->renderViewHelper() ?></td>
            <td><?php echo $this->element->bidlocation->renderLabel() ?></td>
            <td><?php echo $this->element->bidlocation->renderViewHelper() ?></td>
            <td><?php echo $this->element->shift->renderLabel() ?></td>
            <td><?php echo $this->element->shift->renderViewHelper() ?></td>
        </tr>
        <tr>
            <td colspan="6"><?php echo $this->element->submit ?></td>
        </tr>
    </table>
</form>