Trongate Docs
switch to dark modeswitch to dark mode
»
»
The Form Validation Helper

The Form Validation Helper

Trongate's validation helper uses server side validation to assist developers in validating submitted form values.

All form validation procedures these following three steps:

  • Submitted form values are passed through some validation test.
  • The results from the validation tests are assigned a boolean value of true (if the submitted form passes validation tests) or false (if there was at least one form validation error).
  • The user is then presented with either a 'success' message or some validation errors, based on the results of the validation tests.

Just To Let You Know
A complete walk-through of how form validation works is available in the 'Form Handling' chapter of these docs.

How To Set Form Validation Rules

Form validation rules can be set by calling upon the validation helper and then invoking Trongate's inbuilt 'set_rules' method.  This should be used on a line-by-line basis, whereby each form field that requires validation is processed via a line of code which takes the form:

$this->validation_helper->set_rules('name', 'label', 'rule');

How To Pipe Multiple Rules Together​

Trongate lets you apply multiple validation rules to a submitted value by joining rules together by use of the pipe symbol.  For example:

​$this->validation_helper->set_rules('name', 'label', 'rule1|rule2|rule3');

Running Form Validation Tests

The form validation tests that you have created can be applied by calling the validation helper's run() method. Calling run() will produce true if the values passed validation tests and false if there was at least one form validation error.

$result = $this->validation_helper->run();

Displaying Validation Errors

If there are validation errors, they can be displayed by calling Trongate's in-built validation_errors() method from within a view file.  For example:

<?= validation_errors() ?>

Formatting Validation Errors

By default, each validation error is displayed as a paragraph with red text.  However, you can modify the appearance of validation errors by adding opening and closing tags inside your validation_errors() declaration.  For example:

<?= validation_errors('<div class="error">', '</div>') ?>

Validation Rules Reference

Rule Parameter Description Example
required No Returns FALSE if posted value is empty. required
min_length Yes Returns FALSE if posted value has a string length less than specified value. min_length[3]
max_length Yes Returns FALSE if posted value has a string length greater than specified value. max_length[75]
exact_length Yes Returns FALSE unless posted value has a string length equal to specified value. exact_length[12]
greater_than Yes Returns FALSE unless posted value is greater than specified value. greater_than[17]
less_than Yes Returns FALSE unless posted value is less than specified value. less_than[31]
numeric No Returns FALSE is posted value is not numeric. numeric
integer No Returns FALSE is posted value is not an integer (i.e., not a whole number). integer
decimal No Returns FALSE posted value is not numeric with at least one decimal place. decimal
matches Yes Returns FALSE if posted value does not match another specified form element. matches[password]
differs Yes Returns false if form element matches another specified form element. differs[name]
valid_email No Returns FALSE if posted value is not formatted like an email address. valid_email
valid_datepicker_us No Returns FALSE unless posted value is a valid datepicker value of the United States of America format mm-dd-yyyy. valid_datepicker_us
valid_datepicker_eu No Returns FALSE if posted value is not a valid datepicker value of the European format dd-mm-yyyy. valid_datepicker_eu
valid_datetimepicker_us No Returns FALSE if posted value is not a valid datetime picker value. valid_datetimepicker_us
valid_datetimepicker_eu No Returns FALSE if posted value is not a valid datetime picker value. valid_datetimepicker_eu
valid_time No Returns FALSE if posted value is not a valid time. valid_time


HELP & SUPPORT

If you have a question or a comment relating to anything you've see here, please goto the Help Bar.

 
×