Final Class Yiisoft\Form\Field\Part\Label
| Inheritance | Yiisoft\ |
|---|---|
| Uses Traits | Yiisoft\ |
Represents label for a form field.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| addAttributes() | Yiisoft\ |
|
| addClass() | Add one or more CSS classes to the tag. | Yiisoft\ |
| attributes() | Yiisoft\ |
|
| class() | Replace tag CSS classes with a new set of classes. | Yiisoft\ |
| content() | Yiisoft\ |
|
| encode() | Whether content should be HTML-encoded. | Yiisoft\ |
| forId() | Yiisoft\ |
|
| id() | Set tag ID. | Yiisoft\ |
| inputData() | Yiisoft\ |
|
| render() | Yiisoft\ |
|
| setFor() | Yiisoft\ |
|
| useInputId() | Yiisoft\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| getInputData() | Yiisoft\ |
|
| getThemeConfig() | Yiisoft\ |
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 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;
}
| public self content ( string|\ | ||
| $content | string|\ |
|
public function content(string|Stringable|null $content): self
{
$new = clone $this;
$new->content = $content;
return $new;
}
Whether content 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 forId ( ?string $id ) | ||
| $id | ?string | |
public function forId(?string $id): self
{
$new = clone $this;
$new->forId = $id;
return $new;
}
| protected Yiisoft\ |
final protected function getInputData(): InputDataInterface
{
if ($this->inputData === null) {
$this->inputData = new InputData();
}
return $this->inputData;
}
| protected static array getThemeConfig ( ?string $theme ) | ||
| $theme | ?string | |
protected static function getThemeConfig(?string $theme): array
{
return ThemeContainer::getTheme($theme)?->getLabelConfig() ?? [];
}
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 static inputData ( Yiisoft\ | ||
| $inputData | Yiisoft\ |
|
final public function inputData(InputDataInterface $inputData): static
{
$new = clone $this;
$new->inputData = $inputData;
return $new;
}
| public string render ( ) |
public function render(): string
{
$content = $this->content ?? $this->getInputData()->getLabel();
if (empty($content)) {
return '';
}
$labelAttributes = $this->attributes;
if ($this->setFor && !isset($labelAttributes['for'])) {
$id = $this->forId;
if ($id === null && $this->useInputId) {
$id = $this->getInputData()->getId();
}
if ($id !== null) {
$labelAttributes['for'] = $id;
}
}
$tag = Html::label($content)->addAttributes($labelAttributes);
if (!$this->encode) {
$tag = $tag->encode(false);
}
return $tag->render();
}
| public self setFor ( boolean $value ) | ||
| $value | boolean | |
public function setFor(bool $value): self
{
$new = clone $this;
$new->setFor = $value;
return $new;
}
| public self useInputId ( boolean $value ) | ||
| $value | boolean | |
public function useInputId(bool $value): self
{
$new = clone $this;
$new->useInputId = $value;
return $new;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.