Trying to Understand Form Fields Multi Documentation

I am trying to use a MySQL "set" field type - It appears to be implemented by the Multi field as provided in the Fuel user guide by

$options = array('a' => 'option A', 'b' => 'option B', 'c' => 'option C'); $fields['multi_example'] = array('type' => 'multi', 'options' => $options, 'value' => 'a');

The code generated by this generates the needed form field, and if only one value is set, it gets into the form correctly as the initial vale.

But, if one wants to set two values (say "a" and "c") , not just one as in shown in the example, what is wanted? I tried an array, but my guess was clearly "not it".

And after submitting, some array seems to have been returned -- what is the content of that array?

I'm assuming that I will need pre- and post- processing functions to handle the translation from and to the "set" field type....

This is perhaps an example that needs to be documented as I assume most simple multi-select types of fields would be very similar.

Thanks.

Comments

  • edited 6:19AM
    When you used an array for the value, did it look like the following?
    $options = array('a' => 'option A', 'b' => 'option B', 'c' => 'option C'); $fields['multi_example'] = array('type' => 'multi', 'options' => $options, 'value' => array('a', 'b'));
  • edited March 2014
    Yes -- I see neither value in the form

    All of the following work if only one value is set, and none works if two values are set.
    In all cases, the save works...the database gets updated correctly if 0, 1, or 2 values are set.

    I was using version 1 in Fuel 0.93 with success though I was doing the form handling out of a controller and not via the model (never tested via model).

    function form_fields($values = array()) { $fields = parent::form_fields($values); $options = array('FL' => 'FL', 'NY' => 'NY'); # Version 1 --- this is worked in 0.93 # $value = explode(",",$values['distribution']); # $fields['distribution'] = array('type' => 'multi', 'options' => $options, 'value'=>$value); # Version 2 -- this is just a test to see if they can be set following the example in the documentation # $fields['distribution'] = array('type' => 'multi', 'options' => $options, 'value'=>array('FL', 'NY')); # Version 3 $values ['distribution'] = explode(",",$values['distribution']); $fields['distribution'] = array('type' => 'multi', 'options' => $options); $fields['published']['order'] = 1000; return $fields; } public function on_before_save($values) { # Translate the data for distribution field back into a comma delimited format if (isset($values['distribution'])) {$values['distribution']= implode(",",$values['distribution']);} else {$values['distribution'] = '';}; return $values; }
  • edited 6:19AM
    Setting the values in the form_fields method will only work when creating a new record. If you are editing an existing record, it will use whatever is returned from your find_one_array method by default. If your find_one_array method isn't returning an array for that distribution value (which it probably isn't by default unless you set it as a serialized field). Is this perhaps what you are experiencing? The method associated with setting the values for your module's form can be configured in your MY_fuel_modules.php file using the "edit_method" parameter on the particular module.
  • edited 6:19AM
    Setting the field to be serialized in the model worked. Thanks.
Sign In or Register to comment.