Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Form::slect and DB query (Selected value)
  • Hello :)

    I have a select box in my form populated with a db query and everyting is ok, but i want to set a default selected value to the sleect box but i can not achieve this. My code atm is:
    In my view:

    <?php echo Form::select('department_id', '', $department, array('class' =--> 'span4')); ?

    In my controller:

     		$this->template->set_global('department', Model_User::get_department(), false);
    $this->template->title = "Users";
    $this->template->content = View::forge('admin\user/create');

    In my model:

    	public static function get_department()
    {
    $query = DB::select('id', 'department')->from('departments')->execute();
    $result = $query->as_array('id', 'department');
    return $result;
    }
    I have tried to set the 2nd parameter in the select as a string 'Select one' but it doesnt appear, i just need a defaut label without value, how can i get it to work?

    Thank you very much




  • What do you mean by "default selected value"?

    If you want to select a value from the array as the default, you have to add a value to the attribute array:
    <?php echo Form::select('department_id', '', $department, array('value' => $selected, 'class' => 'span4')); ?
    Or do you mean adding a placeholder?
  • Well both things, placeholder for a new record... and a selected value from the array, which is the current value for the current user being edited, in the edit scenario
  • The simplest way to add a placeholder is to add "array('' => 'select me!')" to the front of the options array, as the first value will be selected if no option is selected.

    You select an existing value as I showed you before, by using the 'value' key in the attribute array.
  • Well the placeholder is done, but i dont get how to set the value fo the user being edited as the selected one in the edit form. :/ ... the $department is a array returned by a model which did a sql query 
  • I assume you have a the selected value somewhere? For example from the model you load and display? Is that department_id?

    Lets assume it is:
    <?php echo Form::select('department_id', '', $department, array('value' => $department_id, 'class' => 'span4')); ?

  • Yes, actually it is $user->department_id However i have tried it the first time you said me that, but still in the select box the default value is the first value that apears in the array, not the user value

    Model:


    public function action_edit($id = null)
    {
    $user = Model_User::find($id);
    !($user and $id) and Response::redirect('admin/user');

    $val = Model_User::validate('edit');


    if ($val->run())
    {
    $user->username = Input::post('username');
    $user->name = Input::post('name');
    $user->email = Input::post('email');
    $user->mobile = Input::post('mobile');
    $user->birthday = Input::post('birthday');
    $user->department_id = Input::post('department_id');
    $user->rank_id = Input::post('rank_id');
    $user->updated_at = CEDate::now();

    if ($user->save())
    {
    Response::redirect('admin/user');
    }

    else
    {
    $this->template->set_global('msg', $val->show_errors(),FALSE);
    }
    }

    else
    {
    if (Input::method() == 'POST')
    {
    $user->username = $val->validated('username');
    $user->name = $val->validated('name');
    $user->email = $val->validated('email');
    $user->mobile = $val->validated('mobile');
    $user->birthday = $val->validated('birthday');
    $user->department_id = $val->validated('department_id');
    $user->rank_id = $val->validated('rank_id');

    $this->template->set_global('msg', $val->show_errors(),FALSE);
    }
    $this->template->set_global('user', $user, false);
    }
    $this->template->set_global('rank', Model_Rank::get_rank(), false);
    $this->template->set_global('department', Model_Department::get_department(), false);
    $this->template->title = "Users";
    $this->template->content = View::forge('admin\user/edit');

    }



    The form field:



    <?php echo Form::label('Department', 'department_id'); ?>


    <?php echo Form::select('department_id', '', $department, array('value' => $user->department_id, 'class' => 'span4')); ?>






    As you can see, the load model has all the user data which includes User's department_id.

    Thank you for your time :)
  • HarroHarro
    Accepted Answer
    I've never used the Form class directly (I always use fieldsets) so I had to look it up.
    The selected values is the second parameter of Form::select(), it is also documented that way.

    You pass an empty string ( "" ), so if that is not a valid value in the options array, the first value will be selected.
  • I see.... well  i would like to use Fieldsets instead of Form/Validation class but, i dont get it very well when trying to customize the form output as well as errors, which in my opinion is easier to manipulate using the form helper.

    Thank you for your help :) and ill to try to fix this :)
  • You were completly right!!! The selected value is the second parameter in the Form::select(), so that is why it didnt work.... we was trying to set it in the attrb array, so four parameter of Form::select(), now it works very well, setting it like this:


    <?php echo Form::select('department_id', $user->department_id, $department, array('class' => 'span4')); ?>

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion