views:

63

answers:

2

Hi, I am using CakePHP and i have something like:

PRODUCT -------> PRODUCT_CATEGORY <---------- CATEGORY

so one product can have 'n' categories and viceversa. The problem is that i would like to validate the products so that the have at least one category. Since I am using the Form assistant and the validate functions of CakePHP y have arrived to this:

class Product extends AppModel {
    var $name = 'Product';
    var $validate = array(
        'category_id' => array(
            'rule' => array('multiple', array('min' => 1)),
            'message' => 'You have to choose at least one category'
        )
    );
}

But it doesn't work, any ideas?

A: 

have you tried the NOTEMPTY rule? im assuming the list of categories is in a checkbox format, rite.. by default the category_id if empty. the only logic i can think is, if nothing checked, then it throw the error message.

correct me if im wrong.. :)

Faizal Heesyam
didn't work, but thanks!
leanyo martinet
A: 

I think you should not validate against category_id, instead use Category (the name of your model).

If this still doesn't work, you should be ablo to find a solution in this question on SO: http://stackoverflow.com/questions/2044102/habtm-form-validation-in-cakephp or have a look on this article: http://nuts-and-bolts-of-cakephp.com/2008/10/16/how-to-validate-habtm-data/

Tim