2014-12-04 49 views
2

我有一個身份驗證表單,用戶必須通過數據庫提交他的用戶名和密碼以及信息接受或拒絕。我看過Zend Framework 1.8 tutorial 4 zend_auth and zend_form part 3但我的系統有錯誤:消息:元素必須由字符串或Zend_Form_Element實例指定

Message: Element must be specified by string or Zend_Form_Element instance

這是我的形式:login.php中(應用/表格/ 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(應用/控制器/ 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; 

回答

3

經由$this->addElements(array($username,$password,$login)); 所述的addElement(沒有或多個)方法嘗試添加元素只能處理一個元件

相關問題