消息:元素必须由字符串或Zend_Form_Element实例指定

时间:2014-12-04 08:37:39

标签: php zend-framework zend-form

我有一个身份验证表单,用户必须提交他的用户名和密码以及通过数据库接受或拒绝的信息。我看过Zend Framework 1.8 tutorial 4 zend_auth and zend_form part 3,但我的系统有错误:

  

消息:元素必须由字符串或Zend_Form_Element实例

指定

这是我的表格: Login.php (application / forms / Login.php)

<?php
Class Application_Form_Login extends Zend_Form
{
    public function __construct($option = null)
    {
        parent::__construct($option);

        $this->setName('login');
        $username = new Zend_Form_Element_Text('username');
        $username->setLabel('User name:')
                  ->setRequired();
        $password = new Zend_Form_Element_Password('password');
        $password->setLabel('Password:')
                 -> setRequired('true');
        $login = new Zend_Form_Element_Submit('login');
        $login ->setLabel('Login');
        $this ->addElement(array($username,$password,$login));
        $this->setMethod('post');
        $this->setAction('/authentication/login');
    }
}

这是 AuthenticationController.php (application / controller / AuthenticationController.php)

<?php
class AuthenticationController extends Zend_Controller_Action 
{
    public function loginAction()
    {
        $form = new Application_Form_Login();
        $this->view->form = $form;
        $authAdapter = $this->getAuthAdapter();
        $username='john';
        $password = 'pass1';
        $authAdapter->setIdentity($username)
                    ->setCredential($password);
        $auth= Zend_Auth::getInstance();
        $result = $auth->authenticate($authAdapter);
        if ($result->isValid())
        {
            $identity = $authAdapter->getResultRowObject();
            $authStorage = $auth->getStorage();
            $authStorage->write($identity);
            $this->_redirect('index/index');
        }   
        else 
        {
            echo "invalid";
        }

    }
    private function getAuthAdapter()
    {
        $authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
        $authAdapter->setTableName('users')
                    ->setIdentityColumn('username')
                    ->setCredentialColumn('password');
        return $authAdapter; 
    }
}

并且这样看:

<?php 
echo $this->form;

1 个答案:

答案 0 :(得分:3)

尝试通过$this->addElements(array($username,$password,$login));添加元素 addElement(without s)方法只能处理一个元素