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));
}
}
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;
}
}
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');
}
}
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;
}
}
Código HTML:
Ver original
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="css/index_style.css" type="text/css" rel="stylesheet"> </head> <body> <header> <hgroup> </hgroup> </header> <section> <?php echo $this->form; ?> </section> </section> </body> </html>
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.