LazyRecord\BaseModel::_validateColumn PHP Метод

_validateColumn() защищенный Метод

A validator could be: 1. a ValidationKit validator, 2. a closure 3. a function name The validation result must be returned as in following format: boolean (valid or invalid, true or false) array( boolean valid , string message ) ValidationKit\ValidationMessage object. This method returns (object) { valid: boolean valid or invalid field: string field name message: }
protected _validateColumn ( RuntimeColumn $column, $val, array $args )
$column LazyRecord\Schema\RuntimeColumn
$args array
    protected function _validateColumn(RuntimeColumn $column, $val, array $args)
    {
        // check for requried columns
        if ($column->required && ($val === '' || $val === null)) {
            return array('valid' => false, 'message' => sprintf(_('Field %s is required.'), $column->getLabel()), 'field' => $column->name);
        }
        // XXX: migrate this method to runtime column
        if ($validator = $column->validator) {
            if (is_callable($validator)) {
                $ret = call_user_func($validator, $val, $args, $this);
                if (is_bool($ret)) {
                    return array('valid' => $ret, 'message' => 'Validation failed.', 'field' => $column->name);
                } elseif (is_array($ret)) {
                    return array('valid' => $ret[0], 'message' => $ret[1], 'field' => $column->name);
                } else {
                    throw new Exception('Wrong validation result format, Please returns (valid,message) or (valid)');
                }
            } elseif (is_string($validator) && is_a($validator, 'ValidationKit\\Validator', true)) {
                // it's a ValidationKit\Validator
                $validator = $column->validatorArgs ? new $validator($column->get('validatorArgs')) : new $validator();
                $ret = $validator->validate($val);
                $msgs = $validator->getMessages();
                $msg = isset($msgs[0]) ? $msgs[0] : 'Validation failed.';
                return array('valid' => $ret, 'message' => $msg, 'field' => $column->name);
            } else {
                throw new Exception('Unsupported validator');
            }
        }
        if ($val && $column->validValues) {
            if ($validValues = $column->getValidValues($this, $args)) {
                // sort by index
                if (isset($validValues[0]) && !in_array($val, $validValues)) {
                    return array('valid' => false, 'message' => sprintf('%s is not a valid value for %s', $val, $column->name), 'field' => $column->name);
                } else {
                    $values = array_values($validValues);
                    foreach ($values as &$v) {
                        if (is_array($v)) {
                            $v = array_values($v);
                        }
                    }
                    if (!in_array($val, $values)) {
                        return array('valid' => false, 'message' => sprintf(_('%s is not a valid value for %s'), $val, $column->name), 'field' => $column->name);
                    }
                }
            }
        }
    }