HABTM form validation in CakePHP

I have a Projects table and a Users table that are linked by a HABTM relationship. On the "Add" page of a new project, I have several checkboxes to select "Users for a new project." I want to have at least one user for the project. What is the best way to approach this in CakePHP?

+5
source share
7 answers

Try the following:

// app/models/project.php
/**
 * An additional validation check to ensure at least one User is
 * selected. Spoofs Cake into thinking that there are validation
 * errors on the Project model by invalidating a non-existent field
 * on the Project model, then also invalidates the habtm field as
 * well, so when the form is re-displayed, the error is displayed
 * on the User field.
 **/
function beforeValidate() {
  if (!isset($this->data['User']['User'])
  || empty($this->data['User']['User'])) {
    $this->invalidate('non_existent_field'); // fake validation error on Project
    $this->User->invalidate('User', 'Please select at least one user');
  }
  return true;
}
+19
source

I came across the same problem, but now - after 3 years - with CakePHP 2.3 .

Be clear; Grouphas and belongs User. I had this form:

// View/Groups/add.ctp
echo $this->Form->input('name');
echo $this->Form->input('User');

With the validation rule, as in user448164, answer:

// Model/Group.php
public $validate = array(
    'User' => array(
        'rule' => array('multiple', array('min' => 1)),
        'message' => 'Please select one or more users'
    )
);

, , Googling , , . , :

// View/Groups/add.ctp
echo $this->Form->input('name');
echo $this->Form->input('Group.User');

, , , .

, - .

CakePHP 2.4.x(, 2.3.x)

, CakePHP 2.3.x. . , , CakePHP 2.4.x, .

, :

$data = array(
    'User' => array(
        'Client' => array(8)
    ),
);
$this->User->create();
$this->User->saveAll($data);

: , "" , . , "", deep. :

$data = array(
    'User' => array(
        'Client' => array(8)
    ),
);
$this->User->create();
$this->User->saveAll($data, array('deep' => true));

! . :)

(2014/03/06)

, hasMany habtm. , . .

, Group.User User . User.

+12

, habtm, , .

, , ,

beforeValidate()

// habtm

  foreach($this->hasAndBelongsToMany as $k=>$v) {
   if(isset($this->data[$k][$k]))
   {
    $this->data[$this->alias][$k] = $this->data[$k][$k];
   }
  }

:

'User' => array(
   'rule' => array('multiple', array('min' => 1)),
   'message' => 'Please select one or more users'
  )
+6

teknoid . - , . http://teknoid.wordpress.com/2008/10/16/how-to-validate-habtm-data/

, Tag HABTM Post (:: Project HABTM Users):

-, , . .

+2

2016 CakePhp 2.7

: HABTM CakePHP 2.x


TL; DR;

AppModel.php

public function beforeValidate($options = array()){
   foreach (array_keys($this->hasAndBelongsToMany) as $model){
     if(isset($this->data[$model][$model]))
       $this->data[$this->name][$model] = $this->data[$model][$model];
   }
   return true;
 }

 public function afterValidate($options = array()){
   foreach (array_keys($this->hasAndBelongsToMany) as $model){
     unset($this->data[$this->name][$model]);
     if(isset($this->validationErrors[$model]))
       $this->$model->validationErrors[$model] = $this->validationErrors[$model];
   }
   return true;
 }

HABTM:

public $validate = array(
    'Tag' => array(
        'rule' => array('multiple', array('min' => 1)),
        'required' => true,
        'message' => 'Please select at least one Tag for this Post.'
        )
    );
+2

CakePHP 2.3.x, , GuidoH, HABTM :

public function beforeSave($options = array()){
  foreach (array_keys($this->hasAndBelongsToMany) as $model){
    if(isset($this->data[$this->name][$model])){
      $this->data[$model][$model] = $this->data[$this->name][$model];
      unset($this->data[$this->name][$model]);
    }
  }
  return true;
}
+1

Guido , Guido , beforeSave , .

Cake 2.4.5 +

public function beforeSave($options = array()) {
    $temp = $this->data['Group']['User'];
    unset($this->data['Group']['User']);
    $this->data['User']['User'] = $temp;

    return true;
}
0

All Articles