user606521
user606521

Reputation: 15424

Data validation in CakePHP doesn't work

I don't know why this doesn't work (I am using CakePHP 2.1 and also tried 2.0):

Here is Model

class User extends AppModel
    {
        public $validate = array('username' => array('rule' => 'email'));
    }

Here is Controller

class UsersController extends AppController
    {
        function index()
        {
            $this->set('users', $this->User->find('all') );
        }

        function add()
        {
             if (!empty($this->request->data)) 
             {            
                if ($this->User->save($this->request->data)) 
                {                
                    $this->Session->setFlash('User has been registered.');
                    $this->redirect(array('action' => 'index'));            
                }        
             }
        }
    }

Here is add View

<h1>Add Post</h1>
<?php
    echo $this->Form->create('User');
    echo $this->Form->input('username');
    echo $this->Form->input('password');
    echo $this->Form->end('Register');
?>

And it validates whatever I write... And it should check if username is email...

It's impossible! It has to work! - but it doesn't...

I also checked it with cake php 2.0 and it still does't work - please help, it is so simple i has to work...

Maybe something with my db table is wrong???

CREATE TABLE `users` (
   `id` int(10) unsigned not null auto_increment,
   `username` varchar(50),
   `password` varchar(50),
   `created` datetime,
   `modified` datetime,
   PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=9;

This is very strange - in my app folder I only add 'UserModel.php', 'UserController.php' and 'add.ctp', and a db config - all that I wrote above - and validation doesn't work!!!

Upvotes: 0

Views: 3085

Answers (4)

user3038299
user3038299

Reputation: 11

You should write in your controller ( UserController )

($this-User->set($this->request->data)

before the function validates Like you see;

 function add()
    {
         if (!empty($this->request->data)) 
         {     $this->User->set($this->request->data);
            if ($this->User->validates()) 
            {   $this->User->save($this->request->data)
                $this->Session->setFlash('User has been registered.');
                $this->redirect(array('action' => 'index'));            
            }        
         }
    }

Upvotes: 1

user606521
user606521

Reputation: 15424

Ok I know what is wrong:

  1. my model filename was called 'UserModel.php' and it should be 'User.php'
  2. At start of file 'User.php' I had to put 'App::uses('AppModel', 'Model');'

Upvotes: 1

nIcO
nIcO

Reputation: 5001

I think the $validate array is not declared correctly.

Try this:

$validate = array('username' => array(
                        'email' => array(
                            'rule' => array('email')
                        )));

or this:

$validate = array('username' => 'email');

See http://book.cakephp.org/2.0/en/models/data-validation.html

Upvotes: 2

YonoRan
YonoRan

Reputation: 1728

O.k... this is what I do (also changing the username to an email)

$components = array('Auth' => array('authenticate' => array('Form' => array('fields' => array('username' => 'email', 'password' => 'password')))), 'Email');

This works. In CakePHP 1.3 you had to specify both the username (email) and the password. So when I moved to CakePHP 2.1 I continued doing the same thing and had no problems with the validation.

you can see all that you need here: Cookbook

Upvotes: 0

Related Questions