Final Class Yiisoft\Form\Field\Part\Error
| Inheritance | Yiisoft\Form\Field\Part\Error » Yiisoft\Widget\Widget |
|---|---|
| Uses Traits | Yiisoft\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 |
Public Methods
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| getInputData() | Yiisoft\Form\Field\Base\InputData\InputDataTrait | |
| getThemeConfig() | Yiisoft\Form\Field\Part\Error |
Method Details
| 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;
}
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;
}
| 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;
}
| 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;
}
| public self attributes ( array $attributes ) | ||
| $attributes | array | |
public function attributes(array $attributes): self
{
$new = clone $this;
$new->attributes = $attributes;
return $new;
}
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;
}
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;
}
| public self errorAttributes ( array $attributes ) | ||
| $attributes | array | |
public function errorAttributes(array $attributes): self
{
$new = clone $this;
$new->errorAttributes = $attributes;
return $new;
}
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;
}
| protected Yiisoft\Form\Field\Base\InputData\InputDataInterface getInputData ( ) |
final protected function getInputData(): InputDataInterface
{
if ($this->inputData === null) {
$this->inputData = new InputData();
}
return $this->inputData;
}
| protected static array getThemeConfig ( string|null $theme ) | ||
| $theme | string|null | |
protected static function getThemeConfig(?string $theme): array
{
return ThemeContainer::getTheme($theme)?->getErrorConfig() ?? [];
}
| public self header ( string|null $header ) | ||
| $header | string|null | |
public function header(?string $header): self
{
$new = clone $this;
$new->header = $header;
return $new;
}
| public self headerAttributes ( array $attributes ) | ||
| $attributes | array | |
public function headerAttributes(array $attributes): self
{
$new = clone $this;
$new->headerAttributes = $attributes;
return $new;
}
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;
}
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;
}
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;
}
| 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;
}
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;
}
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;
}
| public self onlyFirst ( boolean $value = true ) | ||
| $value | boolean | |
public function onlyFirst(bool $value = true): self
{
$new = clone $this;
$new->onlyFirst = $value;
return $new;
}
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();
}
| public self separator ( string $separator ) | ||
| $separator | string | |
public function separator(string $separator): self
{
$new = clone $this;
$new->separator = $separator;
return $new;
}
Signup or Login in order to comment.