开发者

Validation Rule not working in cakePHP

开发者 https://www.devze.com 2023-04-08 04:26 出处:网络
I have a registration page and within this registration page the user will be required to fill in some interest, the interests values is called from a model with the name interest.php, all values are

I have a registration page and within this registration page the user will be required to fill in some interest, the interests values is called from a model with the name interest.php, all values are pulling through and saves as expected.

The relationship to the user model is

var $hasAndBelongsToMany = array(
        'Interest' => array(
            'className' => 'Interest',
            'joinTable' => 'users_interests',
            'foreignKey' => 'user_id',
            'associationForeignKey' => 'interest_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        )

Whenever I add a validation rule in interest.php the little required star appears next to the checkboxes but it is not validating at all

<label for="InterestInterestId">Interests</label>
<input type="hidden" value="" name="data[Interest][interest_id]">
<div class="checkbox">
<input id="InterestInterestId8" type="checkbox" value="8" name="data[Interest][interest_id][]">
<label for="InterestInterestId8">Interest 1</label>
</div>
<div class="checkbox">
<input id="InterestInterestId1"开发者_Go百科 type="checkbox" value="1" name="data[Interest][interest_id][]">
<label for="InterestInterestId1">Interest2</label>
</div>

in my view I call the multiple checkboxes like this

 echo $form->input('Interest.interest_id', array('label' => __l('Interests'), 'multiple' => 'checkbox'));

This is my validation rule in interest.php

$this->validate = array(

            'interest_id' => array(
                'rule' => array(
                    'equalTo',
                    '1'
                ) ,
                'message' => __l('Please select some interests')
            )
        );

Am I doing something wrong here, or missing something, any help will be appreciated!!!!


Found the answer with the help of this link HABTM form validation in CakePHP provided by bfavaretto

The problem was this $this->User->Interest->set($this->data);

I forgot to add it

0

精彩评论

暂无评论...
验证码 换一张
取 消