【发布时间】:2014-01-26 16:02:34
【问题描述】:
请注意,我使用的是 CakePHP 1.3。
我想根据管理员在数据库中定义的限制来验证模型的几个字段的长度。
我有一个名为 Setting 的模型,通过它我可以获取 maxLength 值。我的想法是使用 beforeValidate 方法来获取它们,然后相应地设置 $validate 参数:
<?php
class Mod extends AppModel
{
var $belongsTo = array('IBelong');
var $hasMany = array('IHaz');
function beforeValidate() {
// Fetch the maxLength settings : requestAction to the SettingsController ?
// Somehow do $this->Setting->find... ?
// Then set the $validate attribute
}
}
?>
我可以使用什么方法在 beforeValidate 回调中获取 maxLength 值?
谢谢!
编辑:跟随 Dave 的 cmets,这是我目前正在做的事情(并且有效):
<?php
class Mod extends AppModel
{
var $belongsTo = array('IBelong');
var $hasMany = array('IHaz');
function beforeValidate() {
App::import('Model', 'Setting');
$setting = new Setting();
// Use $setting->find... to fetch the settings
// Set the $validate attribute to validate using the settings
}
}
?>
但是,我仍然不确定这是否是正确的方法。
请注意,我有几个要验证的属性,并且我希望避免多次调用 $setting->find 以获得最佳性能。
【问题讨论】:
标签: validation cakephp cakephp-1.3