CakePHP 中的模型验证


Model Validation in CakePHP

>我对验证规则有一些问题。

更新:

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

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

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

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

警告 (512):找不到邮政编码 [CORE''Cake''Model''Validator''CakeValidationRule.php,第 281 行] 的验证处理程序 maxLength]

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

警告 (512):找不到验证处理程序 maxLength for birth [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

蛋糕版本:2.4.3(对不起,我的英语不好,我是法国人^^)

检查 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',
),

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

公共$validate = 数组( 'login' => array( 'rule' => array('maxLength', 15), "消息" => "用户名长度不得超过 15 个字符。" ) );

更多信息:-cakephp.org

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