lithium\template\helper\Form::error PHP Method

error() public method

Generates an error message for a field which is part of an object bound to a form in create().
public error ( string $name, mixed $key = null, array $options = [] ) : string
$name string The name of the field for which to render an error.
$key mixed 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` is `true`, the first available error is used.
$options array Any rendering options or HTML attributes to be used when rendering the error.
return string Returns a rendered error message based on the `'error'` string template.
    public function error($name, $key = null, array $options = array())
    {
        $defaults = array('class' => 'error');
        list(, $options, $template) = $this->_defaults(__FUNCTION__, $name, $options);
        $options += $defaults;
        $params = compact('name', 'key', 'options', 'template');
        return $this->_filter(__METHOD__, $params, function ($self, $params) {
            $options = $params['options'];
            $template = $params['template'];
            if (isset($options['value'])) {
                unset($options['value']);
            }
            if (!($content = $self->binding($params['name'])->errors)) {
                return null;
            }
            $result = '';
            if (!is_array($content)) {
                $args = array(__METHOD__, $template, compact('content', 'options'));
                return $self->invokeMethod('_render', $args);
            }
            $errors = $content;
            if ($params['key'] === null) {
                foreach ($errors as $content) {
                    $args = array(__METHOD__, $template, compact('content', 'options'));
                    $result .= $self->invokeMethod('_render', $args);
                }
                return $result;
            }
            $key = $params['key'];
            $content = !isset($errors[$key]) || $key === true ? reset($errors) : $errors[$key];
            $args = array(__METHOD__, $template, compact('content', 'options'));
            return $self->invokeMethod('_render', $args);
        });
    }