lithium\template\helper\Form::error()

public method

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 is true, the first available error is used.

  • array $options

    Any rendering options or HTML attributes to be used when rendering the error.

Returns

string

Returns a rendered error message based on the 'error' string template.

Source

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