0

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" 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!!!!

tereško
  • 58,060
  • 25
  • 98
  • 150
Elitmiar
  • 35,072
  • 73
  • 180
  • 229
  • I'm not sure that's the cause, but shouldn't it be `'equalTo' => '1'`? – bfavaretto Sep 28 '11 at 17:56
  • I also tried 'interest_id' => array( 'rule' => 'notempty', 'message' => __l('Required') , 'allowEmpty' => false ), – Elitmiar Sep 28 '11 at 17:58
  • 1
    You could also try something like this: http://stackoverflow.com/questions/2044102/habtm-form-validation-in-cakephp – bfavaretto Sep 28 '11 at 18:01
  • @bfavaretto - Thank you for pointing me in the right direction, I've found the problem and this was the issue.= $this->User->Interest->set($this->data); I forgot to add it – Elitmiar Sep 28 '11 at 18:19
  • @bfavaretto - You could always put it in a answer, since technically you helped me solve the problem – Elitmiar Sep 28 '11 at 18:51
  • Thanks, but nevermind. I tried to help, but you found the actual answer by yourself. – bfavaretto Sep 28 '11 at 19:01

1 Answers1

0

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

Community
  • 1
  • 1
Elitmiar
  • 35,072
  • 73
  • 180
  • 229