Greetings,
I am new to CakePHP and am having trouble displaying the form
validation errors from my registration form. After extensive research
online, I feel I am missing something obvious. Maybe I am incorrectly
assuming that Cake will display the error next to the appropriate form
field.
Here is my model:
<?php
class User extends AppModel {
var $name = 'User';
var $validate = array(
'username' => 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'
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'This username has already been taken.'
)
),
'password' => array(
'rule' => array('minLength', '8'),
'message' => 'Mimimum 8 characters long'
),
'email' => array(
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'This username has already been taken.'
),
'email' => array(
'rule' => array('email', true),
'message' => 'Please supply a valid email address.'
)
)
);
function validateLogin($data) {
$user = $this->find(array('username' => $data['username'],
'password' => md\
5($data['password'])), array('id', 'username'));
if(empty($user) == false)
return $user['User'];
return false;
}
}
?>
Here is my controller:
<?php
class UsersController extends AppController
{
var $name = "Users";
var $helpers = array('Html', 'Form');
var $components = array('Auth');
function index()
{
}
function beforeFilter() {
$this->Auth->allow('register');
// $this->__validateLoginStatus();
}
function login() {
$this->layout='main';
$this->pageTitle='ShotSpin - Shop for products, Write Reviews, and
Earn Commissions - Login';
if(empty($this->data) == false)
{
if(($user = $this->User->validateLogin($this->data['User'])) == true)
{
$this->Session->write('User', $user);
$this->Session->setFlash('You\'ve successfully logged in.');
$this->redirect('index');
exit();
}
else
{
$this->Session->setFlash('Sorry, the information you\'ve entered
is incorrect.');
$this->redirect('login');
exit();
}
}
}
function logout() {
$this->Session->destroy('user');
$this->Session->setFlash('You\'ve successfully logged out.');
$this->redirect('login');
}
// register new users
function register() {
$this->layout='main';
$this->pageTitle='ShotSpin - Shop for products, Write Reviews, and
Earn Commissions - Register';
// set the view of errors to false
$this->set('error', false);
// check if we have data
if (!empty($this->data)) {
// pass the data to the model so we can manually validate it
$this->User->set($this->data);
if ($this->User->validates()) {
// check if the passwords match
if ($this->data['User']['password'] == $this->Auth->password($this-
>data['User']['password_confirm'])) {
// create the user
$this->User->create();
$this->User->save($this->data);
$this->redirect(array('action' => 'index'));
exit;
}
} else {
// we had a validation error so display that
$this->set('error', true);
// redirect them back to the registration page
$this->redirect('register');
exit;
}
}
}
function __validateLoginStatus()
{
if($this->action != 'login' && $this->action != 'logout')
{
if($this->Session->check('User') == false)
{
$this->redirect('login');
$this->Session->setFlash('You must be log in to view this
page.');
}
}
}
}
?>
And here is my view for the registration page:
<div id="search">
<br>
<span class="searchtext">Register</span>
<br>
<br>
</div>
<div id="content">
<?php
if ($error){?>
<p class="error">There are some problems with your registration.</p>
<?php }
echo $form->create('User', array('action' => 'register'));
echo $form->input('firstname', array('label' => 'First Name'));
echo $form->input('lastname', array('label' => 'Last Name'));
echo $form->input('email');
echo $form->input('username');
echo $form->input('password');
echo $form->input('password_confirm', array('type' => 'password',
'label' => 'Confirm Password'));
echo $form->submit();
echo $form->end();
?>
</div>
Any insight you can offer would be much appreciated.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---