Proyecto/application/forms/LoginForm.php
Código PHP:
class Application_Form_LoginForm extends Zend_Form
{
public function init()
{
$username = new Zend_Form_Element_Text('username');
$username->setLabel('Username:')
->setRequired(true)
->setAttrib('id', 'username')
->addValidator('NotEmpty', true)
->addValidator('alnum')
->setDecorators(array(
array('ViewHelper',
array('helper' => 'formText')),
array('Label',
array('class' => 'label'))
));
$password = new Zend_Form_Element_Password('password');
$password->setLabel('Contraseña:')
->setRequired(true)
->setAttrib('id', 'password')
->addValidator('NotEmpty', true)
->setDecorators(array(
array('ViewHelper',
array('helper' => 'formPassword')),
array('Label',
array('class' => 'label'))
));
$submit = new Zend_Form_Element_Submit('Entrar');
$submit->setAttrib('id', 'submitbutton')
->setDecorators(array(
array('ViewHelper',
array('helper' => 'formSubmit'))
));
$this->setMethod('post');
$this->setAction('security/login');
$this->addElements(array($username,$password,$submit));
}
}
Proyecto/application/controllers/IndexController.php
Código PHP:
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$form = new Application_Form_LoginForm();
$request = $this->getRequest();
$form->isValid($request->getPost());
$this->view->form = $form;
}
}
Proyecto/application/controllers/SecurityController.php
Código PHP:
class SecurityController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// action index
}
public function loginAction()
{
$request = $this->getRequest();
if (!$request->isPost()) {
return $this->_helper->redirector('index','index');
}
$form = new Application_Form_LoginForm();
if(!$form->isValid($request->getPost())):
$this->view->form = $form;
return $this->_helper->redirector('index','index');
endif;
$modelSecurity = new Application_Model_Security();
$username = $this->_getParam('username');
$password = $this->_getParam('password');
if($modelSecurity->validarUsuario($username, $password)):
return $this->_helper->redirector(index, user);
endif;
return $this->_helper->redirector('index','index');
}
public function logoutAction()
{
Zend_Auth::getInstance()->clearIdentity();
$this->_helper->redirector('index','index');
}
}
Proyecto/application/models/Security.php
Código PHP:
class Application_Model_Security
{
public function validarUsuario($username, $password) {
$authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
$authAdapter->setTableName('users')
->setIdentityColumn('username')
->setCredentialColumn('password');
$authAdapter->setIdentity($username)
->setCredential($password);
//$select = $authAdapter->getDbSelect ();
$auth = Zend_Auth::getInstance ();
$result = $auth->authenticate($authAdapter);
if($result->isValid()) :
return true;
endif;
return false;
}
}
Proyecto/application/views/scrpits/index/index.phml
Código HTML:
Ver original<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Cantarell|Philosopher|Copse"></link> <link href="css/index_style.css" type="text/css" rel="stylesheet"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript" ></script> <h2 class="left-only">Login
</h2> <?php
echo $this->form;
?>
El problema es cuando le doy entrar al boton. el realiza bien todas las validaciones.
pero no esta devolviendo los mensajes de las validaciones del Zend_Form.
Soy un poco nuevo en esto y no he podido solucionar ese detalle.