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 self addAttributes ( array $attributes )
$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 self addClass ( string|null $class )
$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 self addErrorAttributes ( array $attributes )
$attributes array

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

            
addHeaderAttributes() public method

public self addHeaderAttributes ( array $attributes )
$attributes array

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

            
attributes() public method

public self attributes ( array $attributes )
$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 self class ( string|null $class )
$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 self encode ( boolean $value )
$value boolean

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

            
errorAttributes() public method

public self errorAttributes ( array $attributes )
$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 self errorTag ( string $tag )
$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 Yiisoft\Form\Field\Base\InputData\InputDataInterface getInputData ( )

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

            
getThemeConfig() protected static method

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

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

            
header() public method

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

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

            
headerAttributes() public method

public self headerAttributes ( array $attributes )
$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 self headerEncode ( boolean $encode )
$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 self headerTag ( string|null $tag )
$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 self id ( string|null $id )
$id string|null

Tag ID.

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

            
inputData() public method
public Yiisoft\Form\Field\Part\Error inputData ( Yiisoft\Form\Field\Base\InputData\InputDataInterface $inputData )
$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 self message ( string|null $message, string $messages )
$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 self messageCallback ( callable|null $callback )
$callback callable|null

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

            
onlyFirst() public method

public self onlyFirst ( boolean $value true )
$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 string render ( )
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 self separator ( string $separator )
$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 self tag ( string $tag )
$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;
}