Skip to content
PHP Ecosystem
Explore

ActiveField

API reference › Bootstrap 6 › ActiveField

classyii2\extensions\bootstrap6\ActiveField
Extendsyii\widgets\ActiveField » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Sourcesrc/ActiveField.php

Renders a Bootstrap 6 form field for the layouts of ActiveForm.

Markup per layout ({error} is the .invalid-feedback element, {hint} the .form-text element):

  • default: <div class="form-field mb-3"> + label, control, hint, and error stacked by the .form-field grid.
  • horizontal: <div class="row mb-3"> + <label class="sm:col-2 col-form-label"> + <div class="sm:col-10"> wrapping the control, hint, and error. Checkboxes and radios render <div class="sm:col-10 sm:offset-2"> + <div class="form-field">. Grid classes come from $horizontalCssClasses.
  • inline: the form is a row lg:row-cols-auto g-3 align-items-center grid and each field is <div class="col-12"> with a visually-hidden label, a placeholder, and no error. Checkboxes and radios render a nested <div class="form-field">.
  • floating: <div class="form-field mb-3"> + <div class="form-floating"> holding the label BEFORE the control, then hint and error outside the wrapper, as in the Bootstrap validation docs. The feedback is a sibling of the wrapper, not of the control, so the wrapper carries the error CSS class on the server when the attribute has errors. On the client, a :has() rule registered once per form displays the sibling feedback as soon as the control (or the container in ActiveForm::VALIDATION_STATE_ON_CONTAINER mode) carries the error CSS class, so any client validation layer works without jQuery. The feedback is not placed inside .form-floating because the absolutely positioned label spans the whole wrapper height, so the feedback would push the floated label over the control value.

Checkboxes, radios, and switches are rendered by Html (.check, .radio, .switch) with a plain label; the .form-field grid lays them out in two columns. Lists render one .form-field per item and the error after the list container, which receives the error CSS class. Size controls with form-control-sm or form-control-lg through $inputOptions or the per-call class option; dropDownList(), listBox(), and fileInput() keep form-control when a per-call class is given.

Besides the core placeholders, $template accepts {beginLabel}, {labelTitle}, {endLabel} (label parts), and {beginWrapper}, {endWrapper} (tags built from $wrapperOptions).

See also: https://getbootstrap.com/docs/6.0/forms/field/, https://getbootstrap.com/docs/6.0/forms/layout/, https://getbootstrap.com/docs/6.0/forms/floating-labels/.

PropertyTypeDescriptionDefined by
$addAriaAttributesboolAdds aria HTML attributes aria-required and aria-invalid for inputs Default: true. Available since 2.0.11.yii\widgets\ActiveField
$attributestringThe model attribute that this field is associated with. Default: null.yii\widgets\ActiveField
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$checkEnclosedTemplatestringTemplate for checkboxes and radios enclosed by their label. Default: "{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n{hint}\n{error}".ActiveField
$checkHorizontalTemplatestringTemplate for checkboxes and radios in the horizontal layout. Default: "{beginWrapper}\n<div class=\"form-field\">\n{input}\n{label}\n{hint}\n" . "{error}\n</div>\n{endWrapper}".ActiveField
$checkOptionsmixed[]Default options of checkbox() and of the checkboxList() items. Default: [].ActiveField
$checkTemplatestringTemplate for checkboxes and radios; the inline layout wraps it in <div class="form-field">. Default: "{input}\n{label}\n{hint}\n{error}".ActiveField
$enableAjaxValidationbool|nullWhether to enable AJAX-based data validation. Default: null.yii\widgets\ActiveField
$enableClientValidationbool|nullWhether to enable client-side data validation. Default: null.yii\widgets\ActiveField
$enableErrorboolWhether to render the error element; false by default in the inline layout. Default: true.ActiveField
$enableLabelboolWhether to render the label. Default: true.ActiveField
$errorOptionsmixed[]Default: ['class' => 'invalid-feedback'].ActiveField
$formyii\widgets\ActiveFormThe form that this field is associated with. Default: null.yii\widgets\ActiveField
$hintOptionsmixed[]Default: ['class' => 'form-text'].ActiveField
$horizontalCssClassesmixed[]Grid classes of the horizontal layout, keyed by error, field, hint, label, offset (wrapper of fields without label), and wrapper. Default: [].ActiveField
$inlineboolWhether checkboxList() and radioList() lay out their items horizontally. Default: false.ActiveField
$inputOptionsmixed[]Default: ['class' => 'form-control'].ActiveField
$inputTemplatestring|nullTemplate wrapping the rendered control, for example an input group: <div class="input-group">{input}</div>. Default: null.ActiveField
$labelOptionsmixed[]Default: ['class' => 'form-label'].ActiveField
$modelyii\base\ModelThe data model that this field is associated with. Default: null.yii\widgets\ActiveField
$optionsmixed[]Default: ['class' => 'form-field mb-3'].ActiveField
$partsarrayDifferent parts of the field (e.g. input, label). Default: [].yii\widgets\ActiveField
$radioOptionsmixed[]Default options of radio() and of the radioList() items. Default: [].ActiveField
$selectorsarrayThe jQuery selectors for selecting the container, input and error tags. Default: [].yii\widgets\ActiveField
$templatestringThe template that is used to arrange the label, the input field, the error message and the hint text. Default: "{label}\n{input}\n{hint}\n{error}".yii\widgets\ActiveField
$validateOnBlurbool|nullWhether to perform validation when the input field loses focus. Default: null.yii\widgets\ActiveField
$validateOnChangebool|nullWhether to perform validation when the value of the input field is changed. Default: null.yii\widgets\ActiveField
$validateOnTypebool|nullWhether to perform validation while the user is typing in the input field. Default: null.yii\widgets\ActiveField
$validationDelayint|nullNumber of milliseconds that the validation should be delayed when the user types in the field and $validateOnType is set true. Default: null.yii\widgets\ActiveField
$wrapperOptionsmixed[]HTML attributes of the {beginWrapper} tag; the tag option sets the tag name (default div). Default: [].ActiveField
MethodDescriptionDefined by
__construct()ActiveField
__call()Calls the named method which is not a class method.yii\base\Component
__clone()This method is called after the object is created by cloning an existing one.yii\base\Component
__get()Returns the value of a component property.yii\base\Component
__isset()Checks if a property is set, i.e. defined and not null.yii\base\Component
__set()Sets the value of a component property.yii\base\Component
__toString()PHP magic method that returns the string representation of this object.yii\widgets\ActiveField
__unset()Sets a component property to be null.yii\base\Component
attachBehavior()Attaches a behavior to this component.yii\base\Component
attachBehaviors()Attaches a list of behaviors to the component.yii\base\Component
begin()Renders the opening tag of the field container.yii\widgets\ActiveField
behaviors()Returns a list of behaviors that this component should behave as.yii\base\Component
canGetProperty()Returns a value indicating whether a property can be read.yii\base\Component
canSetProperty()Returns a value indicating whether a property can be set.yii\base\Component
checkbox()Renders a checkbox, or a switch with the switch option.ActiveField
checkboxList()Renders a list of checkboxes, see Html::checkboxList().ActiveField
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
colorInput()Renders a color picker with the form-control form-control-color classes.ActiveField
detachBehavior()Detaches a behavior from the component.yii\base\Component
detachBehaviors()Detaches all behaviors from the component.yii\base\Component
dropDownList()ActiveField
end()Renders the closing tag of the field container.yii\widgets\ActiveField
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.yii\base\Component
error()ActiveField
fileInput()Merges $inputOptions before the core method, which skips them on Yii 2.0.x when they equal the core default ['class' => 'form-control'].ActiveField
getBehavior()Returns the named behavior object.yii\base\Component
getBehaviors()Returns all behaviors attached to this component.yii\base\Component
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.yii\base\Component
hasMethod()Returns a value indicating whether a method is defined.yii\base\Component
hasProperty()Returns a value indicating whether a property is defined for this component.yii\base\Component
hiddenInput()Renders a hidden input.yii\widgets\ActiveField
hint()ActiveField
init()Initializes the object.yii\base\BaseObject
inline()Sets whether checkboxList() and radioList() lay out their items horizontally.ActiveField
input()ActiveField
label()Renders the label, or enables or disables it with a bool argument.ActiveField
listBox()ActiveField
off()Detaches an existing event handler from this component.yii\base\Component
on()Attaches an event handler to an event.yii\base\Component
passwordInput()ActiveField
radio()Renders a radio.ActiveField
radioList()Renders a list of radios, see Html::radioList().ActiveField
rangeInput()Renders a range input wrapped in the .form-range element and registers the plugin bundle that initializes it.ActiveField
render()ActiveField
staticControl()Renders a read-only plain-text control with the attribute value.ActiveField
textarea()ActiveField
textInput()ActiveField
trigger()Triggers an event.yii\base\Component
widget()ActiveField
addAriaAttributes() protectedAdds aria attributes to the input options.yii\widgets\ActiveField
addErrorClassIfNeeded() protectedAdds validation class to the input options if needed.yii\widgets\ActiveField
addRoleAttributes() protectedAdd role attributes to the input optionsyii\widgets\ActiveField
adjustLabelFor() protectedAdjusts the for attribute for the label based on the input options.yii\widgets\ActiveField
createLayoutConfig() protectedReturns the layout-specific default configuration.ActiveField
generateLabel() protectedGenerates the label element based on labelOptions configuration when enclosedByLabel is false.yii\widgets\ActiveField
getClientOptions() protectedReturns the JS options for the field.yii\widgets\ActiveField
getInputId() protectedReturns the HTML id of the input element of this form field.yii\widgets\ActiveField
horizontalCssClass() protectedReturns a grid class of $horizontalCssClasses, or an empty array when it is unset or empty.ActiveField
isAjaxValidationEnabled() protectedChecks if ajax validation enabled for the field.yii\widgets\ActiveField
isClientValidationEnabled() protectedChecks if client validation enabled for the field.yii\widgets\ActiveField
layout() protectedReturns the layout of the owning form, or ActiveForm::LAYOUT_DEFAULT for a non-Bootstrap form.ActiveField
registerFloatingFeedbackStyle() protectedRegisters, once per form, the style rule that displays the feedback following a .form-floating wrapper when its control or its container carries the error CSS class. Whitespace-separated classes in ActiveForm::$errorCssClass are matched together; no rule is registered when it holds no class.ActiveField
renderBooleanInput() protectedRenders a checkbox or radio with the layout-specific template and label.ActiveField
renderLabelParts() protectedRenders the {beginLabel}, {labelTitle}, and {endLabel} parts.ActiveField
renderListInput() protectedRenders a checkbox or radio list with the list label pointing to no control.ActiveField
renderWrapperParts() protectedBuilds the {beginWrapper} and {endWrapper} parts unless they are already set.ActiveField
public function __construct(array<string, mixed> $config = [])

Parameters

ParameterTypeDescription
$configarray<string, mixed>Configuration merged over the defaults of createLayoutConfig().
public function checkbox(mixed[] $options = [], bool $enclosedByLabel = false): static

Renders a checkbox, or a switch with the switch option.

Besides the options of Html::checkbox(), the following options are handled: label (false hides it), labelOptions, template, and wrapperOptions.

Parameters

ParameterTypeDescription
$optionsmixed[]Input attributes merged over $checkOptions.
$enclosedByLabelboolWhether to render the input inside the label ($checkEnclosedTemplate).

Returns

static

See also

public function checkboxList(mixed[] $items, mixed[] $options = []): static

Renders a list of checkboxes, see Html::checkboxList().

Parameters

ParameterTypeDescription
$itemsmixed[]Data items, value => label.
$optionsmixed[]List options; inline defaults to $inline.

Returns

static

public function colorInput(mixed[] $options = []): static

Renders a color picker with the form-control form-control-color classes.

Parameters

ParameterTypeDescription
$optionsmixed[]Input attributes.

Returns

static

See also

public function dropDownList(mixed[] $items, mixed[] $options = []): static

Parameters

ParameterTypeDescription
$itemsmixed[]Option data items.
$optionsmixed[]Tag attributes; a class keeps form-control, and the placeholder of $inputOptions is not inherited.

Returns

static

public function error(mixed[]|false $options = []): static

Parameters

ParameterTypeDescription
$optionsmixed[]|falseTag attributes, or false to omit the error element.

Returns

static

public function fileInput(mixed[] $options = []): static

Merges $inputOptions before the core method, which skips them on Yii 2.0.x when they equal the core default ['class' => 'form-control'].

Parameters

ParameterTypeDescription
$optionsmixed[]Tag attributes; a class keeps form-control.

Returns

static

public function hint(bool|string|null $content, mixed[] $options = []): static

Parameters

ParameterTypeDescription
$contentbool|string|nullHint content, null for the model hint, or false to omit it.
$optionsmixed[]Tag attributes.

Returns

static

public function inline(bool $value = true): static

Sets whether checkboxList() and radioList() lay out their items horizontally.

Call it before the list method.

Parameters

ParameterTypeDescription
$valuebool

Returns

static

public function input(string $type, mixed[] $options = []): static

Parameters

ParameterTypeDescription
$typestringInput type.
$optionsmixed[]Tag attributes merged over $inputOptions.

Returns

static

public function label(bool|string|null $label = null, mixed[]|null $options = []): static

Renders the label, or enables or disables it with a bool argument.

Disabling the label in the horizontal layout adds the offset grid class to the wrapper.

Parameters

ParameterTypeDescription
$labelbool|string|nullLabel content (not encoded), null for the model label, or a bool.
$optionsmixed[]|nullTag attributes merged over $labelOptions.

Returns

static

public function listBox(mixed[] $items, mixed[] $options = []): static

Parameters

ParameterTypeDescription
$itemsmixed[]Option data items.
$optionsmixed[]Tag attributes; a class keeps form-control, and the placeholder of $inputOptions is not inherited.

Returns

static

public function passwordInput(mixed[] $options = []): static

Parameters

ParameterTypeDescription
$optionsmixed[]Tag attributes merged over $inputOptions.

Returns

static

public function radio(mixed[] $options = [], bool $enclosedByLabel = false): static

Renders a radio.

Accepts the same options as checkbox() except switch.

Parameters

ParameterTypeDescription
$optionsmixed[]Input attributes merged over $radioOptions.
$enclosedByLabelboolWhether to render the input inside the label ($checkEnclosedTemplate).

Returns

static

See also

public function radioList(mixed[] $items, mixed[] $options = []): static

Renders a list of radios, see Html::radioList().

Parameters

ParameterTypeDescription
$itemsmixed[]Data items, value => label.
$optionsmixed[]List options; inline defaults to $inline.

Returns

static

public function rangeInput(mixed[] $options = []): static

Renders a range input wrapped in the .form-range element and registers the plugin bundle that initializes it.

The rangeOptions option holds the HTML attributes of the wrapper, for example data-bs-bubble or a theme class.

Parameters

ParameterTypeDescription
$optionsmixed[]Input attributes, for example min, max, step, or list.

Returns

static

See also

public function render(callable(): mixed|string|null $content = null): string

Parameters

ParameterTypeDescription
$contentcallable(): mixed|string|nullField content, or null to render the parts with $template.

Returns

string

public function staticControl(mixed[] $options = []): static

Renders a read-only plain-text control with the attribute value.

Parameters

ParameterTypeDescription
$optionsmixed[]Tag attributes; the value option replaces the attribute value.

Returns

static

See also

public function textarea(mixed[] $options = []): static

Parameters

ParameterTypeDescription
$optionsmixed[]Tag attributes merged over $inputOptions.

Returns

static

public function textInput(mixed[] $options = []): static

Parameters

ParameterTypeDescription
$optionsmixed[]Tag attributes merged over $inputOptions.

Returns

static

public function widget(string $class, mixed[] $config = []): static

Parameters

ParameterTypeDescription
$classstringWidget class name.
$configmixed[]Widget configuration.

Returns

static

protected function createLayoutConfig(mixed[] $instanceConfig): array<string, mixed>

Returns the layout-specific default configuration.

Defaults are taken from the current property values, so subclasses can redefine them.

Parameters

ParameterTypeDescription
$instanceConfigmixed[]Configuration passed to the constructor.

Returns

array<string, mixed>: Configuration merged under the constructor configuration.

protected function horizontalCssClass(string $key): mixed[]|string

Returns a grid class of $horizontalCssClasses, or an empty array when it is unset or empty.

Parameters

ParameterTypeDescription
$keystring

Returns

mixed[]|string

protected function layout(): string

Returns the layout of the owning form, or ActiveForm::LAYOUT_DEFAULT for a non-Bootstrap form.

Returns

string

protected function registerFloatingFeedbackStyle(): void

Registers, once per form, the style rule that displays the feedback following a .form-floating wrapper when its control or its container carries the error CSS class. Whitespace-separated classes in ActiveForm::$errorCssClass are matched together; no rule is registered when it holds no class.

The form id and each class are escaped through Html::escapeCssIdentifier(); the registration key keeps the raw id.

protected function renderBooleanInput(string $type, mixed[] $options, bool $enclosedByLabel): void

Renders a checkbox or radio with the layout-specific template and label.

Parameters

ParameterTypeDescription
$typestringInput type, either checkbox or radio.
$optionsmixed[]Input attributes and the special options of checkbox().
$enclosedByLabelboolWhether to render the input inside the label.
protected function renderLabelParts(string|null $label = null, mixed[] $options = []): void

Renders the {beginLabel}, {labelTitle}, and {endLabel} parts.

Parameters

ParameterTypeDescription
$labelstring|nullLabel content, or null for the label option or the encoded model label.
$optionsmixed[]Tag attributes merged over $labelOptions.
protected function renderListInput(string $type, mixed[] $items, mixed[] $options): void

Renders a checkbox or radio list with the list label pointing to no control.

In the floating layout the list cannot float, so it uses the stacked template and a .form-label label.

Parameters

ParameterTypeDescription
$typestringInput type, either checkbox or radio.
$itemsmixed[]Data items, value => label.
$optionsmixed[]List options.
protected function renderWrapperParts(): void

Builds the {beginWrapper} and {endWrapper} parts unless they are already set.

In the floating layout the wrapper receives the error CSS class, so the sibling feedback is displayed.