Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
set_form_fields() how to set default value
  • Hi,

    I set options in a select in my set_form_fields() method (I use Fieldset). It's work. 

    But now, i want to set a default value, i have try that : 

    static::$_properties['theme']['form']['value'] = $themeDefault;
    or
    static::$_properties['theme']['form']['values'] = $themeDefault;

    But that doesn't work. How i can set default value in field in this method ?
  • HarroHarro
    Accepted Answer
    You mean you use the method to set or change the value of the model field property, instead of on the fieldset field object the method creates?

    As far as I know that is not supported. The properties array is meant to define parameters and defaults for the field, not to contain runtime data.

    Instead, call the parent, have it create the fieldset, and then used the returned fieldset object to add runtime data (using set_options or set_value) before you return it.
  • I use the method for populate a select field from other model. example :

    $selectOptions = array();
    $themes = \Model_Theme::find('all');
    foreach($themes as $id => $theme) {
    $selectOptions[$id] = $theme;
    }
            static::$_properties['theme']['form']['options'] = $selectOptions;

    And after populate, i want to set a default value.

    I know i can do it in the controller, but i think it's better way here ?

    ---

    EDIT: It's work fine after call the parent method, thanks Harro.
  • HarroHarro
    Accepted Answer
    What I meant was that the call to parent::set_form_fields() returns the populated fieldset.

    If the options list is static, you can do this, no problem. If it's dynamic and you need to generate multiple fieldsets, it's better to call set_options() on the fieldset returned.

    Either way, you need the fieldset object to do ->field('theme')->set_value('default');

Howdy, Stranger!

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

In this Discussion