CakePHP Could not find validation handler

I am struggling with my cakePHP check

Scenario: In my database, I have one "alliances" table and one "federation". In "federations", ties between alliances remain. "alliances" have silly cols such as id, name, etc. federations are as follows:

id, request_alliance, accept_alliance, type, requested_at, accepted_at 

where request_alliance and accept_alliance are FK for alliances, type 1 or 2.

So my model looks like this:

 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 (created by a former partner, I just added $ hasMany)

class Alliance extends 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)); 

}

So far I have written an opinion on the addition of a new federation between the two alliances. Controller for this

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

When I try to add a new federation, the above function is called, and the new row is stored inside the database with the correct values. But the page still shows me the following errors.

 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 

I can’t imagine that my check was not completed, because a few hours ago I had an error that led to empty fields, and I received the correct verification message that this field cannot remain empty.

Can someone tell me where I am making a mistake that leads to these errors and how to fix it?

Thanks in advance!

+4
source share
1 answer

No validation rule definition

From the question, compare:

 'request_alliance' => array( 'required' => true, 'allowEmpty' => false ), 

WITH

 'type' => array( 'required' => true, 'allowEmpty' => false, 'rule' => array('between', 1, 2) ) 

In the first case, there is no rule, the rule is mandatory if you define the validation rules as an array.

Use the notEmpty validation rule

Of the validation rules, there seems to be a misunderstanding. You probably want a notEmpty rule:

 'request_alliance' => array( 'rule' => 'notEmpty' ) 

If you want to make sure that the field is present in all savings, use the necessary key

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

There is no need to define allowEmpty key , because it is the same as the rule for checking notEmpty if false, and illogical if it is defined as true.

+8
source

Source: https://habr.com/ru/post/1486393/


All Articles