views:

232

answers:

4

Hi everybody,

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

+1  A: 

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

dhofstet
A: 

Thanks!

I already tried it with an appmodel. Now the Label also doesn't work.

Here is my appmodel:

class UserData extends AppModel {
   var $name = 'UserData';
   var $validate = array('nick_name' => array(
               'rule' => 'alphaNumeric',
               'label' =>'Nickname:',
               'error' =>'false')
       );
}
Schtief
MVC separation: Everything to do with data saving/fetching is in the Model, everything to do with presentation of that data is in the View. A validation rule has to do with data saving and goes into the Model, labels for form fields and these kind of things are View related. Read the Cookbook again, slowly, and pay attention to which fields go where. http://book.cakephp.org/view/127/One-Rule-Per-Field http://book.cakephp.org/view/189/Automagic-Form-Elements :)
deceze
Please don't answer to your own question with responses to or for other users. Edit your original question and comment on their answers.
Bobby
A: 

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

Aaron
A: 

^^ 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.

Mailslut
Thanks a lot,that was the problem!!!!!!!
Schtief