【问题标题】:cakephp ignores numeric validation - saves everythingcakephp 忽略数字验证 - 保存所有内容
【发布时间】:2013-08-14 13:15:27
【问题描述】:

我的问题是 cakephp 中的数字验证不适用于一种模型。我找不到原因。此模型中的其他验证工作正常,例如电子邮件...

我的规则:

...
'fone' => array(
    'Not empty' => array(
        'rule' => 'notEmpty',
        'message' => 'Please enter your fonenumber.'
    ),
    'Numeric' => array(
        'rule' => 'numeric',
        'message' => 'Please enter your fonenumber.'
    )
),
...

db 字段是 varchar,如果我尝试 123abc 之类的内容,它会被保存。没有错误信息!

我试过了:

public function beforeValidate($options) {
    debug($this->data);
    exit;
}

查看字段是否已传输 - 它们是,否则不会保存值。

$this->Address->save($this->request->data) 总是返回 true。

提前致谢!

【问题讨论】:

  • 你不需要两条规则。 numeric 已经包含不为空(可能除了 0)。还要尽量避免键中的空格。
  • 好的,我更改了键(没有空格)但没有更改。我也尝试了小写字母,但同样的问题,我也在你的博客中阅读了关于字段列表的内容,但没有任何效果

标签: php validation cakephp


【解决方案1】:

试着改成这个

'fone' => array(
    'Numeric' => array(
        'rule' => 'numeric',
        'message' => 'Please enter your fonenumber.'
    ),
    'Not empty' => array(
        'rule' => 'notEmpty',
        'message' => 'Please enter your fonenumber.'
    ),
),

希望对你有帮助。

【讨论】:

  • 谢谢,但也返回 true,我也只尝试了数字但没有改变。顺便说一下 notEmpty-Rule 工作正常......如果我尝试自定义规则,它也会被忽略 - 即使我输入 $this->Address->save($this->request->data, true);这应该强制验证
  • 你使用的是哪个版本的 cakephp?
【解决方案2】:

我明白了,

这是我的错...我在模型中有一个无效函数,它在控制器中被调用。我通过检查访问此模型的其他表单找到了它

  • 对不起!!!

【讨论】:

    猜你喜欢
    • 2011-06-16
    • 1970-01-01
    • 2014-05-23
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多