The Field Helper was developed for the ClearOS framework to simplify and standardize web forms in webconfig. This document goes over some of the reasons why the Field Helper was created.
When all is said and done, most configuration in ClearOS is done via a web form that contains:
The basic layout for a form field is:
Think of any page in webconfig, and they all come down to label/element, label/element, label/element, etc. In CodeIgniter, a simple text input field (e.g. First Name ___) is coded like the following:
echo "<div>"; echo form_label('First Name', 'first_name'); echo form_input('first_name', $first_name); echo "</div>";
That looks clean, but this won't actually create valid HTML! Here's the result:
<div> <label for='first_name'>First Name</label> <input type='text' name='first_name' value='some_value'> </div>
The for in the label should be referencing an HTML ID. It matches the name in the input field, but that's not helpful. In order to do this properly, an ID needs to be passed into the CodeIgniter form_input function:
$attributes = array( 'name' => 'first_name', 'value' => $first_name, 'id' => 'first_name' ); echo "<div>"; echo form_label('First Name', 'first_name'); echo form_input($attributes); echo "</div>";
That's getting messy, but at least it is HTML compliant. We're not done yet! The CodeIgniter framework also includes a very nice Form Validation Class. Read on.
In the code sample below, you will see the addition of the set_value and form_error functions. You can read about these functions in the CodeIgniter docs, but all you need to know right now is that they need to exist if we want to use CodeIgniter form validation (and we do!).
$attributes['name'] = 'first_name'; $attributes['id'] = 'first_name'; $attributes['value'] = set_value('first_name', $default); echo "<div>"; echo form_label('First Name', 'first_name'); echo form_input($attributes); echo form_error('first_name'); echo "</div>";
Well… that's an awful lot of code and duplication for a simple text input field. This was the impetus behind creating the Field Helper for the ClearOS development framework.
The name of the field ('first_name' in our example), is the key value used in the following functions:
We can save some typing by combining these function calls into a single call. The form_error and set_value handling is done inside the Field Helper so the ClearOS App developer doesn't need to worry about it. The end result:
field_input('First name', 'first_name', $first_name);
Now that's better!
To help with merging a “view only” and an “edit” form, an extra (and optional) parameter is added to the field_input function: $readonly. Consider the following:
$readonly = FALSE; field_input('First name', 'first_name', $first_name, $readonly); field_input('Middle name', 'middle_name', $middle_name, $readonly); field_input('Last name', 'last_name', $last_name, $readonly); // and a dozen other fields typically found for managing a user account
By simply flipping the $readonly flag, the web form will go from editable to view-only!
The ID handling that was necessary to create HTML compliant code is handled automatically by the Field Helper. If you need to specify HTML IDs for specific elements inside your field, then you will want to give this a read. search?q=clearos%2C%20clearos%20content%2C%20dev%2C%20framework%2C%20other%2C%20maintainer_dloper&btnI=lucky