Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
trouble with Fieldset select field and a pre-set value
  • Using FuelPHP 1.6, I am trying to generate a select field using a fieldset, and it works, except that I can't pre-select a value.

    Here is my code:

    $manufacturer_field = array(
    'type' => 'select',
    'value'=>2, // hard-coded for testing
    'options'=>\Manufacturer\Model::dropdown(),
    )
    $form->add_before( 'manufacturer', 'Manufacturer', $manufacturer_field, array(''), 'submit' );


    and here is the output html (with value '2' not selected):

    <div class="control-group">

    <label class="control-label">
    Manufacturer</label>
    <div class="controls"><select id="form_manufacturer" name="manufacturer">
      <option value="1">Salt City Glass</option>

      <option value="2">
    Yambo</option>

    </select>
    </div>

    </div>

  • What version of Fuel are you on?

    This exact code generates this in 1.7/develop:
    <form action="http://tests.local/17develop/public/" accept-charset="utf-8" method="post">
    <table>
    <tr>
    <td class=""><label id="label_manufacturer" for="form_manufacturer">Manufacturer</label></td>
    <td class="">
    <select id="form_manufacturer" name="manufacturer">
    <option value="1">Salt City Glass</option>
    <option value="2" selected="selected">Yambo</option>
    </select> <span></span>
    </td>
    </tr>
    </table>
    </form>
  • I'm on 1.6 - sorry, edited above but after you'd started on the problem.
  • Used this to test on 1.6/master. Worked fine too?
    $manufacturer_field = array(
    'type' => 'select',
    'value'=>2, // hard-coded for testing
    'options'=> array(1 => 'value 1', 2 => 'value 2'),
    );

    $form = \Fieldset::forge('x');
    $form->add( 'manufacturer', 'Manufacturer', $manufacturer_field, array(''));
    \Debug::dump($form->build());die();
  • Well, I wish it hadn't.  Thank you for trying.
  • I've found that it works fine until $form->populate() is called, and then the integer value is replaced with an object of the related model for this dropdown.

    I've changed the name from manufacturer (the key for the related model) to select_manufacturer, and this fixed it.  Is there some better way?
  • No, you should take your fieldnames with care. if you are auto populating stuff. If a mapping is found, the value will be assigned by the fieldset.

    If you have a field that links to a relation, the common thing to do is to use the foreign key for the dropdown field (in this case probably something like manufacturer_id).
  • Ok, I see, thank you.  I tried using manufacturer_id it with $fieldset->add_field() and got an error, but should have been using $fieldset->field('manufacturer_id')->set_options( $options).

    Documentation of of the fieldset_field class might have saved me some head-scratching.  If made, a link to it inside of the fieldset->field() function's docs would be nice.
  • HarroHarro
    Accepted Answer
    I already wondered why you added the field, if it's a related item, the field should already be there.

    Just add the dropdown values to the form section of the properties, or use set_options() on the fieldset field:

    $fieldset->field('manufacturer_id)->set_options( ... );

    and pass it a key-value array for the dropdown.
  • Ninja-edited again while you were posting.

    Yes, I have it now.  As I said above, some docs on the fieldset_field class might have saved me some head-scratching. Since I get it now, I'm not too worried about them, but for the next guy.

    I noticed that I can rearrange fields by changing their order in Model::$_properties, but that it put the related field above the others.  Is there some reason to this, or if I changed it to treat them equally, might the pull-request be accepted?
  • Also, for most dropdowns I consider a link next to the field to create a new entry in the related table crucial.  What is the best way to go about inserting that html?  I have considered modifying the template function to have {html_after_field} and {html_before_field}, and modifying the fieldset_field class to have add_html_before() and add_html_after() functions.
  • To do that properly is quite complicated.

    Our (companies) application framework has what we call a "pagelink" system.

    It can generate links or buttons for this purpose, and when you click on it, it saves the contents of $_POST, before redirecting to a new page. Where it can either present a new form, an update form, or a "view details" page. When you're finished on that page, it will redirect back to the previous page, and repopulates the form. It can also pass data back and forth, so that the new item you have created is selected when you return to the original form.

    It maintains mutliple stacks, so you can link from one page to the next to the next... and return every time.

    Unfortunately it's not open source (we do have to make a living... ;-)
  • That's kind of disheartening, but thank you for the details.

  • Sorry I can't be of assistance this time.

Howdy, Stranger!

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

In this Discussion