2

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 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.