我正在为我的cakePHP验证而挣扎
场景:在我的数据库我有一个表"联盟"和一个"联盟"。在"federations"中,联盟之间的连接被存储。"联盟"有愚蠢的标签,比如身份、名字等等。Federations是这样的:
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)
类Alliance扩展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
我无法想象我的验证没有完成,因为几个小时前我有一个错误,导致空字段,我得到了正确的验证消息,这个字段不能留空。
谁能告诉我我在哪里做的错误,导致这些错误和如何纠正它?
提前感谢!
没有验证规则定义
从问题,比较:
'request_alliance' => array(
'required' => true,
'allowEmpty' => false
),
'type' => array(
'required' => true,
'allowEmpty' => false,
'rule' => array('between', 1, 2)
)
在第一种情况下,没有规则,如果您将验证规则定义为数组,则规则是一个强制键。
使用notEmpty验证规则
从定义的验证规则来看,似乎有一个误解。您可能需要notEmpty验证规则:
'request_alliance' => array(
'rule' => 'notEmpty'
)
如果要确保该字段在所有保存中都存在,请使用所需的键
'request_alliance' => array(
'rule' => 'notEmpty',
'required' => true
)
不需要定义allowEmpty键,因为它与notEmpty验证规则相同,如果定义为false,则不符合逻辑。