Building a form with radio buttons?

edited December 2010 in Modules
Hey there,

A friend told me about this cms and It looks great. I've been messing around trying to make my company's store application as a module, but i'm stuck at this part where I need to display a form with radio buttons. I don't see it listed as a type for the form_builder class. Is there a way to do this or can I still use CI's form_helper?

Thanks,

-Justin

Comments

  • edited 11:39AM
    Checkboxes are a little tricky because if they are not checked, they don't send any $_POST data. There are 2 ways to get around it. The first (and the what I normally do), is use an enum field with values of "yes" or "no". The second way, is to add a hook on your model (like the on_before_validate) to check if there is a $_POST value or not, and if so, create the proper $value to pass along in the save.

    For more on hooks, look at the bottom of this page:
    http://www.getfuelcms.com/user_guide/libraries/my_model

    Let me know if that helps.
  • edited December 2010
    I struggled at first too. With Davids help I did:

    [code]
    $config['layout_fields']['my_page'] = array(
    'content' => array('type' => 'textarea', 'description' => 'Body text'),
    'comments' => array('description' => 'Allow comments on this page?', 'type' => 'enum', 'options' => array('yes' => 'yes', 'no' => 'no')),
    );
    [/code]

    Works fine.
  • edited 11:39AM
    I see what you mean, but I guess i'm essentially making a select box, but w/ radio buttons. It's easier to see what I mean by looking at our live site https://www.gmvoices.com/asteriskstore/order
  • edited 11:39AM
    I just realized I gave you instructions about checkboxes instead of radio buttons which I'm sure confused you (although worth noting so I'll leave the post). My apologies.

    To add to Lance's comment, there is an additional parameter of 'mode' you can pass and you can give it a value of 'radios' which will force the enum to be radio inputs instead of a select. You will get a select if you have more then 2 values and the 'mode' value is set to 'auto'. Also, the Form_builder class has a property of single_select_mode which will set the default way to handle enum form fields. For more on the Form_builder class visit:
    http://www.getfuelcms.com/user_guide/libraries/form_builder

    Hope that helps.
  • edited 11:39AM
    No worries, I got it to ouput all the values like I want. Now it seems i'm having trouble displaying it the way I want.

    I want to have them list vertically and the next form I'll need to include another column for a flash player.

    Is it possible to extend the form_builder class and overwrite the render_table function so I can make a custom table for these forms?

    Thanks,

    -Justin
  • edited 11:39AM
    Sure... extend away.
  • edited July 2011
    I'm having difficulty getting form_builder to create enum and multi that set the values in the option list correctly

    The code (for an enum) is

    $this->load->library('form_builder', array('id'=>'plant_edit_form','submit_value' => 'Submit Changes', 'textarea_rows' => '5', 'multi_select_mode'=>'checkbox','single_select_mode'=>'enum'));

    $fields['form'] = array('type' => 'enum','options' => ARRAY('TR' => 'tree', 'SH' => 'shrub', 'WF' => ' flower', 'GR' => 'grass', 'FN' => 'fern', 'PM' => 'palm', 'AQ' => 'aquatic' ), 'required' => TRUE);

    $this->form_builder->set_fields($fields);
    $vars['form'] = $this->form_builder->render($fields,'table');
    $page_init = array('location' => 'plants/editplant', 'render_mode'=>'cms');
    $this->load->module_library(FUEL_FOLDER, 'fuel_page', $page_init);
    $this->fuel_page->add_variables($vars);
    $this->fuel_page->render();
    The html that is actually being generated is


    tree
    shrub
    flower
    grass
    fern
    palm
    aquatic

    I thought that the keys in the options array were the values (similar to the form_class examples), but apparently not. The values, labels, and text all appear to be identical. Is there no way to have them differ from the values? I also don't see any way to get "selected" onto any of these options.

    A similar issue is occurring with type=multi --- and in the case of multi, I need to be able to get "selected" onto multiple options.

    Am I screwing it up, or is something amiss here?

    Do I need to rewrite using the form class and not use the form_builder?

    Thanks.
  • edited 11:39AM
    The key values should be the option values for the select. What happens if you change the single_select_mode to "select" in the initialization?
  • edited 11:39AM
    It does not help -- Same thing happens -- the issue seems consistent across anything with the "options" array
  • edited 11:39AM
    I ran an experiment -- I took the same parameters and fed them straight into the form class -- I got the correct code generated.

    The only form builder examples on this forum or in the documentation actually do have the keys and values identical -- it is possible that I'm the first to try to use it with them not being the same?
  • edited 11:39AM
    Change the type of the field to a "select". The enum type with the "select" mode changes the labels and values to be equal. This has to do with building form fields based off of "enum" table column types whose.
Sign In or Register to comment.