CI_Form_validation::matches PHP Method

matches() public method

Match one field to another
public matches ( string $str, string $field ) : boolean
$str string string to compare against
$field string
return boolean
    public function matches($str, $field)
    {
        return isset($this->_field_data[$field], $this->_field_data[$field]['postdata']) ? $str === $this->_field_data[$field]['postdata'] : FALSE;
    }

Usage Example

 /**
  * Better version of standard matches method, this one will check if field is array and find appropriate value of this field.
  * @param string $str current value of form field.
  * @param string $field form field name, can be array.
  * @return boolean validation result.
  */
 public function matches($str, $field)
 {
     if (strpos($field, '[') !== FALSE) {
         $path = explode('[', str_replace(']', '', $field));
         $fld = isset($_POST[$path[0]]) ? $_POST[$path[0]] : FALSE;
         if ($fld === FALSE) {
             return FALSE;
         }
         if (count($path) > 1) {
             for ($i = 1; $i < count($path); $i++) {
                 $segment = $path[$i];
                 if (!isset($fld[$segment])) {
                     return FALSE;
                 }
                 $fld = $fld[$segment];
             }
         }
         if ($str == $fld) {
             return TRUE;
         }
         return FALSE;
     } else {
         return parent::matches($str, $field);
     }
 }