1

I am learner in CakePhp. I am developing a login and signup functionality on single page.I am using same Model named 'User' for it.But On validating the both form,When I submit Signup Form same error message shown in both signup and login form. Here is the code

UsersController

class UsersController extends AppController {
  public $layout = 'test';
  public $components = array('Security');
  public function index(){      
    if($this->request->is('Post')){
        if($this->request->data['User']['email']){
            $this->User->set('creation_date',date('Y-m-d H:i:s'));
            $this->request->data['User']['password'] = Security::hash($this->request->data['User']['password'], 'md5', true);
            if($this->User->save($this->request->data)){
                //$this->Session->setFlash('Your data is Entered');
                $this->Session->write('username', $this->request->data['User']['user_name']);
                $this->flash('You are signed Up', 'dashboard');
                //$this->redirect(array('action'=>'dashboard'));
                }
            else{
               $this->Session->setFlash('Data is Not Save'); 
            }
        }
    else {
        $username = $this->request->data['User']['user_name'];
        $password = Security::hash($this->request->data['User']['password'], 'md5', true);
        $query = $this->User->find('all',array('conditions'=>array('User.user_name'=>$username,
                                                          'User.password'=>$password)));
        if (sizeof($query)!=0){
            $this->Session->write('username', $query[0]['User']['user_name']);
            $this->redirect('dashboard');
        }
        else {
            $this->Session->setFlash("User is Not Valid");
        }
    }

    }
}
}

index View

    <div id="loginForm">
<?php 
echo $this->form -> create('', array('url'=>array('controller'=>'Users','action'=>'index')));?>
<table cellpadding="10">
<?php
echo '<tr><td>'.$this->form -> input('user_name', array('placeholder'=>'UserName')).'</td>';
echo '<td>'.$this->form -> input('password', array('id'=>'password','placeholder'=>'Password')).'</td>';
echo '<td>'.$this->form -> end('Login').'</td></tr>';
?>
</table>
    </div>
    <center>
    <h1>Create Account</h1>
    <div id="signupForm">
    <table cellpadding="5">
<?php
echo $this->form -> create('', array('url'=>array('controller'=>'Users','action'=>'index'),'inputDefaults'=>array('label'=>FALSE)));?>
<tr>
    <td>User Name:-
    <?php echo $this->form -> input('user_name', array('placeholder'=>'UserName'));?></td>
    <td> Email:-
    <?php echo $this->form -> input('email', array('placeholder'=>'Email'));?></td>
 </tr>
 <tr>
     <td>Mobile No:-
     <?php echo $this->form -> input('mobile_no', array('placeholder'=>'Mobile')).'<br>';?></td>
    <td>Password:-
     <?php echo $this->form -> input('password', array('placeholder'=>'password')).'<br>';?></td>
 </tr>
 <tr><td colspan="2"><center><?php echo $this->form -> end('Sign Up');?></center></td></tr>
</table>

Here is Scren Shotenter image description here I have searched a lot but i cant get any useful solution.Please tell what should i do.

Thank You.

puneet gupta
  • 373
  • 3
  • 7

1 Answers1

0

There are similar questions in SO, none of them work? One in particular I remember is this (because I answered it). Did you tried that?

Summary of the answer there:
Since form validation uses the model name to input the errors, and the model name for both forms is the same, you need a workaround to do what you want. So you need to change your form names (if they are just one, separate them), fix the model treatment in the controller actions, and change the names back before rendering for a correct validation of errors. It's all explained there.

Community
  • 1
  • 1
Nunser
  • 4,512
  • 8
  • 25
  • 37
  • Thanks Nunser, I have done whatever you suggest in the answer you are talking about.After doing this It was showing "Internal Server Error". – puneet gupta Oct 23 '13 at 08:31
  • Well, something must have go wrong and you have to debug to see what it was, without more info there's no much I can do to help. After you debug and see what's the problem, either update the question or ask another one altogether. – Nunser Oct 23 '13 at 11:57