radio in Form Builder Class.

edited January 2013 in Modules
I have a question about Form Builder class please.
I want to use radio button in the form builder class, how could I do that.
I try
$fields['radio'] = array('required' => TRUE, 'label' => 'Test Radio:', 'options' => array('1' => '1', '2' => '2', '3' => '3') 'type' => 'enum');

If I have 2 options, it shows as radio.
If more than 2 options, it shows as select.

My intention is to have the form_builder generate the radio code please.

Also, is it possible that I can combine the CI form_helper into form_builder?

Thank you.

Comments

  • edited 9:21PM
    Add "mode" => "radios" which will force them to be radios.

    With regards to combining form_helper and form_builder, I'm not sure I understand how you would want to combine them. Form_builder generates entire forms and form_helper generates single form elements and is very similar to the Form class that is used by Form_builder and comes with FUEL.
  • edited January 2013
    For example,
    if I would like to add text field without label. How could I use form_builder to do that?

    Also, when I use the 'mode' => 'enum' with above code.
    $fields['radio'] = array('required' => TRUE, 'label' => 'Test Radio:', 'options' => array('1' => '1', '2' => '2', '3' => '3'), 'type' => 'enum', 'mode' => 'radios');
    HTML:
    <div class="field"> Test Radio: <span class="required">*</span> <input id="radio_1" type="radio" checked="checked" value="1" name="radio"> <label id="label_radio_1" for="radio_1">1</label> <input id="radio_2" type="radio" value="2" name="radio"> <label id="label_radio_2" for="radio_2">2</label> <input id="radio_3" type="radio" value="3" name="radio"> <label id="label_radio_3" for="radio_3">3</label> </div>
    I would like to add p tag in the text, also for each of the radio item, or if I can add a class and id in the div like,
    <div class="field my_radio"> <p>Test Radio: <span class="required">*</span></p> <div> <input id="radio_1" type="radio" checked="checked" value="1" name="radio"> <label id="label_radio_1" for="radio_1">1</label> </div> <div> <input id="radio_2" type="radio" value="2" name="radio"> <label id="label_radio_2" for="radio_2">2</label> </div> <div id="hidden_item"> <input id="radio_3" type="radio" value="3" name="radio"> <label id="label_radio_3" for="radio_3">3</label> </div> </div>

    Thank you very much.
  • edited 9:21PM
    If you are needing something more custom, you can create a field type of "custom" which allows you to pass it the name of a function to render (or an array with an instance as the first array element and a method name as the second array element).

    I just updated the 1.0 beta to allow you to pass a wrapper class and wrapper tag to enum field types (previously you could only do it to multi field types). If you haven't checked out the 1.0 beta, I encourage you to do so as it provides a lot more flexibility with regards to creating custom form elements (among other things):
    https://github.com/daylightstudio/FUEL-CMS/tree/1.0
  • edited January 2013
    Do you have any tutorial about how to use "custom" field?
    I am still confusing about how to pass the function into this custom field type.

    Do I need to add the new function in Form_builder class?
    How to set up this function to display like this simple HTML?

    <div class="field my_radio"> <p>Test Radio: <span class="required">*</span></p> <div> <input id="radio_1" type="radio" checked="checked" value="1" name="radio"> <label id="label_radio_1" for="radio_1">1</label> </div> <div> <input id="radio_2" type="radio" value="2" name="radio"> <label id="label_radio_2" for="radio_2">2</label> </div> <div id="hidden_item"> <input id="radio_3" type="radio" value="3" name="radio"> <label id="label_radio_3" for="radio_3">3</label> </div> </div>

    Thank you very much.
  • edited 9:21PM
    You can specify a "func" parameter which should be a reference to a function. The function can be either a declared function or a lamda function created by the "create_function" php function. Example:
    // USING A LAMBDA FUNCTION $func = create_function('$params', '.... PUT YOUR CUSTOM FUNCTION CODE HERE....'); $fields['my_custom_func'] = array('type' => 'custom', 'func' => $func); // USING a predefined function of "my_func" $fields['my_custom_func'] = array('type' => 'custom', 'func' => 'my_func'); // USING a predefined function of method on an object $fields['my_custom_func'] = array('type' => 'custom', 'func' => array($obj, 'my_method'));
  • edited January 2013
    I have another question about updating to Fuel 1.0 from Fuel 0.9.3?
    In this case which files or folder I need to update?

    Thank you very much for helping me understand about how the custom field work.

    I would like to ask you how can I pass parameters to the custom field during render_divs()?

    I see that there is
    if (!empty($val['custom'])) { $str .= "<div"; if (!empty($this->row_id_prefix)) { $str .= ' id="'.$this->row_id_prefix.Form::create_id($val['name']).'"'; } $str .= " class=\"field\">"; $str .= $this->create_label($val, TRUE); $str .= $val['before_html'].$val['custom'].$val['after_html']; $str .= "</div>\n"; }

    I use var_dump to check for this $val['custom'] and it is always not define.
    I goal is to remove the label so If I can pass parameters into this array I can set the create_label($val, FALSE).
  • edited 9:21PM
    Upgrading depends on how much customization you may have done. What I would do to start would be to get a fresh copy of 1.0 and copy over your view files, models, and controllers to the application directory. In many cases, that will be all you need.

    With regards to your other question try using something like the following which uses a string value instead of a function:
    $fields['my_custom_func'] = array('custom' => 'MY CUSTOM HTML CODE GOES HERE');
    You'll have to set the value of any form inputs in the HTML string. The form_fields method's first parameter is the $values array that will have the records values (if you are editing):
    $custom_val = (!empty($values['my_custom_func'])) ? $values['my_custom_func'] : ''; $fields['my_custom_func'] = array('custom' => '<input name="my_custom_field" type="text" value"'.$custom_val.'"/>');
Sign In or Register to comment.