Skip to content

Instantly share code, notes, and snippets.

@grandmanitou
Created January 28, 2014 15:59
Show Gist options
  • Save grandmanitou/8670362 to your computer and use it in GitHub Desktop.
Save grandmanitou/8670362 to your computer and use it in GitHub Desktop.
CakePHP v2.x validate HABTM
public function beforeRender() {
// Enable HABTM validation
$model = Inflector::singularize($this->name);
foreach($this->{$model}->hasAndBelongsToMany as $k=>$v) {
if(isset($this->{$model}->validationErrors[$k]))
{
$this->{$model}->{$k}->validationErrors[$k] = $this->{$model}->validationErrors[$k];
}
}
}
public function beforeValidate() {
foreach($this->hasAndBelongsToMany as $k=>$v) {
if(isset($this->data[$k][$k]))
{
$this->data[$this->alias][$k] = $this->data[$k][$k];
}
}
}
public $validate = array(
'Type' => array(
'multiple' => array(
'rule' => array('multiple',array('min' => 2)),
'message' => 'Please select at least 2 types'
),
),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment