views:

1282

answers:

1

I am trying to use the built in validation functions of cakePHP for my registration/login page.

<?php
class User extends AppModel
{
    var $name = 'User';
    var $validate = array(
                          'name' => VALID_NOT_EMPTY,
                          'password' => VALID_NOT_EMPTY,
                          'email_id' => VALID_EMAIL
                         );
}

I do not have a separate view file for register or login. I have both the registration and login code of the application in a main controller and the views in a single index.ctp file. If the registration or login is valid, the page is redirected to the home page of the main controller.

class UsersController extends AppController 
{
    var $name = 'Users';
    var $helpers = array('Html', 'Form' );

    function register()
    {
     if (!empty($this->data))
      {
     if ($this->User->save($this->data))
     {
       $this->Session->setFlash('Your registration information was accepted.');
       $this->redirect('/main/home');
     }

   }
    }
 }

Index.ctp

<p>Please fill out the form below to register an account.</p>
<?php 
      echo $form->create('User', array('action' => 'register'));
      echo $form->input('name');
      echo $form->input('email_id');
      echo $form->input('password');
      echo $form->end('Register');
?>

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

Is that why, the custom error messages are not displayed. Because, if I have a separate view file for register module, then I get the custom messages.

But I do not want a separate register view file and a separate login view file. I want to have both the functions in index file of the main controller. Could you help me?

EDIT 1

If I use render,this is what I get in the browser.

Your registration failed.

Not Found

Error: The requested address '/users/register' was not found on this server.

This is the register function in main controller:

function register()
 {
   if (!empty($this->data))
   {
     if ($this->User->save($this->data))
     {
       $this->Session->setFlash('Your registration information was accepted.');
       $this->render('home');
     }
     else
     {
      $this->Session->setFlash('Your registration failed.');
         $this->render('index');
     }

   }
 }
A: 

There might be a conflict going on because you're creating two forms that use the same model. If you comment out the login form, do the error messages show up then?

MSR
The error messages are shown only when I have a separate register.ctp file in the views/users folder. If I redirect the control to the main page, when the registration fails, I get the Session setFlash message but not the error messages. Only if there is a separate view file for the register action the error messages show.
Angeline Aarthi
The validation messages would be loaded back into the view and aren't stored in the session unlike setFlash. If you redirect you will effectively lose the error messages.You might need to use $this->render('index') in the controller for the register method, so you don't need a redirect.
MSR