cakephp multiple select validation
I am using CakePHP and i have something like:
PRODUCT -------> PRODUCT_开发者_如何学GoCATEGORY <---------- 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?
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: 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/
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.. :)
精彩评论