0
votes

I'm trying to create a validator for my models:

But taking the example from http://book.cakephp.org/:

  var $validate = array(
  'country' => array(
    'rule' => 'notEmpty'
 )
);

gives the following error: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash [CORE/cake/libs/model/app_model.php, line 166]

By googling this error I found a mailinglist entry that recommended using: ( http://cakephp.1045679.n5.nabble.com/validation-notEmpty-td1320629.html)

 'country' => array(
    'rule' => VALID_NOT_EMPTY,
    'message' => 'This field cannot be left blank.'
    )

which didn't work. so I tried:

'country' => array(
    'rule' => 'VALID_NOT_EMPTY',
    'message' => 'This field cannot be left blank.'
    )

Which marked the field as a required, but didn't stop me from leaving it blank.

My question is: how to do this correctly? I'm using CakePHP 1.3.6

3
are you sure that that's the place where the error is happening?Teej
yes. only when I add var $validate = array( 'country' => array( 'rule' => 'notEmpty' ) ); the error message appears.Chris

3 Answers

1
votes

Most probably you need to put the required key; something like this:

'country' => array(
    'rule' => 'notEmpty',
     required => true,
    'message' => 'This field cannot be left blank.'
    )

Hope this helps.

0
votes

Why don´t you use the commandline for baking your models (cmd: cake bake)? If you bake the models you can specify the validation rules there. It is fast and easy... and you can see how validation works.

It helped me a lot...

Here an example code.

'username' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'Username required',
            'allowEmpty' => false,
            'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),

You must use an array for the rule definition...

0
votes

in your view file just add 'class'='required' eg:

<?php echo $this->Form->input('new_email',array('class'=>'email required yellow', 'div'=>false, 'label'=>false));?>