Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Splitting an ORM form into sections
  • I'm not sure if this belongs in ORM or general. I am generating a form from a fieldset, from an ORM object. This essentially works, but generates one large form. I would like to split the form up, grouping some fields together under a title for the group, making some parts of the form multi-column etc. What is the best way to approach this? It seems that I either meticulously build a form by hand and construct it as I like, or use the ORM and get what I'm given. I cannot seem to find any middle ground. Any hints on how to format ORM forms? Can, for example, one ORM model be used to generate multiple forms (each taking a selection of named fields) which can then be pieced together in the view into one large form in whatever structrural markup I like?
  • Okay, let's try a different approach. I have a fieldset generated from an ORM object, and a form that can be generated from that. Once the form is generated, can sections of that form be easily pulled out for rendering in different places on the page? I noticed that fieldset (the HTML variety) start and end points can be added into a form, but there does not seem to be a way to insert them at any specific point in the form - they can just be added to the end. In the fieldset (the Fuel variety), fields can be inserted anywhere within the fieldset, but the list of fields that can be inserted do not include the HTML start/end fieldsets. Kind of like being stuck between a rock and a hard place - I feel like I may be missing some vital piece of documentation. -- JJ
  • You can pass the fieldset form to the view, and do this:
    echo ' <fieldset>
      <legend>Bedrijfsgegevens (optioneel)</legend>
      <dl>'.
      $form->field('company_name')->build().
      $form->field('company_contact')->build().
      $form->field('company_address')->build().
      $form->field('company_zipcode')->build().
      $form->field('company_city')->build().'
      </dl>
      </fieldset>';
    
    So build each individual field seperately and use your own html structure for the form.
  • Wow - perfect! So I can build() at the form level, and all that does is build at the item level in a loop. Form->field() gives a list of all Fieldset fields on the form, so it can be automated somewhat. I would argue that Form->field() should probably filter out any Fieldset Fields that are not form items (i.e. have "form"=>"type" set to false). Edit: just raised that as an issue https://github.com/fuel/core/issues/998 I can do a pull request if it is accepted. I think what I have been missing is documentation on the FormItem (or Field, or FormField - not a clue, really) class. Also the Form class documentation (http://docs.fuelphp.com/classes/form.html) does not even mention the build() method at all. I can see how the ability to add comments to documentation like the PHP site can be invaluable. It is great being able to update the documentation through git pull requests, but often we don't know enough of the big picture to provide proper updates - we just need to throw a note at the documentation so it can be picked up, discarded, added to etc. Is that something that is possible to do? -- JJ
  • I see - all the form handling magic is actually in the Fieldset and Fieldset_Field classes. The Form class is just a convenient wrapper around the Fieldset. So a lot of the methods you call on the Form class are actually documented in the Fieldset class (http://docs.fuelphp.com/classes/fieldset.html).
  • Yeah. It's a bit of a mess at the moment, the threesome of Validation, Fieldset and Form. It will be completely rewritten for 2.0 to get rid of this complexity.
  • That's good to hear. Related to this, I was trying to work out how a field (in an ORM object) could be validated to be one of a fixed list of values. It is easy to set a field up to have a form type of "select", with a fixed set of values in the select-list that is validated when submitted from a form. But if the field is *not* a form item, then there appears to be nowhere to put that validation. I am guessing I would need to create a custom validation rule, which is a shame if the code to implement the rule is already in there for the form instantiation of the item. My use-case is a payment gateway model, where I need to protect the model from applications that may try to give it data that would be invalid to the payment gateway.

Howdy, Stranger!

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

In this Discussion