Short version
I have some HABTM checkboxes in the form. Validation works correctly (at least one flag must be checked to pass validation), but CakePHP message divisions are not generated as they should be.
Long version
I have an account that allows users to fill out their name and email address, and then select from the list of brochures (check boxes) that they would like to receive.
The form is as follows:
<?php
echo $this->Form->create('Request',array('action' => 'index'));
echo $this->Form->input('id');
echo $this->Form->input('name');
echo $this->Form->input('email');
echo $this->Form->input('Brochure',array(
'label' => __('Information Required:',true),
'type' => 'select',
'multiple' => 'checkbox',
'options' => $list,
'selected' => $this->Html->value('Brochure.Brochure'),
));
echo $this->Form->submit('Submit');
echo $this->Form->end();
?>
In my controller $list, it is installed like this:
$this->Request->Brochure->find('list',array('fields'=>array('id','name')));
After reading the second answer (posted by user448164) in CakePHP's HABTM form validation in Stack Overflow, I set up the request model as follows:
<?php
class Request extends AppModel {
var $name = 'Request';
function beforeValidate() {
foreach($this->hasAndBelongsToMany as $k=>$v) {
if(isset($this->data[$k][$k]))
{
$this->data[$this->alias][$k] = $this->data[$k][$k];
}
}
}
var $validate = array(
'name' => array(
'rule' => 'notEmpty',
'message' => 'Please enter your full name'
),
'email' => array(
'rule' => 'email',
'message' => 'Please enter a valid email address'
),
'Brochure' => array(
'rule' => array('multiple', array('min' => 1)),
'message' => 'Please select 1'
),
);
?>
99%. , , . , , Cake "error" <div>, <div class="error-message">Please select 1</div>, .
- .
, , HABTM. , div div .