Quick fix for HABTM Validation
I was looking for a simple solution for HABTM validation, and struggled to find one that didn't required me to write a lot of code.
i came up with this, hopefully it will help someone.
This is my first post here, so i guess someone will rewrite my code to be much better.
i came up with this, hopefully it will help someone.
This is my first post here, so i guess someone will rewrite my code to be much better.
basically we want to add to the app_model.php the next code
and then just add a simple "multiple" validation rule with the name of the HABTM connection
let's say we have a baked code that looks like this
all we do is add
more info about the multiple validation rule here http://book.cakephp.org/view/786/multiple
Model Class:
<?php
function beforeValidate() {
foreach($this->hasAndBelongsToMany as $k=>$v) {
if(isset($this->data[$k][$k]))
{
$this->data[$this->alias][$k] = $this->data[$k][$k];
}
}
}
?>
and then just add a simple "multiple" validation rule with the name of the HABTM connection
let's say we have a baked code that looks like this
Model Class:
<?php
var $hasAndBelongsToMany = array(
'Project' => array(
'className' => 'Project',
'joinTable' => 'projects_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'project_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
?>
all we do is add
Model Class:
<?php
var $validate = array(
'Project' => array(
'multiple' => array(
'rule' => array('multiple',array('min' => 2)),
'message' => 'Please select at least 2 projects you attend'),
),
);
?>
more info about the multiple validation rule here http://book.cakephp.org/view/786/multiple








My Opportunity Model
var $hasAndBelongsToMany = array(
'Mood' => array(
'className' => 'Mood',
'joinTable' => 'opportunities_moods',
'foreignKey' => 'opportunity_id',
'associationForeignKey' => 'mood_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
var $validate = array(
'name' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Required field',
),
'Mood' => array(
'multiple' => array(
'rule' => array('multiple',array('min' => 2)),
'message' => 'Please select at least 2 mood you attend'),
),
));
function beforeValidate() {
foreach($this->hasAndBelongsToMany as $k=>$v)
{
if(isset($this->data[$k][$k]))
{
$this->data[$this->alias][$k] = $this->data[$k][$k];
}
}
}
My View
New Opportunity
Form->create('Opportunity');?> Form->input("id"); ?> Form->input("name"); ?> Form->input("code"); ?> Form->input('Mood',array('type' => 'select', 'multiple' => 'checkbox')); ?> Form->end('Save'); ?>
Any Ideas??
Needed to implement Brandon's fix too.
Clean and versatile code from both of you.
Many thanks!
Controller Class:
<?php
class AppController extends Controller {
function beforeRender()
{
$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];
}
}
}
}
?>
Now my error message shows up as I expect it to.
this way we can check it in the current model.
@Daniel Hollands I couldn't find the problem, as for me it shows errors.
I'm using cakephp 1.3 and use something like that in the view
echo $form->input('School', array('title' => __('Select Schools',true),'label' => __('School',true)));
Another thing i would suggest is using some sort of JS validaton, that auto generates from the validation rules.
both jquery and prototype have very good plugins for that.
Thanx a lot.
the normal rule for multiple is
Model Class:
<?php
var $validate = array(
'multiple' => array(
'rule' => array('multiple', array('in' => array('do', 'ray', 'me', 'fa', 'so', 'la', 'ti'), 'min' => 1, 'max' => 3)),
'message' => 'Please select one, two or three options'
)
);
?>
I have the same problem i'm using cakephp 1.3.1
in my view
Form->input('Agency',array('label'=>__('Agencias',true))); ?>
Comments are closed for articles over a year old