在我的博客插件中,我想创建一个captcha,每当机器人学习如何解决它时,用户都可以更改它。我的想法是有两个配置设置:"plugin.blog.captcha_sanswer"answers"plugin.blog.captcha_question",如果用户没有设置,则默认设置。
然后在BlogPostComment模型中,有一个验证规则:
public $validate = array( 'captcha' => array(
'rule' => array('custom', $captcha_a)
, 'message' => 'Please answer the question, correctly and in lowercase.'
, 'required' => false
,
),
);
所以我需要在模型中实例化$captcha_a。我是这样做的:
public $captcha_q = (isset(Configure::read('Plugin.Blog.captcha_answer'))) ? Configure::read('Plugin.Blog.captcha_question') : "orange";
事实证明,在类变量声明期间不能调用函数。
否则我该怎么做?
您能在构造函数中设置这些变量吗?
public function beforeValidate($options = array()) {
parent::beforeValidate($options);
$this->validate['captcha'] = array(/* your rules here */);
}
最好的方法是将验证作为一个单独的函数,并将其传递到$validate数组中。
http://book.cakephp.org/2.0/en/models/data-validation.html#adding-您自己的验证方法