开发者

Cakephp: Form Vailidation

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


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.


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


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));?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜