0
votes

I have a controller with a add() function and create() function. The add function posts to create.

The form is displayed using the form helper. In the add() function, I have an array to set the form input attributes that looks like the following:

$this->data['form'] = array(
        'label_attributes' => array(
            'class' => 'col-lg-2 control-label'
        ),
        'media_name' => array(
            'class' => 'form-control',
            'id'    => 'media_name',
            'name'  => 'media_name',
            'value' => set_value('media_name')
        ),
        'media_link' => array(
            'class' => 'form-control',
            'id'    => 'media_link',
            'name'  => 'media_link',
            'value' => set_value('media_link')
        ),
        'media_width' => array(
            'class' => 'form-control',
            'id'    => 'media_width',
            'name'  => 'media_width',
            'size'  => '4',
            'maxlength' => '4',
            'value' => ($this->form_validation->set_value('media_width')) ? $this->form_validation->set_value('media_width') : '640'
        ),
        'media_height' => array(
            'class' => 'form-control',
            'id'    => 'media_height',
            'name'  => 'media_height',
            'value' => ($this->form_validation->set_value('media_height')) ? $this->form_validation->set_value('media_height') : '360'
        ),
        'media_description' => array(
            'class' => 'form-control',
            'id'    => 'media_desription',
            'name'  => 'media_desription',
            'value' => $this->form_validation->set_value('media_desription')
        )
    );

When I post to the create() function, I lose access to the data['form'] values. Should all this information just be in the view or is it possible to put it in the model so I can load it whenever needed? When I tried to put it in the model, I had issues with the 'value' attributes even if I loaded the form_validation library in the model.

1
If validation fails, I ran $this->add() from my controller to go back to the add() function. This works and shows errors, however, the URI is stays /create and I would like it to go back to /add. /create should only be for POST. - Sean

1 Answers

0
votes

Because the controller class is renewed when a new request comes, so the problem with your code is that, $this->data is created when you visit the add function, while when you post to create function, the controller class is renewed again, there is no $this->data at all at this time.

If you want to pass data from one request to another request, you can pass the data via view or model.

Hope helps!