Урок - Form Validation Class. Часть 2.

Главная » Курсы » Курс CodeIgniter® 2.2 » Урок - Form Validation Class. Часть 2.

Обучающий онлайн курс
CodeIgniter® 2.2

Обучающий курс по CMF CodeIgniter® 2.2.
Лицензия: Лицензия Creative Commons.

 

Rule Reference

 

The following is a list of all the native rules that are available to use:

 

RuleParameterDescriptionExample
required No Returns FALSE if the form element is empty.  
matches Yes Returns FALSE if the form element does not match the one in the parameter. matches[form_item]
is_unique Yes Returns FALSE if the form element is not unique to the table and field name in the parameter. is_unique[table.field]
min_length Yes Returns FALSE if the form element is shorter then the parameter value. min_length[6]
max_length Yes Returns FALSE if the form element is longer then the parameter value. max_length[12]
exact_length Yes Returns FALSE if the form element is not exactly the parameter value. exact_length[8]
greater_than Yes Returns FALSE if the form element is less than the parameter value or not numeric. greater_than[8]
less_than Yes Returns FALSE if the form element is greater than the parameter value or not numeric. less_than[8]
alpha No Returns FALSE if the form element contains anything other than alphabetical characters.  
alpha_numeric No Returns FALSE if the form element contains anything other than alpha-numeric characters.  
alpha_dash No Returns FALSE if the form element contains anything other than alpha-numeric characters, underscores or dashes.  
numeric No Returns FALSE if the form element contains anything other than numeric characters.  
integer No Returns FALSE if the form element contains anything other than an integer.  
decimal No Returns FALSE if the form element contains anything other than a decimal number.  
is_natural No Returns FALSE if the form element contains anything other than a natural number: 0, 1, 2, 3, etc.  
is_natural_no_zero No Returns FALSE if the form element contains anything other than a natural number, but not zero: 1, 2, 3, etc.  
valid_email No Returns FALSE if the form element does not contain a valid email address.  
valid_emails No Returns FALSE if any value provided in a comma separated list is not a valid email.  
valid_ip No Returns FALSE if the supplied IP is not valid. Accepts an optional parameter of "IPv4" or "IPv6" to specify an IP format.  
valid_base64 No Returns FALSE if the supplied string contains anything other than valid Base64 characters.  

 

Note: These rules can also be called as discrete functions. For example:

$this->form_validation->required($string);

Note: You can also use any native PHP functions that permit one parameter.

 

 

Prepping Reference

 

The following is a list of all the prepping functions that are available to use:

 

NameParameterDescription
xss_clean No Runs the data through the XSS filtering function, described in the Input Class page.
prep_for_form No Converts special characters so that HTML data can be shown in a form field without breaking it.
prep_url No Adds "http://" to URLs if missing.
strip_image_tags No Strips the HTML from image tags leaving the raw URL.
encode_php_tags No Converts PHP tags to entities.

 

Note: You can also use any native PHP functions that permit one parameter, like trim, htmlspecialchars, urldecode, etc.

 

 

Function Reference

 

The following functions are intended for use in your controller functions.

 

$this->form_validation->set_rules();

 

Permits you to set validation rules, as described in the tutorial sections above:

 

 

$this->form_validation->run();

 

Runs the validation routines. Returns boolean TRUE on success and FALSE on failure. You can optionally pass the name of the validation group via the function, as described in: Saving Groups of Validation Rules to a Config File.

 

$this->form_validation->set_message();

 

Permits you to set custom error messages. See Setting Error Messages above.

 

 

Helper Reference

 

The following helper functions are available for use in the view files containing your forms. Note that these are procedural functions, so they do not require you to prepend them with $this->form_validation.

 

form_error()

 

Shows an individual error message associated with the field name supplied to the function. Example:

<?php echo form_error('username'); ?>

The error delimiters can be optionally specified. See the Changing the Error Delimiters section above.

 

validation_errors()

 

Shows all error messages as a string: Example:

<?php echo validation_errors(); ?>

The error delimiters can be optionally specified. See the Changing the Error Delimiters section above.

 

set_value()

 

Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form. Example:

<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />

The above form will show "0" when loaded for the first time.

 

set_select()

 

If you use a <select> menu, this function permits you to display the menu item that was selected. The first parameter must contain the name of the select menu, the second parameter must contain the value of each item, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).

 

Example:

<select name="myselect">
<option value="one" <?php echo set_select('myselect', 'one', TRUE); ?> >One</option>
<option value="two" <?php echo set_select('myselect', 'two'); ?> >Two</option>
<option value="three" <?php echo set_select('myselect', 'three'); ?> >Three</option>
</select>

set_checkbox()

 

Permits you to display a checkbox in the state it was submitted. The first parameter must contain the name of the checkbox, the second parameter must contain its value, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE). Example:

<input type="checkbox" name="mycheck[]" value="1" <?php echo set_checkbox('mycheck[]', '1'); ?> />
<input type="checkbox" name="mycheck[]" value="2" <?php echo set_checkbox('mycheck[]', '2'); ?> />

set_radio()

 

Permits you to display radio buttons in the state they were submitted. This function is identical to the set_checkbox() function above.

<input type="radio" name="myradio" value="1" <?php echo set_radio('myradio', '1', TRUE); ?> />
<input type="radio" name="myradio" value="2" <?php echo set_radio('myradio', '2'); ?> />