Skip to content

CI_Form_validation

Mathieu Nayrolles edited this page Jan 20, 2016 · 1 revision

CI_Form_validation

Form Validation Class

  • Class name: CI_Form_validation
  • Namespace:

Properties

$CI

protected object $CI

Reference to the CodeIgniter instance

  • Visibility: protected

$_field_data

protected array $_field_data = array()

Validation data for the current form submission

  • Visibility: protected

$_config_rules

protected array $_config_rules = array()

Validation rules for the current form

  • Visibility: protected

$_error_array

protected array $_error_array = array()

Array of validation errors

  • Visibility: protected

$_error_messages

protected array $_error_messages = array()

Array of custom error messages

  • Visibility: protected

$_error_prefix

protected string $_error_prefix = '<p>'

Start tag for error wrapping

  • Visibility: protected

$_error_suffix

protected string $_error_suffix = '</p>'

End tag for error wrapping

  • Visibility: protected

$error_string

protected string $error_string = ''

Custom error message

  • Visibility: protected

$_safe_form_data

protected boolean $_safe_form_data = FALSE

Whether the form data has been validated as safe

  • Visibility: protected

$validation_data

public array $validation_data = array()

Custom data to validate

  • Visibility: public

Methods

__construct

void CI_Form_validation::__construct(array $rules)

Initialize Form_Validation class

  • Visibility: public

Arguments

  • $rules array

set_rules

\CI_Form_validation CI_Form_validation::set_rules(mixed $field, string $label, mixed $rules, array $errors)

Set Rules

This function takes an array of field names and validation rules as input, any custom error messages, validates the info, and stores it

  • Visibility: public

Arguments

  • $field mixed
  • $label string
  • $rules mixed
  • $errors array

set_data

\CI_Form_validation CI_Form_validation::set_data(array $data)

By default, form validation uses the $_POST array to validate

If an array is set through this method, then this array will be used instead of the $_POST array

Note that if you are validating multiple arrays, then the reset_validation() function should be called after validating each array due to the limitations of CI's singleton

  • Visibility: public

Arguments

  • $data array

set_message

\CI_Form_validation CI_Form_validation::set_message($lang, $val)

Set Error Message

Lets users set their own error messages on the fly. Note: The key name has to match the function name that it corresponds to.

  • Visibility: public

Arguments

  • $lang mixed
  • $val mixed

set_error_delimiters

\CI_Form_validation CI_Form_validation::set_error_delimiters($prefix, $suffix)

Set The Error Delimiter

Permits a prefix/suffix to be added to each error message

  • Visibility: public

Arguments

  • $prefix mixed
  • $suffix mixed

error

string CI_Form_validation::error(string $field, string $prefix, string $suffix)

Get Error Message

Gets the error message associated with a particular field

  • Visibility: public

Arguments

  • $field string - <p>Field name</p>
  • $prefix string - <p>HTML start tag</p>
  • $suffix string - <p>HTML end tag</p>

error_array

array CI_Form_validation::error_array()

Get Array of Error Messages

Returns the error messages as an array

  • Visibility: public

error_string

string CI_Form_validation::error_string($prefix, $suffix)

Error String

Returns the error messages as a string, wrapped in the error delimiters

  • Visibility: public

Arguments

  • $prefix mixed
  • $suffix mixed

run

boolean CI_Form_validation::run(string $group)

Run the Validator

This function does all the work.

  • Visibility: public

Arguments

  • $group string

_reduce_array

mixed CI_Form_validation::_reduce_array($array, $keys, $i)

Traverse a multidimensional $_POST array index until the data is found

  • Visibility: protected

Arguments

  • $array mixed
  • $keys mixed
  • $i mixed

_reset_post_array

void CI_Form_validation::_reset_post_array()

Re-populate the _POST array with our finalized and processed data

  • Visibility: protected

_execute

mixed CI_Form_validation::_execute($row, $rules, $postdata, $cycles)

Executes the Validation routines

  • Visibility: protected

Arguments

  • $row mixed
  • $rules mixed
  • $postdata mixed
  • $cycles mixed

_translate_fieldname

string CI_Form_validation::_translate_fieldname($fieldname)

Translate a field name

  • Visibility: protected

Arguments

  • $fieldname mixed

_build_error_msg

string CI_Form_validation::_build_error_msg($line, $field, $param)

Build an error message using the field and param.

  • Visibility: protected

Arguments

  • $line mixed
  • $field mixed
  • $param mixed

has_rule

boolean CI_Form_validation::has_rule($field)

Checks if the rule is present within the validator

Permits you to check if a rule is present within the validator

  • Visibility: public

Arguments

  • $field mixed

set_value

string CI_Form_validation::set_value($field, $default)

Get the value from a form

Permits you to repopulate a form field with the value it was submitted with, or, if that value doesn't exist, with the default

  • Visibility: public

Arguments

  • $field mixed
  • $default mixed

set_select

string CI_Form_validation::set_select($field, $value, $default)

Set Select

Enables pull-down lists to be set to the value the user selected in the event of an error

  • Visibility: public

Arguments

  • $field mixed
  • $value mixed
  • $default mixed

set_radio

string CI_Form_validation::set_radio($field, $value, $default)

Set Radio

Enables radio buttons to be set to the value the user selected in the event of an error

  • Visibility: public

Arguments

  • $field mixed
  • $value mixed
  • $default mixed

set_checkbox

string CI_Form_validation::set_checkbox($field, $value, $default)

Set Checkbox

Enables checkboxes to be set to the value the user selected in the event of an error

  • Visibility: public

Arguments

  • $field mixed
  • $value mixed
  • $default mixed

required

boolean CI_Form_validation::required($str)

Required

  • Visibility: public

Arguments

  • $str mixed

regex_match

boolean CI_Form_validation::regex_match($str, $regex)

Performs a Regular Expression match test.

  • Visibility: public

Arguments

  • $str mixed
  • $regex mixed

matches

boolean CI_Form_validation::matches(string $str, string $field)

Match one field to another

  • Visibility: public

Arguments

  • $str string - <p>string to compare against</p>
  • $field string

differs

boolean CI_Form_validation::differs($str, $field)

Differs from another field

  • Visibility: public

Arguments

  • $str mixed
  • $field mixed

is_unique

boolean CI_Form_validation::is_unique(string $str, string $field)

Is Unique

Check if the input value doesn't already exist in the specified database field.

  • Visibility: public

Arguments

  • $str string
  • $field string

min_length

boolean CI_Form_validation::min_length($str, $val)

Minimum Length

  • Visibility: public

Arguments

  • $str mixed
  • $val mixed

max_length

boolean CI_Form_validation::max_length($str, $val)

Max Length

  • Visibility: public

Arguments

  • $str mixed
  • $val mixed

exact_length

boolean CI_Form_validation::exact_length($str, $val)

Exact Length

  • Visibility: public

Arguments

  • $str mixed
  • $val mixed

valid_url

boolean CI_Form_validation::valid_url(string $str)

Valid URL

  • Visibility: public

Arguments

  • $str string

valid_email

boolean CI_Form_validation::valid_email($str)

Valid Email

  • Visibility: public

Arguments

  • $str mixed

valid_emails

boolean CI_Form_validation::valid_emails($str)

Valid Emails

  • Visibility: public

Arguments

  • $str mixed

valid_ip

boolean CI_Form_validation::valid_ip($ip, $which)

Validate IP Address

  • Visibility: public

Arguments

  • $ip mixed
  • $which mixed

alpha

boolean CI_Form_validation::alpha($str)

Alpha

  • Visibility: public

Arguments

  • $str mixed

alpha_numeric

boolean CI_Form_validation::alpha_numeric($str)

Alpha-numeric

  • Visibility: public

Arguments

  • $str mixed

alpha_numeric_spaces

boolean CI_Form_validation::alpha_numeric_spaces($str)

Alpha-numeric w/ spaces

  • Visibility: public

Arguments

  • $str mixed

alpha_dash

boolean CI_Form_validation::alpha_dash($str)

Alpha-numeric with underscores and dashes

  • Visibility: public

Arguments

  • $str mixed

numeric

boolean CI_Form_validation::numeric($str)

Numeric

  • Visibility: public

Arguments

  • $str mixed

integer

boolean CI_Form_validation::integer($str)

Integer

  • Visibility: public

Arguments

  • $str mixed

decimal

boolean CI_Form_validation::decimal($str)

Decimal number

  • Visibility: public

Arguments

  • $str mixed

greater_than

boolean CI_Form_validation::greater_than($str, $min)

Greater than

  • Visibility: public

Arguments

  • $str mixed
  • $min mixed

greater_than_equal_to

boolean CI_Form_validation::greater_than_equal_to($str, $min)

Equal to or Greater than

  • Visibility: public

Arguments

  • $str mixed
  • $min mixed

less_than

boolean CI_Form_validation::less_than($str, $max)

Less than

  • Visibility: public

Arguments

  • $str mixed
  • $max mixed

less_than_equal_to

boolean CI_Form_validation::less_than_equal_to($str, $max)

Equal to or Less than

  • Visibility: public

Arguments

  • $str mixed
  • $max mixed

in_list

boolean CI_Form_validation::in_list($value, $list)

Value should be within an array of values

  • Visibility: public

Arguments

  • $value mixed
  • $list mixed

is_natural

boolean CI_Form_validation::is_natural($str)

Is a Natural number (0,1,2,3, etc.)

  • Visibility: public

Arguments

  • $str mixed

is_natural_no_zero

boolean CI_Form_validation::is_natural_no_zero($str)

Is a Natural number, but not a zero (1,2,3, etc.)

  • Visibility: public

Arguments

  • $str mixed

valid_base64

boolean CI_Form_validation::valid_base64($str)

Valid Base64

Tests a string for characters outside of the Base64 alphabet as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045

  • Visibility: public

Arguments

  • $str mixed

prep_for_form

string CI_Form_validation::prep_for_form($data)

Prep data for form

This function allows HTML to be safely shown in a form. Special characters are converted.

  • Visibility: public

Arguments

  • $data mixed

prep_url

string CI_Form_validation::prep_url($str)

Prep URL

  • Visibility: public

Arguments

  • $str mixed

strip_image_tags

string CI_Form_validation::strip_image_tags($str)

Strip Image Tags

  • Visibility: public

Arguments

  • $str mixed

encode_php_tags

string CI_Form_validation::encode_php_tags($str)

Convert PHP tags to entities

  • Visibility: public

Arguments

  • $str mixed

reset_validation

\CI_Form_validation CI_Form_validation::reset_validation()

Reset validation vars

Prevents subsequent validation routines from being affected by the results of any previous validation routine due to the CI singleton.

  • Visibility: public
Clone this wiki locally