Had to remove the redirect for when the validation failed



On Jun 25, 2:38 pm, RhythmicDevil <rhythmicde...@gmail.com> 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
>
> <!-- STARTLOGINFORM -->
>         <div class="grid_4 prefix_4 suffix_4">
>                 <?php echo $form->create('User', array('action' => 
> 'login'));?>
>                         <div class="box" style="margin-top:25%;">
>                                 <h5>PleaseLogin</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">&nbsp;</div>
>                         </div>
>                 <?php echo $form->end(); ?>
>         </div>
> <!-- ENDLOGINFORM -->
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to