Gordon
Gordon

Reputation: 57

cakephp select dropdown list

i have the following models: Student,Form,FormsStream.

the student model have foreign keys to both models. Student.form_id is a foreignKey to the form table,Student.stream_id is foreign to the FormsStream table. i already have entries for both forms and formsStreams.

to add the student Form and Stream i select both from respective drop-down list. i already created the drop-down list.

my Problem.

previously i was able to save/edit student form and stream but after making those fields foreign keys to be selected i cant save new or edit those fields. i use cakePHP 2.5.4. however i can do that in SQL. i cant seem to figure out the problem with the select lists.

my method to get the forms from the database

public function loadStudentForm() {
    $this->loadModel('Student'); //load the associated models
    $this->loadModel('Form');
    $forms = $this->Form->find('list',array('fields' => 'form_name')); //get the forms from the database
    $this->set('forms',$forms);
}

method to get streams from database

public function loadStreams() {
    $this->loadModel('FormsStream');
    $streams = $this->FormsStream->find('list',array('fields' => 'stream_name'));
    $this->set('streams',$streams);
}

student add method

public function addStudent() {
    if ($this->request->is('post')) {
        $this->loadStudentForm();
        $this->loadStreams();
        $this->Student->create();
        if ($this->Student->save($this->request->data)) {
            $this->Session->setFlash(__('New student record added to the database.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The student could not be added.Please ensure all fields are correctly entered'));
        }
    }
}

the extract from add.ctp for selecting student form and stream

echo $this->Form->input('forms',array('label' => 'Form')); 
echo $this->Form->input('streams',array('label' => 'Class stream'));

so far printing contents of validateErrors shows this:

array(
    'Student' => array(
        'form_id' => array(
            (int) 0 => 'You must select the form'
        ),
        'stream_id' => array(
            (int) 0 => 'You must choose a stream'
        )
    ),
    'Form' => array(),
    'FormsStream' => array()
)

i've even tried to intercept the request in burpsuite and i can see the form and stream id are passed.just dont get why they are empty in the above array

_method=POST&data%5BStudent%5D%5Badmission_no%5D=1002&data%5BStudent%5D%5Bfirst_name%5D=peter&data%5BStudent%5D%5Bmiddle_name%5D=per&data%5BStudent%5D%5Blast_name%5D=pee&data%5BStudent%5D%5Bgender%5D=Male&data%5BStudent%5D%5Bdate_of_birth%5D%5Bmonth%5D=11&data%5BStudent%5D%5Bdate_of_birth%5D%5Bday%5D=05&data%5BStudent%5D%5Bdate_of_birth%5D%5Byear%5D=2014&data%5BStudent%5D%5Bjoin_date%5D%5Bmonth%5D=11&data%5BStudent%5D%5Bjoin_date%5D%5Bday%5D=05&data%5BStudent%5D%5Bjoin_date%5D%5Byear%5D=2014&data%5BStudent%5D%5Bforms%5D=1&data%5BStudent%5D%5Bsstrong texttreams%5D=1

Upvotes: 1

Views: 528

Answers (1)

gmponos
gmponos

Reputation: 2277

The input must be form_id and stream_id. Even if you set them with plural form cakephp recognize it in the following form.

echo $this->Form->input('form_id',array('label' => 'Form')); 
echo $this->Form->input('stream_id',array('label' => 'Class stream'));

Upvotes: 1

Related Questions