Valitron\Validator::error PHP Method

error() public method

Add an error to error messages array
public error ( string $field, string $msg, array $params = [] )
$field string
$msg string
$params array
    public function error($field, $msg, array $params = array())
    {
        $msg = $this->checkAndSetLabel($field, $msg, $params);
        $values = array();
        // Printed values need to be in string format
        foreach ($params as $param) {
            if (is_array($param)) {
                $param = "['" . implode("', '", $param) . "']";
            }
            if ($param instanceof \DateTime) {
                $param = $param->format('Y-m-d');
            } else {
                if (is_object($param)) {
                    $param = get_class($param);
                }
            }
            // Use custom label instead of field name if set
            if (is_string($params[0])) {
                if (isset($this->_labels[$param])) {
                    $param = $this->_labels[$param];
                }
            }
            $values[] = $param;
        }
        $this->_errors[$field][] = vsprintf($msg, $values);
    }

Usage Example

Example #1
0
 /**
  * @dataProvider dataProviderFor_testError
  */
 public function testError($expected, $input, $test, $message)
 {
     $v = new Validator(array('test' => $input));
     $v->error('test', $message, $test);
     $this->assertEquals(array('test' => array($expected)), $v->errors());
 }