Validation Gotcha in CakePHP 1.2

26 06 2007

While trying to write a simple login function for a user model today I came across a small gotcha that it is not apparent at first.

In CakePHP 1.1 if you want to manually validate a model you would write:

if ($this->Model->validates($this->data)){

However in CakePHP 1.2 this throws a nice deprecated warning:

Warning (512): (Model::validates) Parameter usage is deprecated, set the $data property instead [CORE/cake/libs/model/model.php, line 1660]

I first tried just an empty validates() expecting it to use Controller::data, however this was appearing to pass validation. Not good when the entire form was empty with VALID_NOT_EMPTY’s on every field. A quick look-see in the source of model.php reveals that Model::invalidFields() expects Model::data to be populated.

As pointed out by nate in the comments my original solution is not recommended.

The updated solution is to use:

if ($this->Model->validates()){
    // success



9 responses

26 06 2007

Direct manipulation of the Model’s $data property is not recommended. Use the set() method.

26 06 2007

Thanks nate, I have updated the post to reflect your recommendation.

20 09 2007

Thanks for this post Geoff – saved me a lot of head scratching!

16 11 2007

Wow, I have looked for this answer all evening. Thanks for posting!

3 03 2008

Geoff, thanks!

This saved me from giving cake the boot. Tired of the deprecated stuff and lack of docs.

16 07 2008

Wow. Huge thanks. Definitely just spent about an hour stuck on this.

22 07 2008

how can i validate a contact form?
i have a model Contact – but no Database
so i just put in the model var $useTable = false;

but with the above code, it always shows validation errors – no matter if i typed something in or not in the contact form:

model Contact:
var $validate = array(
'name' => array(
'empty' => array(
'allowEmpty' => false,
'required' => true,
'message' => 'Please insert your name',
'email' => array(
'required' => array(
'required' => true,
'message' => 'Please insert your email',
'title' => array(
'alphanumeric' => array(
'allowEmpty' => false,
'message' => 'Please insert title',

Controller Contact:
var $name = 'Contact';
var $helpers = array('Html', 'Form');
var $components = array('Email');

function index() {

if (!empty($this->data))
if ($this->Contact->validates())
echo 'done';

View Index

Contact me:


echo $form->input('email');
echo $form->input('title');
echo $form->input('message',array('type'=>'textfield'));
echo $form->input('attachments');


25 11 2008
24 de noviembre de 2008. De CakePHP 1.1 a CakePHP 1.2. El baile de los errores. Parte IV « Tutorial CakePHP informal, sobre la marcha. ¡Participa!

[…] Por otra parte, el uso del método validates de la clase Model, que comprueba si los campos de un registro satisfacen las reglas de validación que se definen en el modelo, también ha cambiado. Esto lo explican aquí.  […]

3 02 2009

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: