Developers Documentation



301 error for file:

User Tools

Site Tools

Field Class - Why

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.

Web Form Basics

When all is said and done, most configuration in ClearOS is done via a web form that contains:

  • Text fields
  • Select boxes
  • Sliders
  • Checkboxes
  • And others…

The basic layout for a form field is:

  ...form element (text field, select box, etc)...

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 "
"; echo form_label('First Name', 'first_name'); echo form_input('first_name', $first_name); echo "

That looks clean, but this won't actually create valid HTML! Here's the result:

  <label for='first_name'>First Name</label>
  <input type='text' name='first_name' value='some_value'>

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 "
"; echo form_label('First Name', 'first_name'); echo form_input($attributes); echo "

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.

Form Validation Handling

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 "
"; echo form_label('First Name', 'first_name'); echo form_input($attributes); echo form_error('first_name'); echo "

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.

Birth of the Field Helper

The name of the field ('first_name' in our example), is the key value used in the following functions:

  • form_label
  • form_input
  • form_error
  • set_value

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!

View and Edit Modes

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!

ID Handling

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.

content/en_us/dev_framework_reference_guide_field_class_-_why.txt · Last modified: 2015/03/01 14:19 (external edit)