lithium\template\helper\Form::error()
Generates an error message for a field which is part of an object bound to a form in
create()
.
Parameters
-
string
$name
The name of the field for which to render an error.
-
mixed
$key
If more than one error is present for
$name
, a key may be specified. If$key
is not set in the array of errors, or if$key
istrue
, the first available error is used. -
array
$options
Any rendering options or HTML attributes to be used when rendering the error.
Returns
stringReturns a rendered error message based on the 'error'
string template.
Source
public function error($name, $key = null, array $options = []) {
$defaults = ['class' => 'error', 'messages' => []];
list(, $options, $template) = $this->_defaults(__FUNCTION__, $name, $options);
$options += $defaults;
if (is_array($options['messages'])) {
$messages = $options['messages'] + ['default' => null];
} else {
$messages = ['default' => $options['messages']];
}
unset($options['messages']);
$params = compact('name', 'key', 'messages', 'options', 'template');
return Filters::run($this, __FUNCTION__, $params, function($params) {
$options = $params['options'];
$template = $params['template'];
$messages = $params['messages'];
if (isset($options['value'])) {
unset($options['value']);
}
if (!$content = $this->binding($params['name'])->errors) {
return null;
}
$result = '';
if (!is_array($content)) {
return $this->_render(__METHOD__, $template, compact('content', 'options'));
}
$errors = $content;
if ($params['key'] === null) {
foreach ($errors as $rule => $content) {
if (isset($messages[$rule])) {
$content = $messages[$rule];
} elseif ($messages['default']) {
$content = $messages['default'];
}
$result .= $this->_render(__METHOD__, $template, compact('content', 'options'));
}
return $result;
}
$key = $params['key'];
$content = !isset($errors[$key]) || $key === true ? reset($errors) : $errors[$key];
return $this->_render(__METHOD__, $template, compact('content', 'options'));
});
}