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');
}
}
}