CakePHP中的模型验证

时间:2013-12-18 01:01:40

标签: php validation cakephp model

我在验证规则方面遇到了一些问题。

更新:

  

警告(512):找不到姓氏的验证处理程序maxLength [CORE \ Cake \ Model \ Validator \ CakeValidationRule.php,第281行]

     

警告(512):找不到名字的验证处理程序maxLength [CORE \ Cake \ Model \ Validator \ CakeValidationRule.php,第281行]

     

警告(512):找不到手机的验证处理程序maxLength [CORE \ Cake \ Model \ Validator \ CakeValidationRule.php,第281行]

     

警告(512):找不到地址的验证处理程序maxLength [CORE \ Cake \ Model \ Validator \ CakeValidationRule.php,第281行]

     

警告(512):无法找到zipcode的验证处理程序maxLength [CORE \ Cake \ Model \ Validator \ CakeValidationRule.php,第281行]

     

警告(512):找不到城市的验证处理程序maxLength [CORE \ Cake \ Model \ Validator \ CakeValidationRule.php,第281行]

     

警告(512):找不到出生的验证处理程序maxLength [CORE \ Cake \ Model \ Validator \ CakeValidationRule.php,第281行]

public $validate = array(
    'username' => array(
        'alphanumeric' => array(
            'rule' => array('alphanumeric'),
            'allowEmpty' => false,
            'required' => true,
            'message' => "Alphanumeric characters only.",
        ),
        'between' => array(
            'rule' => array('between',4,20),
            'message' => "The username must be between %d and %d characters.",
        ),
        'isUnique' => array(
            'rule' => 'isUnique',
            'message' => "This username is already taken.",
        ),
    ),
    'password' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => "You must specify your password.",
        ),
    ),
    'passwordconfirm' => array(
        'between' => array(
            'rule' => array('between', 8, 20),
            'required' => true,
            'message' => "The password must be between %d and %d characters.",
        ),
        'equalTo' => array(
            'rule' => array('equalToField', 'password'),
            'message' => "Passwords are not identical.",
        )
    ),
    'oldpassword' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'required' => true,
            'message' => "You must specify your old password.",
            'on' => 'update',
        ),
    ),
    'lastname' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 25),
            'required' => true,
            'message' => "Your last name can't contain more than %d characters.",
        ),
    ),
    'firstname' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 25),
            'required' => true,
            'message' => "Your first name can't contain more than %d characters.",
        ),
    ),
    'mail' => array(
        'email' => array(
            'rule' => array('email'),
            'required' => true,
            'message' => "You must specify a valid Email address.",
        ),
        'isUnique' => array(
            'rule' => 'isUnique',
            'message' => "This Email is already taken.",
        ),
    ),
    'user_type' => array(
        'inList' => array(
            'rule' => array('inList', array('particular','professional','association')),
            'required' => false,
            'message'   => "Your choice is not valid.",
        ),
    ),
    'denomination' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 150),
            'message' => "Your company name can't contain more than %d characters.",
            'on' => 'update',
        ),
    ),
    'siret' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 45),
            'message' => "Your company registration number can't contain more than %d characters.",
            'on' => 'update',
        ),
    ),
    'phone' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 20),
            'message' => "Your phone number can't contain more than %d characters.",
            'on' => 'update',
        ),
    ),
    'address' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 255),
            'message' => "Your email address can't contain more than %d characters.",
            'on' => 'update',
        ),
    ),
    'zipcode' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 15),
            'message' => "Your zip code can't contain more than %d characters.",
            'on' => 'update',
        ),
    ),
    'city' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 150),
            'message' => "Your city can't contain more than %d characters.",
            'on' => 'update',
        ),
    ),
    'birth' => array(
        'maxLength' => array(
            'rule' => array('maxLength', 10),
            'message' => '%d characters maximum for your date of birth.',
            'allowEmpty' => true,
        ),
    ),
);

有时候,我也有这个错误:(如果可以帮助的话)

Fatal Error
Error: Call to undefined method Validation::getDataSource() 
File: Z:\wamp\www\gc2\lib\Cake\Model\Datasource\DboSource.php   
Line: 1062

Notice: If you want to customize this error message, create app\View\Errors\fatal_error.ctp

CakePHP的版本:2.4.3 (抱歉我的英文不好,我是法国人^^)

2 个答案:

答案 0 :(得分:1)

检查maxLength上的语法。它应该是驼色的。因为你使用了全部小写,所以找不到正确的方法。

您写道:

'rule' => array('maxlength', 25),

应该是

'rule' => array('maxLength', 25),

请参阅:http://book.cakephp.org/2.0/en/models/data-validation.html#Validation::maxLength

此外,您正在使用如下自定义名称编写规则:

'address' => array(
    'maxLength' => array(
        'rule' => array('maxLength', 255),
        'message' => "Your email address can't contain more than %d characters.",
        'on' => 'update',
    ),
),

尝试将其更改为:

'address' => array(
    'rule' => array('maxLength', 255),
    'message' => "Your email address can't contain more than %d characters.",
    'on' => 'update',
),

答案 1 :(得分:0)

此规则可确保数据保持在最大长度要求范围内。

  

public $ validate = array(       'login'=>阵列(           'rule'=>数组('maxLength',15),           'message'=> '用户名长度不得超过15个字符。       )   );

更多信息: - cakephp.org

http://book.cakephp.org/2.0/en/models/data-validation.html