Welcome Guest, Not a member yet? Register   Sign In
[Solved] Form Helper set_value has unexpected results
#1

[eluser]Unknown[/eluser]
Quote:As I was typing this, I remembered that the set_value function will not work without a validation rule for the field. I set a validation rule and it works perfectly. Since I was 90% through the post when I figured that out, I thought I would share in case anyone else runs into the same problem.

Also, I am a bonehead, and I could have just used the second parameter in set_value to set the default value from the user options.

I am fairly new to CodeIgniter, so I am probably doing something wrong. I have a form that, when submitted, should return the value that the user entered. Beyond that, I have some default values that I am populating from user preferences. The following is a selection of the code that I am having trouble with:

Code:
class Ticket extends MY_Controller {
  const DEFAULT_ASSIGNMENT = 2;

  function index() {
    $uid = $this->session->userdata('user_id');
    $user = $this->user_model->getDetailsById($uid);

    $data['data']['assignment_options'] =
        array_to_select($this->assignment_model->getAll(), 'id', 'name');
    $data['data']['selected_assignment'] =
        $_POST ? set_value('assign_to') : $this::DEFAULT_ASSIGNMENT;

    $data['data']['department_options'] =
        array_to_select($this->department_model->getAll(), 'id', 'name');
    $data['data']['selected_department'] =
        $_POST ? set_value('department') : $user->department_id;

    $this->load->view('includes/template', $data);
  }
}

A selection from the view is as follows:

Code:
<?php
  echo form_label('Assign To', 'assign_to', array('class' => 'bold'));
  echo form_dropdown('assign_to', $assignment_options, $selected_assignment);

  echo form_label('Department', 'department', array('class' => 'bold'));
  echo form_dropdown('department', $department_options, $selected_department);
?>

The problem that I have is with the set_value('assign_to') in the controller. For whatever reason, it doesn't want to work, but the exact same code on the department works flawlessly. If I submit the form and redirect to the index function, the value for assign_to does not get properly set. If I change it to read:

Code:
$data['data']['selected_assignment'] =
  $_POST ? $this->input->post('assign_to') : $this::DEFAULT_ASSIGNMENT;

it will work perfectly.

Any thoughts?




Theme © iAndrew 2016 - Forum software by © MyBB