2
votes

I have an authentication form, user must submit his username and password and information accept or deny through database. I've watched Zend Framework 1.8 tutorial 4 zend_auth and zend_form part 3 but my system has error:

Message: Element must be specified by string or Zend_Form_Element instance

This is my form: 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');
    }
}

and this is 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; 
    }
}

And view like this :

<?php 
echo $this->form;
1

1 Answers

3
votes

try adding the elements via $this->addElements(array($username,$password,$login)); the addElement(without s) method can only handle one element