views:

613

answers:

2

Hi, I'm trying to perform validation in the login page for the name,email and password fields. If the input fails validation,the error message should be displayed.

But here,when I fill in the details and submit, it is redirected to the next page. Only the value is not saved in the database.

Why is the message not displayed?

This is my model:

 class User extends AppModel {
    var $name = 'User';
    var $validate = array(
   'name' => array(
     'alphaNumeric' => array(
        'rule' => 'alphaNumeric',
        'required' => true,
        'message' => 'Alphabets and numbers only'
        ),
     'between' => array(
        'rule' => array('between', 5, 15),
        'message' => 'Between 5 to 15 characters'
       )
     ),
   'password' => array(
      'rule' => array('minLength', '8'),
      'message' => 'Mimimum 8 characters long'
      ),
   'email_id' => 'email'
       );

function loginUser($data)
{
 $this->data['User']['email_id']=$data['User']['email_id']; 
 $this->data['User']['password']=$data['User']['password'];   

 $login=$this->find('all');
 foreach($login as $form):
  if($this->data['User']['email_id']==$form['User']['email_id'] && $this->data['User']['password']==$form['User']['password'])
  {
   $this->data['User']['id']= $this->find('all',array('fields' => array('User.id'),
        'conditions'=>array('User.email_id'=> $this->data['User']['email_id'],'User.password'=>$this->data['User']['password'])  
        ));
   $userId=$this->data['User']['id'][0]['User']['id'];

   return $userId;

  }
 endforeach;
}

function registerUser($data)
{
 if (!empty($data)) 
 {
  $this->data['User']['name']=$data['User']['name'];
  $this->data['User']['email_id']=$data['User']['email_id']; 
  $this->data['User']['password']=$data['User']['password'];
  if($this->save($this->data))
  {
   $this->data['User']['id']= $this->find('all',array('fields' => array('User.id'),
        'order' => 'User.id DESC'   
        ));
   $userId=$this->data['User']['id'][0]['User']['id'];
   return $userId;
  }
 }
}

}

This is my controller:

class UsersController extends AppController 
{
var $name = 'Users';
var $uses=array('Form','User','Attribute','Result');
var $helpers=array('Html','Ajax','Javascript','Form');

function login()
   {

 $userId=$this->User->loginUser($this->data);
 if($userId>0){
  $this->Session->setFlash('Login Successful.');
  $this->redirect('/forms/homepage/'.$userId);
  break;
 }
 else{
  $this->flash('Login Unsuccessful.','/forms');
 }
}

function register()
{ 
 $userId=$this->User->registerUser($this->data);  
 $this->Session->setFlash('You have been registered.');
 $this->redirect('/forms/homepage/'.$userId);               
}

}

EDIT

Why is the message,example,"Minimum 8 characters long", is not being displayed when give less than 8 characters in the password field?

 <!--My view file File: /app/views/forms/index.ctp   -->
 <?php 
echo $javascript->link('prototype.js');
echo $javascript->link('scriptaculous.js');
echo $html->css('main.css');
?>

  <div id="appTitle"> 
  <h2> formBuildr </h2>
  </div>

  <div id="register">
  <h3>Register</h3>
  <?php
echo $form->create('User',array('action'=>'register'));
echo $form->input('User.name');
echo $form->error('User.name','Name not found');
echo $form->input('User.email_id');
echo $form->error('User.email_id','Email does not match');
echo $form->input('User.password');
echo $form->end('Register');
  ?>
  </div>

  <div id="login">
  <h3>Login</h3>
  <?php
echo $form->create('User',array('action'=>'login'));
echo $form->input('User.email_id');
echo $form->input('User.password');
echo $form->end('Login');
  ?>
  </div>
A: 

Your validation seems correct

How about trying the following:

  1. Make sure set your $form->create to the appropriate function
  2. Make sure there is no $this->Model->read() before issuing Model->save();

Edit

Did you have the following?:

function register()
{
   //do not put any $this->User->read or find() here or before saving pls.
   if ($this->User->save($this->data))
   {
       //...
   }
}

Edit2

IF you're doing a read() or find() before saving the Model then that will reset the fields. You should be passing the variable as type=hidden in the form. I hope i am making sense.

Edit3

I think you need to move your registerUser() into your controller because having that function in the model doesn't provide you a false return. it's always going to be true even if it has validation errors.

drikoda
Yes,I have that..
Angeline Aarthi
No,actually I save the entry in the table and then only get the Id of the saved entry. I do not read() or find() before saving.
Angeline Aarthi
Yes i noticed. Try moving out the save routine in your controller?
drikoda
I'm trying to save it in the controller,but it isnt getting saved...function register(){ if (!empty($this->data)) { if($this->User->save($this->data)) { $this->data['User']['id']= $this->User->find('all',array('fields' => array('User.id'), 'order' => 'User.id DESC' )); $userId=$this->data['User']['id'][0]['User']['id']; $this->Session->setFlash('You have been registered.'); $this->redirect('/forms/homepage/'.$userId); } } }
Angeline Aarthi
A: 

Comment out the redirect line and set the debug to 2 in config/core.php. Then look at the sql that is being generated to see if your insert is working. If the errors are not being displayed, maybe in the view, you are using $form->text or $form->select instead of the $form->input functions. Only the $form->input functions will automatically display the error messages.

jimiyash
If all else fails sometimes it is better to just start over afresh and maybe look what other people are doing. Registration is very common and I'm sure you can find a cakephp registration tutorial on the net.
jimiyash
well,what ever code I try, some error comes up..Can some one tell me a nice tutorial for it?
Angeline Aarthi