Schtief
Schtief

Reputation: 3

Cake PHP - Form Validiation doesn't work

my CakePHP (1.2.5.) doesn't validate my form correct.

 $this->UserData->save($this->data);

gives me always a true value back. I can't find the problem. The label for UserData.nichname works.

That's the View:

<?php
echo $form->create('UserData');
echo $form->error('UserData.nick_name');
echo $form->input('UserData.nick_name', array('id' => 'UserDatanick_name', 'rule' => 'alphaNumeric', 'label' =>'Nickname:', 'error' =>'false'));
echo $form->end( array( 'label' => ' Save ') );
?>

Here is my Controller:

class UserDatasController extends AppController {
  var $name = 'UserDatas';
  function add(){
     if (!empty ($this->data)){
         $this->UserData->create();
         if ($this->UserData->save($this->data)){
             $this->Session->setFlash('input is valid');
         } else {
             $this->Session->setFlash('input is not valid');
         }
     }
  }
}

The rules for are not in the model, that's the reaseon i don't post it.

What else is necessary for a validation?

Thanks in advance Steve

Upvotes: 0

Views: 1401

Answers (3)

Dal Hundal
Dal Hundal

Reputation: 3324

^^ also check what your files are called. you have named your model file user_data.php right? and your controller user_data_controller.php?

Note the underscores because of your CamelCasing. If you get the model file name wrong, it wont complain but will instead use a default magic model - which could be why your validation rules within the model didnt get picked up.

Upvotes: 0

Aaron
Aaron

Reputation: 316

I believe you only specify the rules in the model, but the label would be kept in the $form->input() function

Upvotes: 0

dhofstet
dhofstet

Reputation: 9964

The validation rules have to be defined in the model, not in the view, see also the chapter about data validation in the cakebook.

Upvotes: 1

Related Questions