0

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!!!

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

I have already tried it, and it's still the same - and I read this chapter and I am doing everything exactly like it should be done and it still doesn't work - I don't know what to do! Maybe if it works for you, you could send me model/view/controller + database script on [email protected]?
You're right sorry. Your syntax seems correct as well. I always use my first example, because this is what cake bake generates. Then I don't know. You should probably debug the invalidFields() in Model.php from the core. For example, to start, you could verify that the $validate array is what you want if you put a debug($this->validate) in this function.
1

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

Comments

1

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

Comments

0

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

3 Comments

But I am not using auth component yet - so it should work normally and check if field 'username' is a valid email...
Sorry... I didn't notice that you were not using the Auth component... My bad. Now that I take a closer look... Why don't you change the table so you have an Email field instead of a Username field? why have a username and treat it as an email instead of just having an Email?
And also I don't care for now on logging in user but registering them so it should be basic UserModel save...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.