Multiple Flag Check (HABTM) Issues in CakePHP Form

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 .

+3
1

, , , .

, , . CakePHP v1.2, , HABTM , .. Request->BrochuesRequest->Brochure. , .

-, , , save / saveAll beforeValidate, validates. , Request->set. .

, $field, invalidate. , , , , , .. $form->input('BrochuresRequest.brochures_id'), $this->BrochuresRequest->invalidate('BrochuresRequest.brochures_id').

, , $this->BrochuresRequest->invalidate('brochures_id').

<?php   
// requests/add view
echo $form->input('BrochuresRequest.brochures_id', array('multiple' => true));

// requests_controller
function add() {
    if (!empty($this->data)) {
        $this->Request->create();
        // critical set to have $this->data 
        // for beforeValidate when calling validates
        $this->Request->set($this->data); 
        if ($this->Request->validates()) {
            $this->Request->saveAll($this->data);
        }
    }
}

// request model
function beforeValidate() {
    if (count($this->data['BrochuresRequest']['brochures_id']) < 1) {
        $this->invalidate('non_existent_field'); // fake validation error on Project
        // must be brochures_id and not BrochuresRequest.brochures_id
        $this->BrochuresRequest->invalidate('brochures_id', 'Please select 1');
        return false;
    }
    return true;
}
?>

, :

  • $form- >
  • isset, , $this->data.
  • beforeValidate should return false if you want it to prevent the save action.
0
source

All Articles