keyboard_arrow_left Form Validation

Before explaining Thonem’s approach to data validation, let’s describe the ideal scenario:

  1. the form is displayed.
  2. You fill the form in and submit it.
  3. If you submitted something invalid, or perhaps missed a required item, the form is redisplayed containing your data along with an error message show the problem.
  4. This process continues until you have to submit a valid form.

On the other hand, the script must:

  1. Check for the required data.
  2. Verify that the data is in the correct type, and meets the correct criteria. For example, if a username is submitted it must be validated to contain only permitted characters. It must consist of a minimum length, and not exceed a maximum length. The username can’t be someone else’s existing username, or perhaps even a reserved word. Etc.
  3. Sanitize the data for security.
  4. Pre-format the data if needed (Does the data need to be trimmed? HTML encoded? Etc.)
  5. Prep the data for inserting it in the database.

Although there is nothing terribly complex about the above points, it usually requires a significant amount of code, and to display error messages, various control structures are usually placed within the form HTML. Form validation, while simple to create, is generally very messy and tedious to implement.


$this->data["languages"] = language_list();

$this->form_validation->set_array("languages", array_keys($this->data["languages"]));

$this->form_validation->set_rules('_language_tab', lang("Language"), "trim|required|from_array[languages]");