我正在使用cakePHP验证
方案: 在我的数据库中,我有一个表“联盟”和一个“联盟”。在“联盟”中,存储联盟之间的联系。 “联盟”有像id,name等愚蠢的cols。联合会是这样的:
id, request_alliance, accept_alliance, type, requested_at, accepted_at
其中request_alliance和accept_alliance对联盟是FK,类型为1或2。
所以我的模型看起来像这样:
class Federation extends AppModel
{
// Bundarten:
// 1 - NAP
// 2 - Bund
public $displayField;
var $belongsTo = array('Alliance_requesting' => array('className' => 'Alliance', 'foreignKey' => 'request_alliance'),
'Alliance_accepting' => array('className' => 'Alliance', 'foreignKey' => 'accept_alliance'));
public $validate = array(
'request_alliance' => array('required' => true, 'allowEmpty' => false),
'accept_alliance' => array('required' => true, 'allowEmpty' => false),
'type' => array('required' => true, 'allowEmpty' => false, 'rule' => array('between', 1, 2))
);
}
联盟(由前合伙人创建,我只添加了$ hasMany)
类联盟扩展了AppModel {
var $hasMany = array(
'Federation_requesting' => array('className' => 'Federation', 'foreignKey' => 'request_alliance', 'dependent' => true),
'Federation_accepting' => array('className' => 'Federation', 'foreignKey' => 'accept_alliance', 'dependent' => true)
);
public $validationDomain = 'alliance';
public $validate = array(
'tag' => array(
'uniqueTag' => array(
'rule' => 'isUnique',
'message' => 'Alliance tag already in use'),
'between' => array(
'rule' => array('between', 2, 15),
'message' => 'Alliance tag must betwenn %d to %d characters')),
'name' => array(
'rule' => array('between', 3, 30),
'message' => 'Alliance name must between %d to %d characters'),
'image_url' => array(
'rule' => 'url',
'message' => 'Alliance picture must be a valid URL',
'allowEmpty' => true),
'homepage' => array(
'rule' => 'url',
'message' => 'Homepage must be a valid URL',
'allowEmpty' => true));
}
到目前为止,我已经写了一个视图,在两个联盟之间添加一个新的联盟。这个控制器
class FederationsController extends AppController
{
var $name = 'Federations';
var $components = array('Message');
var $uses = array('Alliance', 'Federation');
// Requesting new federation
function add()
{
if(empty($this->data['Federation'])) {
$message = __d('federation', "Invalid Request");
$this->notice($message);
return $this->redirect(Path::overall_highscore_alliances_path());
}
$requesting_alliance_id = $this->data['Federation']['req_alliance_id'];
$req_alliance = $this->Alliance->get($requesting_alliance_id);
if(!$req_alliance) {
return $this->redirect(Path::overall_highscore_alliances_path());
}
if(!$this->Alliance->isCurrentUserDiplomat($req_alliance)) {
$message = __d('federation', "Only the diplomat is allowed to modify federations.");
$this->notice($message);
return $this->redirect(Path::alliance_path($requesting_alliance_id));
}
$accepting_alliance_id = $this->data['Federation']['acc_alliance_id'];
$acc_alliance = $this->Alliance->get($accepting_alliance_id);
if(!$acc_alliance) {
$message = __d('federation', "The target alliance for this federation doesn't exists.");
$this->notice($message);
return $this->redirect(Path::alliance_path($requesting_alliance_id));
}
$type = $this->data['Federation']['type'];
$requested_at = time();
$this->Federation->create();
$values = array('request_alliance' => $requesting_alliance_id,
'accept_alliance' => $accepting_alliance_id,
'type' => $type,
'requested_at' => $requested_at);
$saved = $this->Federation->save($values, true, array('request_alliance', 'accept_alliance', 'type', 'requested_at'));
$name = h($acc_alliance['name']);
$message = $saved ? __d('federation', "Federation with '%s' successfully requested.", $name) : '';
$this->notice($message);
$this->errors($this->Federation->validationErrors);
$this->redirect(Path::alliance_path($requesting_alliance_id));
}
}
当我尝试添加新的联合时,将调用上述函数,并在DB内部使用正确的值存储新行。但该页面仍然显示以下错误
Could not find validation handler 1 for request_alliance
Could not find validation handler for request_alliance
Could not find validation handler 1 for accept_alliance
Could not find validation handler for accept_alliance
我无法想象我的验证没有完成,因为几个小时前我有一个错误导致空字段,我得到了正确的验证消息,该字段不能留空。
有谁能告诉我在哪里犯了导致这些错误的错误以及如何纠正错误?
提前致谢!
答案 0 :(得分:8)
从问题中,比较:
'request_alliance' => array(
'required' => true,
'allowEmpty' => false
),
使用
'type' => array(
'required' => true,
'allowEmpty' => false,
'rule' => array('between', 1, 2)
)
在第一种情况下没有规则,如果您将验证规则定义为数组,则规则是必需的密钥。
从定义的验证规则来看,似乎存在误解。您可能需要notEmpty验证规则:
'request_alliance' => array(
'rule' => 'notEmpty'
)
如果您想确保所有保存中都有该字段,请使用the required key
'request_alliance' => array(
'rule' => 'notEmpty',
'required' => true
)
没有必要定义allowEmpty key,因为如果为false则与notEmpty验证规则相同,如果定义为true则与notical不相同。