Re: Login validation error messages

2009-06-25 Thread RhythmicDevil
Had to remove the redirect for when the validation failed




On Jun 25, 2:38 pm, RhythmicDevil  wrote:
> Hello,
> I am trying to figure out why myloginform fields are not showing
> errors. From what I have read if I used cake's built in form stuff and
> setup avalidationarray this should happen automatically if I call
> validate on the model. If I dump the Model I can see that there are
> errors in the validationErrors array. Do I actually have to set those
> errors for the View from the Controller?
>
> I have tried using $form->error() but I get nothing.
> If I invalidate the field in the controller the field gets marked with
> anerror.
>
> My ultimate goal is that I want each field to change to anerrorstyle
> and place a message next to it describing theerror.
>
> Thanks for any help
>
> MODEL
>
> class User extends AppModel
> {
>
>     var $validate = array (
>         'username'=> array (
>                 'rule'=>'notEmpty',
>                 'required' => true,
>                         'message'=>'Username is required'
>         ),
>         'password'=> array (
>                 'rule'=>'notEmpty',
>                 'required' => true,
>                 'message'=>'Password is required'
>         )
>     );
>
>     function validateLogin($data)
>     {
>         $user = $this->find( array ('username'=>$data['username'],
> 'password'=>md5($data['password'])), array ('id', 'username'));
>
>         if ( empty($user) == false)
>         {
>             return $user['User'];
>         }
>
>         return false;
>     }
>
> }
>
> CONTROLLER
>
> class UsersController extends AppController
> {
>     public $layout = 'login';
>     public $fields = array ('username', 'password');
>
>     function index()
>     {
>         print "Index";
>     }
>
>     functionlogin()
>     {
>         // If data has been submitted
>         if ( empty($this->data) == false)
>         {
>             // Pass the data to the model
>             $this->User->set($this->data);
>
>             // If the data validates
>             if ($this->User->validates())
>             {
>                 // If theloginsucceeds
>                 if (($user = $this->User->validateLogin($this->data
> ['User'])) == true)
>                 {
>                     $this->Session->write('User', $user);
>                     $this->Session->setFlash('You\'ve successfully
> logged in.');
>                     $this->redirect('/subscribers/index');
>                     exit ();
>                 }
>                 else // If theloginfails
>                 {
>                     $this->Session->setFlash('Loginfailed, please try
> again.');
>                     $this->redirect('index');
>                     exit ();
>                 }
>             }
>             else // If the data does not validate
>             {
>                 debug($this->User);
>                     $this->redirect('index');
>                     exit ();
>             }
>         }
>
>     }
>
>     function logout()
>     {
>         $this->Session->destroy('user');
>         $this->Session->setFlash('You\'ve successfully logged out.');
>         $this->redirect('login');
>     }
>
> }
>
> VIEW
>
> 
>         
>                 create('User', array('action' => 
> 'login'));?>
>                         
>                                 PleaseLogin
>                                 
>                                         
>                                                 
>                                                         $session->flash();
>                                                 ?>
>                                                 
>                                                          class="form_element">
>                                                                                                                                          echo 
> $form->input('User.username');
>                                                                 ?>
>                                                         
>                                                 
>                                                 
>                                                          class="form_element">
>                                                                  $form->input('User.password');?>
>                                                         
>                                                 
>                                                 
>                                                         
>                                                                  $form->submit('Login');?>
>                                                         
>                                                 
>                                         
>                                 
>                                  
>                         
>                 end(); ?>
>         
> 
--~--~-~--~~~---~--~~
You r

Login validation error messages

2009-06-25 Thread RhythmicDevil

Hello,
I am trying to figure out why my login form fields are not showing
errors. From what I have read if I used cake's built in form stuff and
setup a validation array this should happen automatically if I call
validate on the model. If I dump the Model I can see that there are
errors in the validationErrors array. Do I actually have to set those
errors for the View from the Controller?

I have tried using $form->error() but I get nothing.
If I invalidate the field in the controller the field gets marked with
an error.


My ultimate goal is that I want each field to change to an error style
and place a message next to it describing the error.

Thanks for any help


MODEL

class User extends AppModel
{

var $validate = array (
'username'=> array (
'rule'=>'notEmpty',
'required' => true,
'message'=>'Username is required'
),
'password'=> array (
'rule'=>'notEmpty',
'required' => true,
'message'=>'Password is required'
)
);


function validateLogin($data)
{
$user = $this->find( array ('username'=>$data['username'],
'password'=>md5($data['password'])), array ('id', 'username'));

if ( empty($user) == false)
{
return $user['User'];
}

return false;
}
}





CONTROLLER

class UsersController extends AppController
{
public $layout = 'login';
public $fields = array ('username', 'password');

function index()
{
print "Index";
}

function login()
{
// If data has been submitted
if ( empty($this->data) == false)
{
// Pass the data to the model
$this->User->set($this->data);

// If the data validates
if ($this->User->validates())
{
// If the login succeeds
if (($user = $this->User->validateLogin($this->data
['User'])) == true)
{
$this->Session->write('User', $user);
$this->Session->setFlash('You\'ve successfully
logged in.');
$this->redirect('/subscribers/index');
exit ();
}
else // If the login fails
{
$this->Session->setFlash('Login failed, please try
again.');
$this->redirect('index');
exit ();
}
}
else // If the data does not validate
{
debug($this->User);
$this->redirect('index');
exit ();
}
}

}

function logout()
{
$this->Session->destroy('user');
$this->Session->setFlash('You\'ve successfully logged out.');
$this->redirect('login');
}
}



VIEW



create('User', array('action' => 'login'));?>

Please Login


flash();
?>


input('User.username');
?>




input('User.password');?>




submit('Login');?>




 

end(); ?>


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---