Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakePHP Could not find validation handler

I'm struggeling with my cakePHP validation

Scenario: In my DB I have one table "alliances" and one "federations". In "federations" connections between alliances are stored. "alliances" has got stupid cols like id, name, etc.. federations is like this:

id, request_alliance, accept_alliance, type, requested_at, accepted_at

where request_alliance and accept_alliance are FK to alliances, type is 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 an former partner, I only added the $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've written a view to add a new federation between two alliances. The 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 it the above function is called and a new row is stored inside the DB 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 validation is not done, because some hours ago I had a mistake which leads to empty fields and I got the correct validation message that this field can't left blank.

Can anyone tell me where I do the mistake which leads to these errors and how to correct it?

Thanks in advance!

like image 414
bish Avatar asked Jun 15 '13 14:06

bish


1 Answers

There is 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, rule is a mandatory key if you define the validation rules as an array.

Use the notEmpty validation rule

From the validation rules defined, it looks like there's a misunderstanding. You probably want the notEmpty validation rule:

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

If you want to ensure that the field is present in all saves, use the required key

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

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

like image 57
AD7six Avatar answered Nov 15 '22 03:11

AD7six