Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Set Fieldset field 'name' and 'id' on the fly?
  • I currently have a fieldset for my 'list' ORM object. Within this I have added another fieldset to add 'listitems' to the list. These fieldsets are being created and populated within a set_form_fields function in the 'list' model file. This code shows how I build the inner 'listitems' fieldset within the function:

    foreach($instance->items as $item) {
    $form->add(\Fieldset::forge('item_'.$item->id,
    array(
    'form_attributes' => array(
    'class' => 'itemform'
    )
    )
    )->add_model($item, $item)->populate($item));
    }

    I am planning on adding a button which uses ajax to dynamically add another listitem field onto the form so that as many can be added and saved as necessary. 

    The issue I have is that when the code above creates and populates the inner 'listitem' fieldset all of the fields will have the same name and id attributes. I know these are taken from the $_properties array in the model but is there some way I can edit them in the code I have shown above on the fly?

    Thanks.
  • HarroHarro
    Accepted Answer
    If you want to build a 1:N form, you might want to look at tabular forms, which supports exactly this, and creates the fieldnames as an array so you can add additional rows.

    See http://fuelphp.com/docs/classes/fieldset.html#one_to_many_forms for more information.
  • Thanks that's working now. Only thing is, either side of the delete checkboxes it is outputting {icon}?
  • I've moved the form.php config into app and removed {icon} from the template?

    Is this okay/any reason I should not do this?
  • Oops.

    That should not be there. The tabular form feature comes from a Fieldset core extension we have been using in our applications, and the part that adds field icons hasn't made it into the fieldset code (because it's application specific).

    So it's safe to remove, and I'll remove it from the 1.6 template.
  • Okay, thanks for the help!
  • Just wondering, is there a way with the tabular forms to insert many new records rather than just one at a time?

    Would this have to be done with ajax via a button?
  • It's up to you.

    It generates standard table rows, with array's for fieldnames, so it's very easy to copy the last time using a bit of javascript.

    Sometimes we do it this way, sometimes we use an automatic mechanism (if the one before last is filled in, a new empty one will automatically be added at the bottom), and sometimes we just create a form with 10 empty lines.
  • Ah right, thanks again.

Howdy, Stranger!

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

In this Discussion