Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Forms, Fieldsets, ORM and tags
  • Hi there, I've recently started playing around with FuelPHP as we're hoping to use it for an internal project within the business. I've been attempting to get my head around forms, fieldsets and ORM models but would really appreciate some help! First off, anyone able to quickly explain the differences & relationships between Form and Fieldset classes please? To me, using Form would just create a <form> tag and populate as required. Using a Fieldset class would create a <fieldset> class - without a form - and populate as required. I'm attempting to create a form with multiple field-sets depending on an initial option selection. Let's say I've got 5 options (radio buttons) at the top hardcoded into the template. Clicking one of these does a bit of JavaScript wizardy and ends up showing an element with an ID closely related to the selected radio buttons value, meaning that the radio buttons toggle each field-set prompting for more information. However, I think I've misunderstood the whole Form creation process in FuelPHP!
    I should point out here that I've tweaked the following in /app/config/form.php:
    'form_template'         => "\n\t\t{open}{fields}{close}\n\t\t",
      'fieldset_template'     => "\n\t\t{open}{fields}{close}\n\t\t",
      'field_template'        => "\n\t\t<span class=\"field\">{label}{required}{field}</span>",
    
    As I wasn't keen on the <table> approach.
    I've also got several ORM Models which describe the tables I'll be using for the separate data entry options. So in my controller, I have the following:
    $forms  = Form::open(Array('action' => Uri::create('auth/create'), 'method' => 'post'));
        
        $forms .= Form::fieldset_open(Array('class' => 'entry-form', 'id' => 'create-client'), 'Create Client');
        $forms .= Fieldset::forge('create_client')->add_model('Model_Client')->build();
        $forms .= Form::button(Array('value' => 'Create', 'class' => 'btn primary large'));
        $forms .= Form::fieldset_close();
    
        // above Fieldset_open blah blah * 5
     
        $forms .= Form::close();
    

    Now my thinking here is that I'll get a <form> tag with several <fieldset> tags embedded (but importantly, only one <form> ).
    However it appears that every fieldset also wraps it's contents in another <form> tag.. so my Form::button()s do nothing in the way of affecting the form... I guess my question here is; is there any way to stop the Fieldset class from adding a Form tag?
    Is there any way I can just have one Form tag with several Fieldsets embedded containing fields from each of my ORM Models?
    Have I completely got the wrong end of the stick with Forms and Fieldsets?
    And where shall we have lunch?
    Many thanks in advance!
  • Aha okay, that makes sense.. So if I wanted a number of 'grouped' fields based on several tables, I'd call Fieldset::forge as now, but set the config to one that doesn't include the <form> element? What exactly does your forms.embedded config look like then please?
  • My forms.php config file looks identical to the standard, but with an extra array level so I can add more:
    return array(
     'default' => array(
      'prep_value'            => true,
      'auto_id'               => true,
      'auto_id_prefix'        => 'form_',
      'form_method'           => 'post',
      'form_template'         => "\n\t\t{open}\n\t\t<table>\n{fields}\n\t\t</table>\n\t\t{close}\n",
      'fieldset_template'     => "\n\t\t<tr><td colspan=\"2\">{open}<table>\n{fields}</table></td></tr>\n\t\t{close}\n",
      'field_template'        => "\t\t<tr>\n\t\t\t<td class=\"{error_class}\">{label}{required}</td>\n\t\t\t<td class=\"{error_class}\">{field} <span>{description}</span> {error_msg}</td>\n\t\t</tr>\n",
      'multi_field_template'  => "\t\t<tr>\n\t\t\t<td class=\"{error_class}\">{group_label}{required}</td>\n\t\t\t<td class=\"{error_class}\">{fields}\n\t\t\t\t{field} {label}<br />\n{fields}<span>{description}</span>\t\t\t{error_msg}\n\t\t\t</td>\n\t\t</tr>\n",
      'error_template'        => '<span>{error_msg}</span>',
      'required_mark'         => '*',
      'inline_errors'         => false,
      'error_class'           => 'validation_error',
     ),
    );
    
    you can then add as many as you want. My form definitions are specific for my applications and the way I work with errors and CSS, so I don't think that is very useful. To remove the form from the definition, simply use
      'form_template'         => "\n\t\t<table>\n{fields}\n\t\t</table>\n",
    
    so remove the open and close tags.
  • Awesome, thank you!
  • The Form class is a helper class to provide the different HTML form elements. The Fieldset class is a class to generate input forms including validation based on a set of fieldnames. It has nothing to do with the HTML tag of the same name. So by default $fieldsetobject->build() generates the complete form, in accordance with the form template provided. If you want it to generate something else, you have to use a different template. I use a separate config file for that, that contains multiple template scenario's in an array. You can then do this:
    // I use a config file called forms.php which is autoloaded into the forms group
    $forms .= Fieldset::forge('create_client')->add_model('Model_Client')->set_config(Config::get('forms.embedded')->build();
    

Howdy, Stranger!

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

In this Discussion