0 follower

Final Class Yiisoft\Form\Field\Part\Error

InheritanceYiisoft\Form\Field\Part\Error » Yiisoft\Widget\Widget
Uses TraitsYiisoft\Form\Field\Base\InputData\InputDataTrait

Represent a field validation error (if there are several errors, the first one is used). If field is no validation error, field part will be hidden.

Psalm Types

Name Value
MessageCallback callable

Method Details

Hide inherited methods

addAttributes() public method

public addAttributes( array $attributes ): self
$attributes array

                public function addAttributes(array $attributes): self
{
    $new = clone $this;
    $new->attributes = array_merge($this->attributes, $attributes);
    return $new;
}

            
addClass() public method

Add one or more CSS classes to the tag.

public addClass( string|null $class ): self
$class string|null

One or many CSS classes.

                public function addClass(?string ...$class): self
{
    $new = clone $this;
    Html::addCssClass($new->attributes, $class);
    return $new;
}

            
addErrorAttributes() public method

public addErrorAttributes( array $attributes ): self
$attributes array

                public function addErrorAttributes(array $attributes): self
{
    $new = clone $this;
    $new->errorAttributes = array_merge($this->errorAttributes, $attributes);
    return $new;
}

            
addHeaderAttributes() public method

public addHeaderAttributes( array $attributes ): self
$attributes array

                public function addHeaderAttributes(array $attributes): self
{
    $new = clone $this;
    $new->headerAttributes = array_merge($this->headerAttributes, $attributes);
    return $new;
}

            
attributes() public method

public attributes( array $attributes ): self
$attributes array

                public function attributes(array $attributes): self
{
    $new = clone $this;
    $new->attributes = $attributes;
    return $new;
}

            
class() public method

Replace tag CSS classes with a new set of classes.

public class( string|null $class ): self
$class string|null

One or many CSS classes.

                public function class(?string ...$class): self
{
    $new = clone $this;
    $new->attributes['class'] = array_filter($class, static fn($c) => $c !== null);
    return $new;
}

            
encode() public method

Whether error messages should be HTML-encoded.

public encode( boolean $value ): self
$value boolean

                public function encode(bool $value): self
{
    $new = clone $this;
    $new->encode = $value;
    return $new;
}

            
errorAttributes() public method

public errorAttributes( array $attributes ): self
$attributes array

                public function errorAttributes(array $attributes): self
{
    $new = clone $this;
    $new->errorAttributes = $attributes;
    return $new;
}

            
errorTag() public method

Set an error item tag name.

public errorTag( string $tag ): self
$tag string

Error item tag name.

                public function errorTag(string $tag): self
{
    if ($tag === '') {
        throw new InvalidArgumentException('Tag name cannot be empty.');
    }
    $new = clone $this;
    $new->errorTag = $tag;
    return $new;
}

            
getInputData() protected method
protected getInputData( ): Yiisoft\Form\Field\Base\InputData\InputDataInterface

                final protected function getInputData(): InputDataInterface
{
    if ($this->inputData === null) {
        $this->inputData = new InputData();
    }
    return $this->inputData;
}

            
getThemeConfig() protected static method

protected static getThemeConfig( string|null $theme ): array
$theme string|null

                protected static function getThemeConfig(?string $theme): array
{
    return ThemeContainer::getTheme($theme)?->getErrorConfig() ?? [];
}

            
header() public method

public header( string|null $header ): self
$header string|null

                public function header(?string $header): self
{
    $new = clone $this;
    $new->header = $header;
    return $new;
}

            
headerAttributes() public method

public headerAttributes( array $attributes ): self
$attributes array

                public function headerAttributes(array $attributes): self
{
    $new = clone $this;
    $new->headerAttributes = $attributes;
    return $new;
}

            
headerEncode() public method

Whether header content should be HTML-encoded.

public headerEncode( boolean $encode ): self
$encode boolean

                public function headerEncode(bool $encode): self
{
    $new = clone $this;
    $new->headerEncode = $encode;
    return $new;
}

            
headerTag() public method

Set the header tag name.

public headerTag( string|null $tag ): self
$tag string|null

Header tag name.

                public function headerTag(?string $tag): self
{
    if ($tag === '') {
        throw new InvalidArgumentException('Tag name cannot be empty.');
    }
    $new = clone $this;
    $new->headerTag = $tag;
    return $new;
}

            
id() public method

Set tag ID.

public id( string|null $id ): self
$id string|null

Tag ID.

                public function id(?string $id): self
{
    $new = clone $this;
    $new->attributes['id'] = $id;
    return $new;
}

            
inputData() public method
public inputData( Yiisoft\Form\Field\Base\InputData\InputDataInterface $inputData ): Yiisoft\Form\Field\Part\Error
$inputData Yiisoft\Form\Field\Base\InputData\InputDataInterface

                final public function inputData(InputDataInterface $inputData): static
{
    $new = clone $this;
    $new->inputData = $inputData;
    return $new;
}

            
message() public method

Error messages to display.

public message( string|null $message, string $messages ): self
$message string|null
$messages string

                public function message(?string $message, string ...$messages): self
{
    $new = clone $this;
    $new->messages = $message === null ? null : [$message, ...$messages];
    return $new;
}

            
messageCallback() public method

Callback that will be called to obtain an error message.

public messageCallback( callable|null $callback ): self
$callback callable|null

                public function messageCallback(?callable $callback): self
{
    $new = clone $this;
    $new->messageCallback = $callback;
    return $new;
}

            
onlyFirst() public method

public onlyFirst( boolean $value true ): self
$value boolean

                public function onlyFirst(bool $value = true): self
{
    $new = clone $this;
    $new->onlyFirst = $value;
    return $new;
}

            
render() public method

Generates a tag that contains the first validation error of the specified form attribute.

public render( ): string
return string

The generated error tag.

                public function render(): string
{
    $messages = $this->messages ?? $this->getInputData()->getValidationErrors();
    if (empty($messages)) {
        return '';
    }
    if ($this->onlyFirst) {
        $messages = [reset($messages)];
    }
    $messageCallback = $this->messageCallback;
    if ($messageCallback !== null) {
        $messages = array_map(
            fn(string $message) => $messageCallback($message, $this->getInputData()),
            $messages,
        );
    }
    $content = [];
    if ($this->header !== null) {
        $content[] = $this->headerTag === null
            ? ($this->headerEncode ? Html::encode($this->header) : $this->header)
            : CustomTag::name($this->headerTag)
                ->attributes($this->headerAttributes)
                ->content($this->header)
                ->encode($this->headerEncode)
                ->render();
        $content[] = "\n";
    }
    $isFirst = true;
    foreach ($messages as $message) {
        if ($isFirst) {
            $isFirst = false;
        } else {
            $content[] = $this->separator;
        }
        $content[] = $this->errorTag === null
            ? ($this->encode ? Html::encode($message) : $message)
            : CustomTag::name($this->errorTag)
                ->attributes($this->errorAttributes)
                ->content($message)
                ->encode($this->encode)
                ->render();
    }
    return CustomTag::name($this->tag)
        ->addAttributes($this->attributes)
        ->content(...(count($messages) === 1 ? $content : ["\n", ...$content, "\n"]))
        ->encode(false)
        ->render();
}

            
separator() public method

public separator( string $separator ): self
$separator string

                public function separator(string $separator): self
{
    $new = clone $this;
    $new->separator = $separator;
    return $new;
}

            
tag() public method

Set the container tag name for the error.

public tag( string $tag ): self
$tag string

Container tag name.

                public function tag(string $tag): self
{
    if ($tag === '') {
        throw new InvalidArgumentException('Tag name cannot be empty.');
    }
    $new = clone $this;
    $new->tag = $tag;
    return $new;
}