Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Populate form select box with related values from database
  • I've just started using FuelPHP today and I've been making great progress but I've hit a problem that I haven't been able to move forward on for some time.

    I have a table named 'book' which contains the field 'genre_id'. And a table named 'genre' holding the matching 'id' and genre 'value'.

    I have a view/form (created by scaffolding) that allows you to create a new record in 'book', along with the genre_id. But I don't want the form to display the genre_id, I want it to display the joined genre value from the 'genre' table so that the user can see what it is. But then have it save the genre_id back to the 'books' table.

    I suspect I'll need to create an array or key-value pairs (genre_id-genre), and use that in my form select box. But I'm not sure how to start with it.

    Could anyone point me in the right direction? I've been looking at the documentation but I can't figure this out.

    Apologies if this is in the wrong forum.
  • There are several solutions possible, depending on how many times you need to generate the fieldset, and how volatile your data is.

    In general, you need it only once, when the fieldset is created. The best way to handle this is to add a method to your model called "set_form_fields".

    public static function set_form_fields($form, $instance = null)
    {
        // create the base fieldset
        parent::set_form_fields($form, $instance);

        // populate the dropdown options, with a blank first entry
        $form->field('dropdown')->set_options(
            \Arr::merge(
                array('' => ''),
                \DB::select('id', 'value')->from('table')->order_by('value', 'ASC')->execute()->as_array('id', 'value')
            )
        );
    }

    It is called when you generate a fieldset from a Model definition, and allows you to customize the generated standard fieldset object.
  • Thanks Harro, I've added this code to my model and am looking at documentation trying to generate a fieldset from a model definition, as you've suggested. But I'm not having much luck.

    Could you point me in the right direction another time?

    If you have any to hand, it would be great to see an end-to-end example (ie "this goes in the model, this goes in the controller, this goes in the view" etc) so that I can visually follow the logic. If nothing like this exists, I will write up my solution once I have it working!

    Appreciate your help with this (I'm fairly new to MVC and I'm reading up on it on the side).
  • I usually do:

    // create the model fieldset
    $form = \Fieldset::forge('formname');
    $form->form()->add_csrf();
    $form->add_model('\\Model\\Modelname');

    ( I namespace all my classes, instead of using underscores ).

    You then pass it on to the form:

    View::forge('some/view')->set('form', $form, false);

    And in the view:

    echo $form;

    This will generate a standard form, based on the form template in the config file. If you need more complex forms, you can do things in your view like:

        // disable the form open/close tags for the fieldset, we'll do that manually
        $form->set_config(array('form_template' => '{fields}'));

        echo Form::open(array('action' => Uri::main(), 'class' => 'focus', 'onsubmit' => 'return fuel_set_csrf_token(this);', 'novalidate' => 'novalidate'));

        echo '    <fieldset>
            <legend>'.__('candidates.form.'.$type).'</legend>
            <dl>'.
            ($type=='edit' ? $form->field('id')->build() : '').
            ($type=='edit' ? $form->field('added')->build() : '').
            $form->field('session_id')->build().
            '        </dl>
            </fieldset>';

        // disable the id field for the remainder of the form
        $type == 'edit' and $form->disable('id');
        $type == 'edit' and $form->disable('added');
        $form->disable('session_id');

        echo '    <fieldset>
            <legend>'.__('candidates.form.details').'</legend>
            <dl>'.
            $form->build().'
            </dl>
            </fieldset>'.
            Form::submit('save', __('candidates.form.save'), array('value' => 'save', 'class' => 'button left')).
            Form::submit('cancel', __('candidates.form.cancel'), array('value' => 'cancel', 'formnovalidate', 'class' => 'button gray right'));

        echo Form::close();

Howdy, Stranger!

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

In this Discussion