Cake\View\Helper\FormHelper::error PHP Метод

error() публичный Метод

Uses the error, errorList and errorItem templates. The errorList and errorItem templates are used to format multiple error messages per field. ### Options: - escape boolean - Whether or not to html escape the contents of the error.
public error ( string $field, string | array | null $text = null, array $options = [] ) : string
$field string A field name, like "modelname.fieldname"
$text string | array | null Error message as string or array of messages. If an array, it should be a hash of key names => messages.
$options array See above.
Результат string Formatted errors or ''.
    public function error($field, $text = null, array $options = [])
    {
        if (substr($field, -5) === '._ids') {
            $field = substr($field, 0, -5);
        }
        $options += ['escape' => true];
        $context = $this->_getContext();
        if (!$context->hasError($field)) {
            return '';
        }
        $error = (array) $context->error($field);
        if (is_array($text)) {
            $tmp = [];
            foreach ($error as $k => $e) {
                if (isset($text[$k])) {
                    $tmp[] = $text[$k];
                } elseif (isset($text[$e])) {
                    $tmp[] = $text[$e];
                } else {
                    $tmp[] = $e;
                }
            }
            $text = $tmp;
        }
        if ($text !== null) {
            $error = $text;
        }
        if ($options['escape']) {
            $error = h($error);
            unset($options['escape']);
        }
        if (is_array($error)) {
            if (count($error) > 1) {
                $errorText = [];
                foreach ($error as $err) {
                    $errorText[] = $this->formatTemplate('errorItem', ['text' => $err]);
                }
                $error = $this->formatTemplate('errorList', ['content' => implode('', $errorText)]);
            } else {
                $error = array_pop($error);
            }
        }
        return $this->formatTemplate('error', ['content' => $error]);
    }