CI_Form_validation Class Reference

List of all members.


Public Member Functions

 CI_Form_validation ($rules=array())
 Constructor.
 set_rules ($field, $label= '', $rules= '')
 Set Rules.
 set_message ($lang, $val= '')
 Set Error Message.
 set_error_delimiters ($prefix= '< p >', $suffix= '</p >')
 Set The Error Delimiter.
 error ($field= '', $prefix= '', $suffix= '')
 Get Error Message.
 error_string ($prefix= '', $suffix= '')
 Error String.
 run ($group= '')
 Run the Validator.
 _reduce_array ($array, $keys, $i=0)
 Traverse a multidimensional $_POST array index until the data is found.
 _reset_post_array ()
 Re-populate the _POST array with our finalized and processed data.
 _execute ($row, $rules, $postdata=NULL, $cycles=0)
 Executes the Validation routines.
 _translate_fieldname ($fieldname)
 Translate a field name.
 set_value ($field= '', $default= '')
 Get the value from a form.
 set_select ($field= '', $value= '', $default=FALSE)
 Set Select.
 set_radio ($field= '', $value= '', $default=FALSE)
 Set Radio.
 set_checkbox ($field= '', $value= '', $default=FALSE)
 Set Checkbox.
 required ($str)
 Required.
 matches ($str, $field)
 Match one field to another.
 min_length ($str, $val)
 Minimum Length.
 max_length ($str, $val)
 Max Length.
 exact_length ($str, $val)
 Exact Length.
 valid_email ($str)
 Valid Email.
 valid_emails ($str)
 Valid Emails.
 valid_ip ($ip)
 Validate IP Address.
 alpha ($str)
 Alpha.
 alpha_numeric ($str)
 Alpha-numeric.
 alpha_dash ($str)
 Alpha-numeric with underscores and dashes.
 numeric ($str)
 Numeric.
 is_numeric ($str)
 Is Numeric.
 integer ($str)
 Integer.
 is_natural ($str)
 Is a Natural number (0,1,2,3, etc.
 is_natural_no_zero ($str)
 Is a Natural number, but not a zero (1,2,3, etc.
 valid_base64 ($str)
 Valid Base64.
 prep_for_form ($data= '')
 Prep data for form.
 prep_url ($str= '')
 Prep URL.
 strip_image_tags ($str)
 Strip Image Tags.
 xss_clean ($str)
 XSS Clean.
 encode_php_tags ($str)
 Convert PHP tags to entities.

Public Attributes

 $CI
 $_field_data = array()
 $_config_rules = array()
 $_error_array = array()
 $_error_messages = array()
 $_error_prefix = '<p>'
 $_error_suffix = '</p>'
 $error_string = ''
 $_safe_form_data = FALSE

Detailed Description

Definition at line 27 of file Form_validation.php.


Member Function Documentation

CI_Form_validation::_execute ( row,
rules,
postdata = NULL,
cycles = 0 
)

Executes the Validation routines.

private

Parameters:
array 
array 
mixed 
integer 
Returns:
mixed

Definition at line 475 of file Form_validation.php.

References _translate_fieldname().

Referenced by run().

00476         {
00477                 // If the $_POST data is an array we will run a recursive call
00478                 if (is_array($postdata))
00479                 { 
00480                         foreach ($postdata as $key => $val)
00481                         {
00482                                 $this->_execute($row, $rules, $val, $cycles);
00483                                 $cycles++;
00484                         }
00485                         
00486                         return;
00487                 }
00488                 
00489                 // --------------------------------------------------------------------
00490 
00491                 // If the field is blank, but NOT required, no further tests are necessary
00492                 $callback = FALSE;
00493                 if ( ! in_array('required', $rules) AND is_null($postdata))
00494                 {
00495                         // Before we bail out, does the rule contain a callback?
00496                         if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
00497                         {
00498                                 $callback = TRUE;
00499                                 $rules = (array('1' => $match[1]));
00500                         }
00501                         else
00502                         {
00503                                 return;
00504                         }
00505                 }
00506 
00507                 // --------------------------------------------------------------------
00508                 
00509                 // Isset Test. Typically this rule will only apply to checkboxes.
00510                 if (is_null($postdata) AND $callback == FALSE)
00511                 {
00512                         if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
00513                         {
00514                                 // Set the message type
00515                                 $type = (in_array('required', $rules)) ? 'required' : 'isset';
00516                         
00517                                 if ( ! isset($this->_error_messages[$type]))
00518                                 {
00519                                         if (FALSE === ($line = $this->CI->lang->line($type)))
00520                                         {
00521                                                 $line = 'The field was not set';
00522                                         }                                                       
00523                                 }
00524                                 else
00525                                 {
00526                                         $line = $this->_error_messages[$type];
00527                                 }
00528                                 
00529                                 // Build the error message
00530                                 $message = sprintf($line, $this->_translate_fieldname($row['label']));
00531 
00532                                 // Save the error message
00533                                 $this->_field_data[$row['field']]['error'] = $message;
00534                                 
00535                                 if ( ! isset($this->_error_array[$row['field']]))
00536                                 {
00537                                         $this->_error_array[$row['field']] = $message;
00538                                 }
00539                         }
00540                                         
00541                         return;
00542                 }
00543 
00544                 // --------------------------------------------------------------------
00545 
00546                 // Cycle through each rule and run it
00547                 foreach ($rules As $rule)
00548                 {
00549                         $_in_array = FALSE;
00550                         
00551                         // We set the $postdata variable with the current data in our master array so that
00552                         // each cycle of the loop is dealing with the processed data from the last cycle
00553                         if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
00554                         {
00555                                 // We shouldn't need this safety, but just in case there isn't an array index
00556                                 // associated with this cycle we'll bail out
00557                                 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
00558                                 {
00559                                         continue;
00560                                 }
00561                         
00562                                 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
00563                                 $_in_array = TRUE;
00564                         }
00565                         else
00566                         {
00567                                 $postdata = $this->_field_data[$row['field']]['postdata'];
00568                         }
00569 
00570                         // --------------------------------------------------------------------
00571         
00572                         // Is the rule a callback?                      
00573                         $callback = FALSE;
00574                         if (substr($rule, 0, 9) == 'callback_')
00575                         {
00576                                 $rule = substr($rule, 9);
00577                                 $callback = TRUE;
00578                         }
00579                         
00580                         // Strip the parameter (if exists) from the rule
00581                         // Rules can contain a parameter: max_length[5]
00582                         $param = FALSE;
00583                         if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match))
00584                         {
00585                                 $rule   = $match[1];
00586                                 $param  = $match[2];
00587                         }
00588                         
00589                         // Call the function that corresponds to the rule
00590                         if ($callback === TRUE)
00591                         {
00592                                 if ( ! method_exists($this->CI, $rule))
00593                                 {               
00594                                         continue;
00595                                 }
00596                                 
00597                                 // Run the function and grab the result
00598                                 $result = $this->CI->$rule($postdata, $param);
00599 
00600                                 // Re-assign the result to the master data array
00601                                 if ($_in_array == TRUE)
00602                                 {
00603                                         $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
00604                                 }
00605                                 else
00606                                 {
00607                                         $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
00608                                 }
00609                         
00610                                 // If the field isn't required and we just processed a callback we'll move on...
00611                                 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
00612                                 {
00613                                         return;
00614                                 }
00615                         }
00616                         else
00617                         {                               
00618                                 if ( ! method_exists($this, $rule))
00619                                 {
00620                                         // If our own wrapper function doesn't exist we see if a native PHP function does. 
00621                                         // Users can use any native PHP function call that has one param.
00622                                         if (function_exists($rule))
00623                                         {
00624                                                 $result = $rule($postdata);
00625                                                                                         
00626                                                 if ($_in_array == TRUE)
00627                                                 {
00628                                                         $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
00629                                                 }
00630                                                 else
00631                                                 {
00632                                                         $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
00633                                                 }
00634                                         }
00635                                                                                 
00636                                         continue;
00637                                 }
00638 
00639                                 $result = $this->$rule($postdata, $param);
00640 
00641                                 if ($_in_array == TRUE)
00642                                 {
00643                                         $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
00644                                 }
00645                                 else
00646                                 {
00647                                         $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
00648                                 }
00649                         }
00650                                                         
00651                         // Did the rule test negatively?  If so, grab the error.
00652                         if ($result === FALSE)
00653                         {                       
00654                                 if ( ! isset($this->_error_messages[$rule]))
00655                                 {
00656                                         if (FALSE === ($line = $this->CI->lang->line($rule)))
00657                                         {
00658                                                 $line = 'Unable to access an error message corresponding to your field name.';
00659                                         }                                               
00660                                 }
00661                                 else
00662                                 {
00663                                         $line = $this->_error_messages[$rule];
00664                                 }
00665 
00666                                 // Build the error message
00667                                 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
00668 
00669                                 // Save the error message
00670                                 $this->_field_data[$row['field']]['error'] = $message;
00671                                 
00672                                 if ( ! isset($this->_error_array[$row['field']]))
00673                                 {
00674                                         $this->_error_array[$row['field']] = $message;
00675                                 }
00676                                 
00677                                 return;
00678                         }
00679                 }
00680         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Form_validation::_reduce_array ( array,
keys,
i = 0 
)

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

private

Parameters:
array 
array 
integer 
Returns:
mixed

Definition at line 372 of file Form_validation.php.

Referenced by run().

00373         {
00374                 if (is_array($array))
00375                 {
00376                         if (isset($keys[$i]))
00377                         {
00378                                 if (isset($array[$keys[$i]]))
00379                                 {
00380                                         $array = $this->_reduce_array($array[$keys[$i]], $keys, ($i+1));
00381                                 }
00382                                 else
00383                                 {
00384                                         return NULL;
00385                                 }
00386                         }
00387                         else
00388                         {
00389                                 return $array;
00390                         }
00391                 }
00392         
00393                 return $array;
00394         }

Here is the caller graph for this function:

CI_Form_validation::_reset_post_array (  ) 

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

private

Returns:
null

Definition at line 404 of file Form_validation.php.

References prep_for_form().

Referenced by run().

00405         {
00406                 foreach ($this->_field_data as $field => $row)
00407                 {
00408                         if ( ! is_null($row['postdata']))
00409                         {
00410                                 if ($row['is_array'] == FALSE)
00411                                 {
00412                                         if (isset($_POST[$row['field']]))
00413                                         {
00414                                                 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
00415                                         }
00416                                 }
00417                                 else
00418                                 {
00419                                         $post = '$_POST["';
00420                                         
00421                                         if (count($row['keys']) == 1)
00422                                         {
00423                                                 $post .= current($row['keys']);
00424                                                 $post .= '"]';
00425                                         }
00426                                         else
00427                                         {
00428                                                 $i = 0;
00429                                                 foreach ($row['keys'] as $val)
00430                                                 {
00431                                                         if ($i == 0)
00432                                                         {
00433                                                                 $post .= $val.'"]';
00434                                                                 $i++;
00435                                                                 continue;
00436                                                         }
00437                                                 
00438                                                         $post .= '["'.$val.'"]';
00439                                                 }
00440                                         }
00441                                         
00442                                         if (is_array($row['postdata']))
00443                                         {                                       
00444                                                 $array = array();
00445                                                 foreach ($row['postdata'] as $k => $v)
00446                                                 {
00447                                                         $array[$k] = $this->prep_for_form($v);
00448                                                 }
00449                                                 
00450                                                 $post .= ' = $array;';
00451                                         }
00452                                         else
00453                                         {                                               
00454                                                 $post .= ' = "'.$this->prep_for_form($row['postdata']).'";';
00455                                         }
00456 
00457                                         eval($post);
00458                                 }
00459                         }
00460                 }
00461         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Form_validation::_translate_fieldname ( fieldname  ) 

Translate a field name.

private

Parameters:
string the field name
Returns:
string

Definition at line 691 of file Form_validation.php.

Referenced by _execute().

00692         {
00693                 // Do we need to translate the field name?
00694                 // We look for the prefix lang: to determine this
00695                 if (substr($fieldname, 0, 5) == 'lang:')
00696                 {
00697                         // Grab the variable
00698                         $line = substr($fieldname, 5);                  
00699                         
00700                         // Were we able to translate the field name?  If not we use $line
00701                         if (FALSE === ($fieldname = $this->CI->lang->line($line)))
00702                         {
00703                                 return $line;
00704                         }
00705                 }
00706 
00707                 return $fieldname;
00708         }

Here is the caller graph for this function:

CI_Form_validation::alpha ( str  ) 

Alpha.

public

Parameters:
string 
Returns:
bool

Definition at line 1047 of file Form_validation.php.

01048         {
01049                 return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
01050         }

CI_Form_validation::alpha_dash ( str  ) 

Alpha-numeric with underscores and dashes.

public

Parameters:
string 
Returns:
bool

Definition at line 1075 of file Form_validation.php.

01076         {
01077                 return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
01078         }

CI_Form_validation::alpha_numeric ( str  ) 

Alpha-numeric.

public

Parameters:
string 
Returns:
bool

Definition at line 1061 of file Form_validation.php.

01062         {
01063                 return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
01064         }

CI_Form_validation::CI_Form_validation ( rules = array()  ) 

Constructor.

Definition at line 44 of file Form_validation.php.

References get_instance(), and log_message().

00045         {       
00046                 $this->CI =& get_instance();
00047                 
00048                 // Validation rules can be stored in a config file.
00049                 $this->_config_rules = $rules;
00050                 
00051                 // Automatically load the form helper
00052                 $this->CI->load->helper('form');
00053 
00054                 // Set the character encoding in MB.
00055                 if (function_exists('mb_internal_encoding'))
00056                 {
00057                         mb_internal_encoding($this->CI->config->item('charset'));
00058                 }
00059         
00060                 log_message('debug', "Validation Class Initialized");
00061         }

Here is the call graph for this function:

CI_Form_validation::encode_php_tags ( str  ) 

Convert PHP tags to entities.

public

Parameters:
string 
Returns:
string

Definition at line 1271 of file Form_validation.php.

01272         {
01273                 return str_replace(array('<?php', '<?PHP', '<?', '?>'),  array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
01274         }

CI_Form_validation::error ( field = '',
prefix = '',
suffix = '' 
)

Get Error Message.

Gets the error message associated with a particular field

public

Parameters:
string the field name
Returns:
void

Definition at line 204 of file Form_validation.php.

00205         {       
00206                 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
00207                 {
00208                         return '';
00209                 }
00210                 
00211                 if ($prefix == '')
00212                 {
00213                         $prefix = $this->_error_prefix;
00214                 }
00215 
00216                 if ($suffix == '')
00217                 {
00218                         $suffix = $this->_error_suffix;
00219                 }
00220 
00221                 return $prefix.$this->_field_data[$field]['error'].$suffix;
00222         }

CI_Form_validation::error_string ( prefix = '',
suffix = '' 
)

Error String.

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

public

Parameters:
string 
string 
Returns:
str

Definition at line 236 of file Form_validation.php.

00237         {
00238                 // No errrors, validation passes!
00239                 if (count($this->_error_array) === 0)
00240                 {
00241                         return '';
00242                 }
00243                 
00244                 if ($prefix == '')
00245                 {
00246                         $prefix = $this->_error_prefix;
00247                 }
00248 
00249                 if ($suffix == '')
00250                 {
00251                         $suffix = $this->_error_suffix;
00252                 }
00253                 
00254                 // Generate the error string
00255                 $str = '';
00256                 foreach ($this->_error_array as $val)
00257                 {
00258                         if ($val != '')
00259                         {
00260                                 $str .= $prefix.$val.$suffix."\n";
00261                         }
00262                 }
00263                 
00264                 return $str;
00265         }

CI_Form_validation::exact_length ( str,
val 
)

Exact Length.

public

Parameters:
string 
value 
Returns:
bool

Definition at line 968 of file Form_validation.php.

00969         {
00970                 if (preg_match("/[^0-9]/", $val))
00971                 {
00972                         return FALSE;
00973                 }
00974 
00975                 if (function_exists('mb_strlen'))
00976                 {
00977                         return (mb_strlen($str) != $val) ? FALSE : TRUE;                
00978                 }
00979         
00980                 return (strlen($str) != $val) ? FALSE : TRUE;
00981         }

CI_Form_validation::integer ( str  ) 

Integer.

public

Parameters:
string 
Returns:
bool

Definition at line 1118 of file Form_validation.php.

01119         {
01120                 return (bool)preg_match( '/^[\-+]?[0-9]+$/', $str);
01121         }

CI_Form_validation::is_natural ( str  ) 

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

)

public

Parameters:
string 
Returns:
bool

Definition at line 1132 of file Form_validation.php.

01133     {   
01134                 return (bool)preg_match( '/^[0-9]+$/', $str);
01135     }

CI_Form_validation::is_natural_no_zero ( str  ) 

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

)

public

Parameters:
string 
Returns:
bool

Definition at line 1146 of file Form_validation.php.

01147     {
01148         if ( ! preg_match( '/^[0-9]+$/', $str))
01149         {
01150                 return FALSE;
01151         }
01152         
01153         if ($str == 0)
01154         {
01155                 return FALSE;
01156         }
01157     
01158                 return TRUE;
01159     }

CI_Form_validation::is_numeric ( str  ) 

Is Numeric.

public

Parameters:
string 
Returns:
bool

Definition at line 1104 of file Form_validation.php.

01105     {
01106         return ( ! is_numeric($str)) ? FALSE : TRUE;
01107     } 

CI_Form_validation::matches ( str,
field 
)

Match one field to another.

public

Parameters:
string 
field 
Returns:
bool

Definition at line 896 of file Form_validation.php.

00897         {
00898                 if ( ! isset($_POST[$field]))
00899                 {
00900                         return FALSE;                           
00901                 }
00902                 
00903                 $field = $_POST[$field];
00904 
00905                 return ($str !== $field) ? FALSE : TRUE;
00906         }

CI_Form_validation::max_length ( str,
val 
)

Max Length.

public

Parameters:
string 
value 
Returns:
bool

Definition at line 943 of file Form_validation.php.

00944         {
00945                 if (preg_match("/[^0-9]/", $val))
00946                 {
00947                         return FALSE;
00948                 }
00949 
00950                 if (function_exists('mb_strlen'))
00951                 {
00952                         return (mb_strlen($str) > $val) ? FALSE : TRUE;         
00953                 }
00954         
00955                 return (strlen($str) > $val) ? FALSE : TRUE;
00956         }

CI_Form_validation::min_length ( str,
val 
)

Minimum Length.

public

Parameters:
string 
value 
Returns:
bool

Definition at line 918 of file Form_validation.php.

00919         {
00920                 if (preg_match("/[^0-9]/", $val))
00921                 {
00922                         return FALSE;
00923                 }
00924 
00925                 if (function_exists('mb_strlen'))
00926                 {
00927                         return (mb_strlen($str) < $val) ? FALSE : TRUE;         
00928                 }
00929         
00930                 return (strlen($str) < $val) ? FALSE : TRUE;
00931         }

CI_Form_validation::numeric ( str  ) 

Numeric.

public

Parameters:
string 
Returns:
bool

Definition at line 1089 of file Form_validation.php.

01090         {
01091                 return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
01092 
01093         }

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.

public

Parameters:
string 
Returns:
string

Definition at line 1190 of file Form_validation.php.

Referenced by _reset_post_array().

01191         {
01192                 if (is_array($data))
01193                 {
01194                         foreach ($data as $key => $val)
01195                         {
01196                                 $data[$key] = $this->prep_for_form($val);
01197                         }
01198                         
01199                         return $data;
01200                 }
01201                 
01202                 if ($this->_safe_form_data == FALSE OR $data === '')
01203                 {
01204                         return $data;
01205                 }
01206 
01207                 return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
01208         }

Here is the caller graph for this function:

CI_Form_validation::prep_url ( str = ''  ) 

Prep URL.

public

Parameters:
string 
Returns:
string

Definition at line 1219 of file Form_validation.php.

01220         {
01221                 if ($str == 'http://' OR $str == '')
01222                 {
01223                         return '';
01224                 }
01225                 
01226                 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
01227                 {
01228                         $str = 'http://'.$str;
01229                 }
01230                 
01231                 return $str;
01232         }

CI_Form_validation::required ( str  ) 

Required.

public

Parameters:
string 
Returns:
bool

Definition at line 874 of file Form_validation.php.

00875         {
00876                 if ( ! is_array($str))
00877                 {
00878                         return (trim($str) == '') ? FALSE : TRUE;
00879                 }
00880                 else
00881                 {
00882                         return ( ! empty($str));
00883                 }
00884         }

CI_Form_validation::run ( group = ''  ) 

Run the Validator.

This function does all the work.

public

Returns:
bool

Definition at line 277 of file Form_validation.php.

References _execute(), _reduce_array(), _reset_post_array(), log_message(), and set_rules().

00278         {
00279                 // Do we even have any data to process?  Mm?
00280                 if (count($_POST) == 0)
00281                 {
00282                         return FALSE;
00283                 }
00284                 
00285                 // Does the _field_data array containing the validation rules exist?
00286                 // If not, we look to see if they were assigned via a config file
00287                 if (count($this->_field_data) == 0)
00288                 {
00289                         // No validation rules?  We're done...
00290                         if (count($this->_config_rules) == 0)
00291                         {
00292                                 return FALSE;
00293                         }
00294                         
00295                         // Is there a validation rule for the particular URI being accessed?
00296                         $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
00297                         
00298                         if ($uri != '' AND isset($this->_config_rules[$uri]))
00299                         {
00300                                 $this->set_rules($this->_config_rules[$uri]);
00301                         }
00302                         else
00303                         {
00304                                 $this->set_rules($this->_config_rules);
00305                         }
00306         
00307                         // We're we able to set the rules correctly?
00308                         if (count($this->_field_data) == 0)
00309                         {
00310                                 log_message('debug', "Unable to find validation rules");
00311                                 return FALSE;
00312                         }
00313                 }
00314         
00315                 // Load the language file containing error messages
00316                 $this->CI->lang->load('form_validation');
00317                                                         
00318                 // Cycle through the rules for each field, match the 
00319                 // corresponding $_POST item and test for errors
00320                 foreach ($this->_field_data as $field => $row)
00321                 {               
00322                         // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
00323                         // Depending on whether the field name is an array or a string will determine where we get it from.
00324                         
00325                         if ($row['is_array'] == TRUE)
00326                         {
00327                                 $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
00328                         }
00329                         else
00330                         {
00331                                 if (isset($_POST[$field]) AND $_POST[$field] != "")
00332                                 {
00333                                         $this->_field_data[$field]['postdata'] = $_POST[$field];
00334                                 }
00335                         }
00336                 
00337                         $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);             
00338                 }
00339 
00340                 // Did we end up with any errors?
00341                 $total_errors = count($this->_error_array);
00342 
00343                 if ($total_errors > 0)
00344                 {
00345                         $this->_safe_form_data = TRUE;
00346                 }
00347 
00348                 // Now we need to re-set the POST data with the new, processed data
00349                 $this->_reset_post_array();
00350                 
00351                 // No errors, validation passes!
00352                 if ($total_errors == 0)
00353                 {
00354                         return TRUE;
00355                 }
00356 
00357                 // Validation fails
00358                 return FALSE;
00359         }

Here is the call graph for this function:

CI_Form_validation::set_checkbox ( field = '',
value = '',
default = FALSE 
)

Set Checkbox.

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

public

Parameters:
string 
string 
Returns:
string

Definition at line 834 of file Form_validation.php.

00835         {
00836                 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
00837                 {
00838                         if ($default === TRUE AND count($this->_field_data) === 0)
00839                         {
00840                                 return ' checked="checked"';
00841                         }
00842                         return '';
00843                 }
00844         
00845                 $field = $this->_field_data[$field]['postdata'];
00846                 
00847                 if (is_array($field))
00848                 {
00849                         if ( ! in_array($value, $field))
00850                         {
00851                                 return '';
00852                         }
00853                 }
00854                 else
00855                 {
00856                         if (($field == '' OR $value == '') OR ($field != $value))
00857                         {
00858                                 return '';
00859                         }
00860                 }
00861                         
00862                 return ' checked="checked"';
00863         }

CI_Form_validation::set_error_delimiters ( prefix = '<p>',
suffix = '</p>' 
)

Set The Error Delimiter.

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

public

Parameters:
string 
string 
Returns:
void

Definition at line 187 of file Form_validation.php.

00188         {
00189                 $this->_error_prefix = $prefix;
00190                 $this->_error_suffix = $suffix;
00191         }

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.

public

Parameters:
string 
string 
Returns:
string

Definition at line 165 of file Form_validation.php.

References $lang.

00166         {
00167                 if ( ! is_array($lang))
00168                 {
00169                         $lang = array($lang => $val);
00170                 }
00171         
00172                 $this->_error_messages = array_merge($this->_error_messages, $lang);
00173         }

CI_Form_validation::set_radio ( field = '',
value = '',
default = FALSE 
)

Set Radio.

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

public

Parameters:
string 
string 
Returns:
string

Definition at line 790 of file Form_validation.php.

00791         {
00792                 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
00793                 {
00794                         if ($default === TRUE AND count($this->_field_data) === 0)
00795                         {
00796                                 return ' checked="checked"';
00797                         }
00798                         return '';
00799                 }
00800         
00801                 $field = $this->_field_data[$field]['postdata'];
00802                 
00803                 if (is_array($field))
00804                 {
00805                         if ( ! in_array($value, $field))
00806                         {
00807                                 return '';
00808                         }
00809                 }
00810                 else
00811                 {
00812                         if (($field == '' OR $value == '') OR ($field != $value))
00813                         {
00814                                 return '';
00815                         }
00816                 }
00817                         
00818                 return ' checked="checked"';
00819         }

CI_Form_validation::set_rules ( field,
label = '',
rules = '' 
)

Set Rules.

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

public

Parameters:
mixed 
string 
Returns:
void

Definition at line 76 of file Form_validation.php.

Referenced by run().

00077         {
00078                 // No reason to set rules if we have no POST data
00079                 if (count($_POST) == 0)
00080                 {
00081                         return;
00082                 }
00083         
00084                 // If an array was passed via the first parameter instead of indidual string
00085                 // values we cycle through it and recursively call this function.
00086                 if (is_array($field))
00087                 {
00088                         foreach ($field as $row)
00089                         {
00090                                 // Houston, we have a problem...
00091                                 if ( ! isset($row['field']) OR ! isset($row['rules']))
00092                                 {
00093                                         continue;
00094                                 }
00095 
00096                                 // If the field label wasn't passed we use the field name
00097                                 $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
00098 
00099                                 // Here we go!
00100                                 $this->set_rules($row['field'], $label, $row['rules']);
00101                         }
00102                         return;
00103                 }
00104                 
00105                 // No fields? Nothing to do...
00106                 if ( ! is_string($field) OR  ! is_string($rules) OR $field == '')
00107                 {
00108                         return;
00109                 }
00110 
00111                 // If the field label wasn't passed we use the field name
00112                 $label = ($label == '') ? $field : $label;
00113 
00114                 // Is the field name an array?  We test for the existence of a bracket "[" in
00115                 // the field name to determine this.  If it is an array, we break it apart
00116                 // into its components so that we can fetch the corresponding POST data later           
00117                 if (strpos($field, '[') !== FALSE AND preg_match_all('/\[(.*?)\]/', $field, $matches))
00118                 {       
00119                         // Note: Due to a bug in current() that affects some versions
00120                         // of PHP we can not pass function call directly into it
00121                         $x = explode('[', $field);
00122                         $indexes[] = current($x);
00123 
00124                         for ($i = 0; $i < count($matches['0']); $i++)
00125                         {
00126                                 if ($matches['1'][$i] != '')
00127                                 {
00128                                         $indexes[] = $matches['1'][$i];
00129                                 }
00130                         }
00131                         
00132                         $is_array = TRUE;
00133                 }
00134                 else
00135                 {
00136                         $indexes        = array();
00137                         $is_array       = FALSE;                
00138                 }
00139                 
00140                 // Build our master array               
00141                 $this->_field_data[$field] = array(
00142                                                                                         'field'                         => $field, 
00143                                                                                         'label'                         => $label, 
00144                                                                                         'rules'                         => $rules,
00145                                                                                         'is_array'                      => $is_array,
00146                                                                                         'keys'                          => $indexes,
00147                                                                                         'postdata'                      => NULL,
00148                                                                                         'error'                         => ''
00149                                                                                         );
00150         }

Here is the caller graph for this function:

CI_Form_validation::set_select ( field = '',
value = '',
default = FALSE 
)

Set Select.

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

public

Parameters:
string 
string 
Returns:
string

Definition at line 746 of file Form_validation.php.

00747         {               
00748                 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
00749                 {
00750                         if ($default === TRUE AND count($this->_field_data) === 0)
00751                         {
00752                                 return ' selected="selected"';
00753                         }
00754                         return '';
00755                 }
00756         
00757                 $field = $this->_field_data[$field]['postdata'];
00758                 
00759                 if (is_array($field))
00760                 {
00761                         if ( ! in_array($value, $field))
00762                         {
00763                                 return '';
00764                         }
00765                 }
00766                 else
00767                 {
00768                         if (($field == '' OR $value == '') OR ($field != $value))
00769                         {
00770                                 return '';
00771                         }
00772                 }
00773                         
00774                 return ' selected="selected"';
00775         }

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

public

Parameters:
string the field name
string 
Returns:
void

Definition at line 723 of file Form_validation.php.

00724         {
00725                 if ( ! isset($this->_field_data[$field]))
00726                 {
00727                         return $default;
00728                 }
00729                 
00730                 return $this->_field_data[$field]['postdata'];
00731         }

CI_Form_validation::strip_image_tags ( str  ) 

Strip Image Tags.

public

Parameters:
string 
Returns:
string

Definition at line 1243 of file Form_validation.php.

01244         {
01245                 return $this->CI->input->strip_image_tags($str);
01246         }

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

public

Parameters:
string 
Returns:
bool

Definition at line 1173 of file Form_validation.php.

01174         {
01175                 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
01176         }

CI_Form_validation::valid_email ( str  ) 

Valid Email.

public

Parameters:
string 
Returns:
bool

Definition at line 992 of file Form_validation.php.

Referenced by valid_emails().

00993         {
00994                 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
00995         }

Here is the caller graph for this function:

CI_Form_validation::valid_emails ( str  ) 

Valid Emails.

public

Parameters:
string 
Returns:
bool

Definition at line 1006 of file Form_validation.php.

References valid_email().

01007         {
01008                 if (strpos($str, ',') === FALSE)
01009                 {
01010                         return $this->valid_email(trim($str));
01011                 }
01012                 
01013                 foreach(explode(',', $str) as $email)
01014                 {
01015                         if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE)
01016                         {
01017                                 return FALSE;
01018                         }
01019                 }
01020                 
01021                 return TRUE;
01022         }

Here is the call graph for this function:

CI_Form_validation::valid_ip ( ip  ) 

Validate IP Address.

public

Parameters:
string 
Returns:
string

Definition at line 1033 of file Form_validation.php.

01034         {
01035                 return $this->CI->input->valid_ip($ip);
01036         }

CI_Form_validation::xss_clean ( str  ) 

XSS Clean.

public

Parameters:
string 
Returns:
string

Definition at line 1257 of file Form_validation.php.

01258         {
01259                 return $this->CI->input->xss_clean($str);
01260         }


Member Data Documentation

CI_Form_validation::$_config_rules = array()

Definition at line 31 of file Form_validation.php.

CI_Form_validation::$_error_array = array()

Definition at line 32 of file Form_validation.php.

CI_Form_validation::$_error_messages = array()

Definition at line 33 of file Form_validation.php.

CI_Form_validation::$_error_prefix = '<p>'

Definition at line 34 of file Form_validation.php.

CI_Form_validation::$_error_suffix = '</p>'

Definition at line 35 of file Form_validation.php.

CI_Form_validation::$_field_data = array()

Definition at line 30 of file Form_validation.php.

CI_Form_validation::$_safe_form_data = FALSE

Definition at line 37 of file Form_validation.php.

CI_Form_validation::$CI

Definition at line 29 of file Form_validation.php.

CI_Form_validation::$error_string = ''

Definition at line 36 of file Form_validation.php.


The documentation for this class was generated from the following file: