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
<!-- START LOGIN FORM -->
<div class="grid_4 prefix_4 suffix_4">
<?php echo $form->create('User', array('action' => 'login'));?>
<div class="box" style="margin-top:25%;">
<h5>Please Login</h5>
<div class="body">
<table style="width:70%; margin:0 auto 0 auto;">
<?php
$session->flash();
?>
<tr>
<td colspan="2" class="form_element">
<?php
echo $form->input('User.username');
?>
</td>
</tr>
<tr>
<td colspan="2" class="form_element">
<?php echo $form->input('User.password');?>
</td>
</tr>
<tr>
<td colspan="2">
<?php echo $form->submit('Login');?>
</td>
</tr>
</table>
</div>
<div class="footer"> </div>
</div>
<?php echo $form->end(); ?>
</div>
<!-- END LOGIN FORM -->
--~--~---------~--~----~------------~-------~--~----~
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+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---
No comments:
Post a Comment