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);
}
}
}
}
}