简体   繁体   English

CakePHP Validation :: naturalNumber()无法正常工作

[英]CakePHP Validation::naturalNumber() not working

Good morning (at least in Arizona). 早上好(至少在亚利桑那州)。

I am running a simple validation check on a Model. 我正在对模型运行简单的验证检查。 Nothing fancy. 没有什么花哨。

$this->loadModel('Something');
$data = array(
    'foo_id' => '1',
    'bar' => 'John'
); 
$this->Something->save($data);

----------------------------------------

class Something extends AppModel {
    public $validate = array(
        'foo_id' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'Foo id is missing',
                'required' => true
            ),
            'naturalNumber' => array(
                'rule' => 'naturalNumber',
                'message' => 'Foo id must be greater than 0',
                'required' => true
            )
        )
    );
}

This should be easy but Something.foo_id always fails the natural number validation. 这应该很容易,但是Something.foo_id总是会失败自然数验证。 Notice that Something.id is not defined, so save() should run an INSERT . 注意,未定义Something.id ,因此save()应该运行INSERT I have confirmed the value is greater than 0 and have even forced it to be an integer. 我已经确认该值大于0,甚至已将其强制为整数。 I'm out of ideas and would appreciate if anyone could hazard a guess at what's going on. 我没有主意,如果有人可以猜测发生了什么,我将不胜感激。 Thanks a lot for the help. 非常感谢您的帮助。

if you take a look at the documentation ( http://book.cakephp.org/2.0/en/models/data-validation.html#Validation::naturalNumber ) or the upgrade guide ( http://book.cakephp.org/2.0/en/appendices/2-2-migration-guide.html ) you should be able to notice that this rule has been added in 2.2 如果您查看文档( http://book.cakephp.org/2.0/en/models/data-validation.html#Validation::naturalNumber )或升级指南( http://book.cakephp.org /2.0/en/appendices/2-2-migration-guide.html ),您应该可以注意到此规则已在2.2中添加

so either upgrade or manually add this rule to your app model 因此,可以升级此规则或将其手动添加到您的应用模型中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM