php - cakePHP 找不到验证处理程序

标签 php validation cakephp model

我正在为我的 cakePHP 验证而苦苦挣扎

场景: 在我的数据库中,我有一张表“联盟”和一张“联盟”。在“联盟”中,存储了联盟之间的联系。 “联盟”有一些愚蠢的列,例如 ID、名称等。联盟是这样的:

id, request_alliance, accept_alliance, type, requested_at, accepted_at

其中 request_alliance 和 accept_alliance 是联盟的 FK,type 是 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))

  );

}  

Alliance(前合伙人创建,我只加了$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));

}

到目前为止,我已经编写了一个在两个联盟之间添加新联盟的 View 。这个的 Controller

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));
  }

}

当我尝试添加一个新的联盟时,上面的函数被调用并且一个新行以正确的值存储在数据库中。但是页面仍然显示以下错误

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'
)

如果要确保该字段存在于所有保存中,请使用 the required key

'request_alliance' => array(
    'rule' => 'notEmpty',
    'required' => true
)

无需定义 allowEmpty key ,因为如果为假,则与 notEmpty 验证规则相同,如果定义为真,则不合逻辑。

关于php - cakePHP 找不到验证处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17124703/

相关文章:

java - 验证架构是有效的 JSON 架构而不是数据

java - Spring MVC : How to perform validation?

jquery - 复选框条件更改时如何使用 ajax 更新 mysql 数据库?

php - CakePHP:访问其他模型/app_model.php 中的模型以验证 Banknumber

php - 来自其他 MYSQL DB 的 wp_insert_post,出现 wordpress 错误

php - 为行分配数字,删除后返回正确的顺序

php - 是否可以使用 post 将多个表单发送到 php self

php - Laravel - selectRaw( count(*) ) 返回为字符串而不是整数?

validation - Grails 2.4 中空白约束的目的?

mysql - cakephp 带有子查询和 if 的虚拟字段