How to best create a fieldset and legend using the formbuilder class

edited September 2011 in Modules
I have a long form that I want to split up into 2 parts using 2 different fieldset's. I'm not sure if I should somehow use the form class to create the fieldsets in my controller, or if I should create them in the view.

Since i'm creating my entire form in the controller and then passing it to the view, i'd like to just create the fieldset's in there as well. any ideas how I should do that?

Here's a snippet from my controller

// build the form $fields = array(); $fields['name'] = array('label' => 'Contact Name', 'required' => TRUE, 'class' => 'input left'); $fields['company'] = array('label' => 'Company', 'required' => TRUE, 'class' => 'input left'); $fields['address'] = array('label' => 'Address', 'required' => TRUE, 'class' => 'input left'); $fields['phone'] = array('label' => 'Phone', 'required' => FALSE, 'class' => 'input left'); $fields['fax'] = array('label' => 'FAX', 'required' => FALSE, 'class' => 'input left'); $fields['email'] = array('label' => 'Email', 'required' => TRUE, 'class' => 'input left'); $fields['inspection'] = array('label' => 'Inspection and Estimate Required by When?', 'required' => TRUE, 'class' => 'input left'); $fields['location'] = array('label' => 'Name of Location', 'required' => TRUE, 'class' => 'input left'); $fields['address'] = array('label' => 'Address', 'required' => TRUE, 'class' => 'input left'); $fields['contactName'] = array('label' => 'Contact Name', 'required' => TRUE, 'class' => 'input left'); $fields['phoneNum'] = array('label' => 'Phone Number', 'required' => FALSE, 'class' => 'input left'); $fields['access'] = array('label' => 'Access', 'required' => TRUE, 'class' => 'input left'); $fields['reason'] = array('label' => 'Reason for Estimate', 'required' => TRUE, 'class' => 'input left'); $fields['comments'] = array('label' => 'Comments', 'required' => TRUE, 'class' => 'input left'); $this->form_builder->set_fields($fields); //$this->form_builder->render($fields); // will set the values of the fields if there is an error... must be after set_fields $this->form_builder->set_validator($this->validator); $this->form_builder->set_field_values($_POST); $vars['form'] = $this->form_builder->render();

Comments

  • edited 5:14AM
    Form_builder doesn't support field sets but does have a field type of "section" you can use to create different areas of the form. Providing either a label, value, or name value will result in an H3 tag being rendered (the H3 can be configured to by changing the "section_tag" property of form_builder).
  • edited 5:14AM
    I don't think that an h3 tag will do for me, as I need to wrap sections of the form in fieldset or div tag.

    Is it possible to build the form using 2 separate sections and then use both of them like this? If so, how would I do that in the controller?
    <fieldset> <legend>First Section</legend> <?php echo $form; ?> </fieldset> <fieldset> <legend>Second Section</legend> <?php echo $form2; ?> </fieldset>
  • edited 5:14AM
    Yes you can. You essentially just run it twice in your controller and pass the $vars array to your view:
    $this->form_builder->use_form_tag = FALSE; $this->form_builder->set_fields($fields1); $vars['form1'] = $this->form_builder->render(); $this->form_builder->set_fields($fields1); $vars['form2'] = $this->form_builder->render();
  • edited 5:14AM
    Ok i gotcha. I was close, but was confused on how to do the render() piece. Thanks.
  • edited 5:14AM
    Is it possible to easily do this "split form" trick in the create/edit view of a simple module? I have a PORDUCTS module with a lot of fields and I would like to display them over 2 columns (for example).

    I've created a custom view based on the module_create_edit layout but I can't figure out where or how to insert the logic to double up the form.

    Is creating a seperate controller the only way?
  • edited 5:14AM
    Unfortunately not at the moment.

    However, the next major release we are working on will allow you to create different fieldsets as well as tabs to group fields together. Look for something at the end of this month.
  • edited 5:14AM
    Hello, I am trying to use the solution you gave toymachiner62, but I get the *required fields text and the submit button twice on my form.

    Why is that?

    My controller code:
    $fields = array(); $fields['registeredName'] = array('label'=>'Registered Name', 'size' => 100, 'required' => TRUE); $fields['registrationNo'] = array('label'=>'Registration No.', 'size' => 60, 'required' => TRUE); $fields['vatNo'] = array('label'=>'Vat No.', 'size' => 60, 'required' => TRUE); $fields['activity'] = array('label'=>'Main Activity', 'type' => 'select', 'options' => array('select' => 'Select your job title', 'Supplier' => 'Supplier', 'Transporter' => 'Transporter', 'Trader' => 'Trader', 'Broker' => 'Broker'), 'required' => TRUE); $fields1 = array(); $fields1['phaddr1'] = array('label'=>'Address Line 1', 'required' => TRUE); $fields1['phaddr2'] = array('label'=>'Address Line 2', 'required' => TRUE); $fields1['phsuburb'] = array('label'=>'Suburb', 'required' => TRUE); $fields1['phtown'] = array('label'=>'Town/City', 'required' => TRUE); $fields1['phpostcode'] = array('label'=>'Post Code', 'required' => TRUE); $this->form_builder->use_form_tag = FALSE; $this->form_builder->set_validator($this->validator); $this->form_builder->set_field_values($_POST); $this->form_builder->display_errors = TRUE; $this->form_builder->required_text = '<span class="required">*</span>Required fields'; $this->form_builder->set_fields($fields); $vars['form'] = $this->form_builder->render(); $this->form_builder->set_fields($fields1); $vars['form1'] = $this->form_builder->render(); $page_init = array('location' => 'register'); $this->load->module_library(FUEL_FOLDER, 'fuel_page', $page_init); $this->fuel_page->add_variables($vars); $this->fuel_page->render();

    What is the advantage of using form builder instead of just using codeigniter's form class in my view and create the fields in the view instead of in the controeller?
  • edited 5:14AM
    You can certainly just create your forms using CI's form functions. Form_builder allows you to create common form elements using an array syntax. You can pass it a Validator object which will highlight fields that have errors as well. FUEL uses it to take the form_field's method information to generate the form.
  • edited November 2013
    Just realized I replied to the wrong post and unable to delete this reply. So, instead of wasting it - In the form_fields method of a model, when I change this:
    $fields = parent::form_fields($values); $CI =& get_instance();
    To this$CI =& get_instance(); $fields = array();
    fieldsets work perfectly. The form field values are filling correctly as well. What's the difference between the 2 and are there any disadvantages to doing it that way?
  • edited 5:14AM
    Please see this response about setting the field order:
    http://forum.getfuelcms.com/discussion/1468/using-fieldset-in-the-cms#Item_2
Sign In or Register to comment.