Class kartik\form\ActiveField
Inheritance | kartik\form\ActiveField » yii\widgets\ActiveField » yii\base\Component » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable |
Uses Traits | kartik\base\AddonTrait |
Subclasses | kartik\widgets\ActiveField |
Available since version | 1.0 |
ActiveField represents a form input field within an kartik\form\ActiveForm and extends the yii\widgets\ActiveField component to handle various bootstrap functionality like form types, input groups/addons, toggle buttons, feedback icons, and other enhancements.
For example,
// $form is your active form instance
echo $form->field($model, 'email', ['addon' => ['type'=>'prepend', 'content'=>'@']]);
echo $form->field($model, 'amount_paid', ['addon' => ['type'=>'append', 'content'=>'.00']]);
echo $form->field($model, 'phone', [
'addon' => [
'type'=>'prepend',
'content'=>'<i class="fas fa-mobile-alt"></i>'
]
]);
Usage example with horizontal form and advanced field layout CSS configuration:
echo $form->field($model, 'email', ['labelSpan' => 2, 'deviceSize' => ActiveForm::SIZE_SMALL]]);
echo $form->field($model, 'amount_paid', ['horizontalCssClasses' => ['wrapper' => 'hidden-xs']]);
echo $form->field($model, 'phone', [
'horizontalCssClasses' => ['label' => 'col-md-2 col-sm-3 col-xs-12 myRedClass']
]);
echo $form->field($model, 'special', [
'template' => '{beginLabel}For: {labelTitle}{endLabel}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}'
]);
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$addAriaAttributes | boolean | Adds aria HTML attributes aria-required and aria-invalid for inputs |
yii\widgets\ActiveField |
$addClass | string | CSS classname to add to the input | kartik\form\ActiveField |
$addon | array | Addon to prepend or append to the widget (based on bootstrap input group styling). | kartik\base\AddonTrait |
$attribute | string | The model attribute that this field is associated with. | yii\widgets\ActiveField |
$autoOffset | boolean | Whether to auto offset toggle inputs (checkboxes / radios) horizontal form layout for BS 4. | kartik\form\ActiveField |
$autoPlaceholder | boolean | Whether the label is to be hidden and auto-displayed as a placeholder | kartik\form\ActiveField |
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component. | yii\base\Component |
$checkEnclosedTemplate | string | The template for rendering checkboxes and radios for a default Bootstrap markup with an enclosed label | kartik\form\ActiveField |
$checkTemplate | string | The template for rendering checkboxes and radios for a default Bootstrap markup without an enclosed label | kartik\form\ActiveField |
$checkWrapperOptions | array | The HTML attributes for the container wrapping BS4 checkbox or radio controls within which the content will be rendered via the $checkTemplate or $checkEnclosedTemplate | kartik\form\ActiveField |
$contentAfterError | string | Content to be placed after error block | kartik\form\ActiveField |
$contentAfterField | string | Content to be placed after field within the form group at the end | kartik\form\ActiveField |
$contentAfterHint | string | Content to be placed after hint block | kartik\form\ActiveField |
$contentAfterInput | string | Content to be placed after input | kartik\form\ActiveField |
$contentAfterLabel | string | Content to be placed after label | kartik\form\ActiveField |
$contentBeforeError | string | Content to be placed before error block | kartik\form\ActiveField |
$contentBeforeField | string | Content to be placed before field within the form group at the beginning | kartik\form\ActiveField |
$contentBeforeHint | string | Content to be placed before hint block | kartik\form\ActiveField |
$contentBeforeInput | string | Content to be placed before input | kartik\form\ActiveField |
$contentBeforeLabel | string | Content to be placed before label | kartik\form\ActiveField |
$customFileTemplate | string | The template for rendering the Bootstrap 4. | kartik\form\ActiveField |
$deviceSize | string | One of the bootstrap sizes (refer the ActiveForm::SIZE constants) | kartik\form\ActiveField |
$enableAjaxValidation | boolean|null | Whether to enable AJAX-based data validation. | yii\widgets\ActiveField |
$enableClientValidation | boolean|null | Whether to enable client-side data validation. | yii\widgets\ActiveField |
$enableError | boolean | Whether to render the error. | kartik\form\ActiveField |
$enableLabel | boolean | Whether to render the label. | kartik\form\ActiveField |
$errorOptions | array | The default options for the error tags. | yii\widgets\ActiveField |
$feedbackIcon | array | The feedback icon configuration (applicable for [bootstrap text inputs](http://getbootstrap. | kartik\form\ActiveField |
$form | kartik\form\ActiveForm | kartik\form\ActiveField | |
$highlightAddon | boolean | Whether to highlight error and success states on input group addons automatically | kartik\form\ActiveField |
$hintOptions | array | The default options for the hint tags. | yii\widgets\ActiveField |
$hintSettings | array | The settings for displaying the hint. | kartik\form\ActiveField |
$hintType | integer | The hint display type. | kartik\form\ActiveField |
$horizontalCssClasses | null|array | CSS grid classes for horizontal layout. | kartik\form\ActiveField |
$inputOptions | array | The default options for the input tags. | yii\widgets\ActiveField |
$labelOptions | array | The default options for the label tags. | kartik\form\ActiveField |
$labelSpan | integer | The bootstrap grid column width (usually between 1 to 12) | kartik\form\ActiveField |
$model | yii\base\Model | The data model that this field is associated with. | yii\widgets\ActiveField |
$options | array | The HTML attributes (name-value pairs) for the field container tag. | yii\widgets\ActiveField |
$parts | array | Different parts of the field (e.g. input, label). | yii\widgets\ActiveField |
$renderEmptyWrapper | boolean | Whether to render the wrapper in the template if $wrapperOptions is empty. | kartik\form\ActiveField |
$selectors | array | The jQuery selectors for selecting the container, input and error tags. | yii\widgets\ActiveField |
$showErrors | boolean | Whether to show errors for the field | kartik\form\ActiveField |
$showHints | boolean | Whether to show hints for the field | kartik\form\ActiveField |
$showLabels | boolean|string | Whether to show labels for the field. | kartik\form\ActiveField |
$showRequiredIndicator | boolean | Whether to show required asterisk/star indicator after each field label when the model attribute is
set to have a required validation rule. |
kartik\form\ActiveField |
$skipFormLayout | boolean | Whether to override the form layout styles and skip field formatting as per the form layout. | kartik\form\ActiveField |
$staticValue | string | The static value for the field to be displayed for the static input OR when the form is in staticOnly mode. | kartik\form\ActiveField |
$template | string | Inherits and overrides values from parent class. | kartik\form\ActiveField |
$validateOnBlur | boolean|null | Whether to perform validation when the input field loses focus. | yii\widgets\ActiveField |
$validateOnChange | boolean|null | Whether to perform validation when the value of the input field is changed. | yii\widgets\ActiveField |
$validateOnType | boolean|null | Whether to perform validation while the user is typing in the input field. | yii\widgets\ActiveField |
$validationDelay | integer|null | Number of milliseconds that the validation should be delayed when the user types in the field
and $validateOnType is set true . |
yii\widgets\ActiveField |
$wrapperOptions | array | Options for the wrapper tag, used in the {beginWrapper} token within $template. |
kartik\form\ActiveField |
Protected Properties
Property | Type | Description | Defined By |
---|---|---|---|
$_hasFeedback | boolean | Whether there is a feedback icon configuration set | kartik\form\ActiveField |
$_isHintSpecial | boolean | Whether there is a feedback icon configuration set | kartik\form\ActiveField |
$_isStatic | boolean | Is it a static input | kartik\form\ActiveField |
$_multiselect | boolean | The container for multi select | kartik\form\ActiveField |
$_offset | boolean | Whether the input is to be offset (like for checkbox or radio). | kartik\form\ActiveField |
$_pluginHintKeys | array | The list of hint keys that will be used by ActiveFieldHint jQuery plugin | kartik\form\ActiveField |
$_settings | array | The settings for the active field layout | kartik\form\ActiveField |
Public Methods
Method | Description | Defined By |
---|---|---|
__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 |
__construct() | Constructor. | kartik\form\ActiveField |
__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. | kartik\form\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. This method will generate the "checked" tag attribute according to the model attribute value. | kartik\form\ActiveField |
checkboxButtonGroup() | Renders a list of checkbox toggle buttons. | kartik\form\ActiveField |
checkboxList() | Renders a list of checkboxes. A checkbox list allows multiple selection, like listBox(). As a result, the corresponding submitted value is an array. The selection of the checkbox list is taken from the value of the model attribute. | kartik\form\ActiveField |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
detachBehavior() | Detaches a behavior from the component. | yii\base\Component |
detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component |
dropDownList() | Renders a drop-down list. | kartik\form\ActiveField |
end() | Renders the closing tag of the field container. | kartik\form\ActiveField |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component |
error() | Generates a tag that contains the first validation error of $attribute. | yii\widgets\ActiveField |
fileInput() | Renders a file input. | kartik\form\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 |
hint() | Renders the hint tag. | kartik\form\ActiveField |
init() | Initializes the object. | kartik\form\ActiveField |
input() | Renders an input tag. | kartik\form\ActiveField |
label() | Generates a label tag for $attribute. | kartik\form\ActiveField |
listBox() | Renders a list box. | kartik\form\ActiveField |
multiselect() | Renders a multi select list box. This control extends the checkboxList and radioList available in yii\widgets\ActiveField - to display a scrolling multi select list box. | kartik\form\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() | Renders a password input. | kartik\form\ActiveField |
radio() | Renders a radio button. This method will generate the "checked" tag attribute according to the model attribute value. | kartik\form\ActiveField |
radioButtonGroup() | Renders a list of radio toggle buttons. | kartik\form\ActiveField |
radioList() | Renders a list of radio buttons. A radio button list is like a checkbox list, except that it only allows single selection. The selection of the radio buttons is taken from the value of the model attribute. | kartik\form\ActiveField |
render() | Renders the whole field. | kartik\form\ActiveField |
staticInput() | Renders a static input (display only). | kartik\form\ActiveField |
textInput() | Renders a text input. | kartik\form\ActiveField |
textarea() | Renders a text area. | kartik\form\ActiveField |
trigger() | Triggers an event. | yii\base\Component |
widget() | Renders a widget as the input of the field. | kartik\form\ActiveField |
Protected Methods
Method | Description | Defined By |
---|---|---|
addAriaAttributes() | Adds aria attributes to the input options. | yii\widgets\ActiveField |
addErrorClassBS4() | Adds Bootstrap 4 validation class to the input options if needed. | kartik\form\ActiveField |
addErrorClassIfNeeded() | Adds validation class to the input options if needed. | yii\widgets\ActiveField |
addRoleAttributes() | Add role attributes to the input options | yii\widgets\ActiveField |
adjustLabelFor() | Adjusts the for attribute for the label based on the input options. |
yii\widgets\ActiveField |
buildLayoutParts() | Builds the field layout parts | kartik\form\ActiveField |
buildTemplate() | Builds the final template based on the bootstrap form type, display settings for label, error, and hint, and content before and after label, input, error, and hint. | kartik\form\ActiveField |
createLayoutConfig() | Create layout specific configuration | kartik\form\ActiveField |
generateAddon() | Generates the addon markup | kartik\form\ActiveField |
generateHint() | Generates the hint. | kartik\form\ActiveField |
getAddonContent() | Parses and returns addon content. | kartik\base\AddonTrait |
getClientOptions() | Returns the JS options for the field. | yii\widgets\ActiveField |
getColCss() | Gets bootstrap grid column CSS based on size | kartik\form\ActiveField |
getConfigParam() | Gets configuration parameter from formConfig | kartik\form\ActiveField |
getCustomCss() | Gets custom CSS for custom controls supported in bootstrap 4.x and 5.x | kartik\form\ActiveField |
getFeedbackIcon() | Generates a feedback icon | kartik\form\ActiveField |
getHintData() | Gets a hint configuration setting value | kartik\form\ActiveField |
getHintIcon() | Generates the hint icon | kartik\form\ActiveField |
getHintIconCss() | Gets the hint icon css based on hintSettings |
kartik\form\ActiveField |
getInputId() | Returns the HTML id of the input element of this form field. |
yii\widgets\ActiveField |
getToggleField() | Generates a toggle field (checkbox or radio) | kartik\form\ActiveField |
getToggleFieldList() | Renders a list of checkboxes / radio buttons. The selection of the checkbox / radio buttons is taken from the value of the model attribute. | kartik\form\ActiveField |
hasLabels() | Validate label display status | kartik\form\ActiveField |
initActiveField() | Initialize the active field | kartik\form\ActiveField |
initDisability() | Validates and sets disabled or readonly inputs | kartik\form\ActiveField |
initFieldOptions() | Initializes sizes and placeholder based on $autoPlaceholder | kartik\form\ActiveField |
initFieldSize() | Initializes field by detecting the bootstrap CSS size and sets a size modifier CSS to the field container | kartik\form\ActiveField |
initHints() | Initialize hint settings | kartik\form\ActiveField |
initHorizontal() | Prepares bootstrap grid col classes for horizontal layout including label and input tags and initiate private CSS variables. The process order for 'labelSpan' and 'wrapper' is as follows: | kartik\form\ActiveField |
initLabels() | Initialize label options | kartik\form\ActiveField |
initLayout() | Initialize layout settings for label, input, error and hint blocks and for various bootstrap 3 form layouts | kartik\form\ActiveField |
isAjaxValidationEnabled() | Checks if ajax validation enabled for the field. | yii\widgets\ActiveField |
isClientValidationEnabled() | Checks if client validation enabled for the field. | yii\widgets\ActiveField |
isCustomControl() | Checks whether bootstrap 4.x custom control based on options parameter |
kartik\form\ActiveField |
mergeSettings() | Merges the parameters for layout settings | kartik\form\ActiveField |
renderAddonItem() | Renders an addon item based on its configuration | kartik\base\AddonTrait |
renderFeedbackIcon() | Render the bootstrap feedback icon | kartik\form\ActiveField |
renderLabelParts() | Render the label parts | kartik\form\ActiveField |
setHintData() | Sets a hint property setting as a data attribute within self::$options |
kartik\form\ActiveField |
setLayoutContainer() | Sets the layout element container | kartik\form\ActiveField |
Constants
Constant | Value | Description | Defined By |
---|---|---|---|
HINT_DEFAULT | 1 | kartik\form\ActiveField | |
HINT_SPECIAL | 2 | kartik\form\ActiveField | |
MULTI_SELECT_HEIGHT | '145px' | kartik\form\ActiveField | |
NOT_SET | '' | kartik\form\ActiveField | |
TYPE_CHECKBOX | 'checkbox' | kartik\form\ActiveField | |
TYPE_RADIO | 'radio' | kartik\form\ActiveField |
Property Details
Whether there is a feedback icon configuration set
Whether there is a feedback icon configuration set
Is it a static input
The container for multi select
Whether the input is to be offset (like for checkbox or radio).
The list of hint keys that will be used by ActiveFieldHint jQuery plugin
'iconCssClass', 'labelCssClass', 'contentCssClass', 'hideOnEscape', 'hideOnClickOut', 'title', 'placement', 'container', 'animation', 'delay', 'template', 'selector', 'viewport'
]
The settings for the active field layout
'input' => '{input}', 'error' => '{error}', 'hint' => '{hint}', 'showLabels' => true, 'showErrors' => true, 'labelSpan' => \kartik\form\ActiveForm::DEFAULT_LABEL_SPAN, 'deviceSize' => \kartik\form\ActiveForm::SIZE_MEDIUM
]
CSS classname to add to the input
Whether to auto offset toggle inputs (checkboxes / radios) horizontal form layout for BS 4.x forms.
This will read the labelSpan
and automatically offset the checkboxes/radios.
Whether the label is to be hidden and auto-displayed as a placeholder
The template for rendering checkboxes and radios for a default Bootstrap markup with an enclosed label
The template for rendering checkboxes and radios for a default Bootstrap markup without an enclosed label
The HTML attributes for the container wrapping BS4 checkbox or radio controls within which the content will be rendered via the $checkTemplate or $checkEnclosedTemplate
Content to be placed after error block
Content to be placed after field within the form group at the end
Content to be placed after hint block
Content to be placed after input
Content to be placed after label
Content to be placed before error block
Content to be placed before field within the form group at the beginning
Content to be placed before hint block
Content to be placed before input
Content to be placed before label
The template for rendering the Bootstrap 4.x custom file browser control
See also https://getbootstrap.com/docs/4.1/components/forms/#file-browser.
One of the bootstrap sizes (refer the ActiveForm::SIZE constants)
Whether to render the error. Default is true
except for layout inline
.
Whether to render the label. Default is true
.
The feedback icon configuration (applicable for bootstrap text inputs). This must be setup as an array containing the following keys:
type
: string, the icon type to use. Should be one ofraw
oricon
. Defaults toicon
, where thedefault
,error
andsuccess
settings will be treated as an icon CSS suffix name. If set toraw
, they will be treated as a raw content markup.prefix
: string, the icon CSS class prefix to use iftype
isicon
. Defaults toglyphicon glyphicon-
for Bootstrap 3.x andfas fa-
for Bootstrap 4.x and above.default
: string, the icon (CSS class suffix name or raw markup) to show by default. If not set will not be shown.error
: string, the icon (CSS class suffix name or raw markup) to use when input has an error validation. If not set will not be shown.success
: string, the icon (CSS class suffix name or raw markup) to use when input has a success validation. If not set will not be shown.defaultOptions
: array, the HTML attributes to apply for default icon. The special attributedescription
can be set to describe this feedback as anaria
attribute for accessibility. Defaults to(default)
.errorOptions
: array, the HTML attributes to apply for error icon. The special attributedescription
can be set to describe this feedback as anaria
attribute for accessibility. Defaults to(error)
.successOptions
: array, the HTML attributes to apply for success icon. The special attributedescription
can be set to describe this feedback as anaria
attribute for accessibility. Defaults to(success)
.
Whether to highlight error and success states on input group addons automatically
The settings for displaying the hint. These settings are parsed only if hintType
is set to
self::HINT_SPECIAL
. The following properties are supported:
showIcon
: boolean, whether to display the hint via a help icon indicator. Defaults totrue
.icon
: string, the markup to display the help icon. Defaults to<i class="glyphicon glyphicon-question-sign text-info"></i>
for Bootstrap 3.x.<i class="fas fa-question-circle text-info"></i>
for Bootstrap 4.x and above.
iconBesideInput
: boolean, whether to display the icon beside the input. Defaults tofalse
. The following actions will be taken based on this setting:- if set to
false
the help icon is displayed beside the label and thelabelTemplate
setting is used to render the icon and label markups. - if set to
true
the help icon is displayed beside the input and theinputTemplate
setting is used to render the icon and input markups.
- if set to
labelTemplate
: string, the template to render the help icon and the field label. Defaults to{label}{help}
, where{label}
will be replaced by the ActiveField label content{help}
will be replaced by the help icon indicator markup
inputTemplate
: string, the template to render the help icon and the field input. Defaults to'<div style="width:90%; float:left">{input}</div><div style="padding-top:7px">{help}</div>',
, where{input}
will be replaced by the ActiveField input content{help}
will be replaced by the help icon indicator markup
onLabelClick
: boolean, whether to display the hint on clicking the label. Defaults tofalse
.onLabelHover
: boolean, whether to display the hint on hover of the label. Defaults totrue
.onIconClick
: boolean, whether to display the hint on clicking the icon. Defaults totrue
.onIconHover
: boolean, whether to display the hint on hover of the icon. Defaults tofalse
.iconCssClass
: string, the CSS class appended to thespan
container enclosing the icon.labelCssClass
: string, the CSS class appended to thespan
container enclosing label text within label tag.contentCssClass
: string, the CSS class appended to thespan
container displaying help content within popover.hideOnEscape
: boolean, whether to hide the popover on clicking escape button on the keyboard. Defaults totrue
.hideOnClickOut
: boolean, whether to hide the popover on clicking outside the popover. Defaults totrue
.title
: string, the title heading for the popover dialog. Defaults to empty string, whereby the heading is not displayed.placement
: string, the placement of the help popover on hover or click of the icon or label. Defaults totop
.container
: string, the specific element to which the popover will be appended to. Defaults totable
wheniconBesideInput
istrue
, else defaults toform
animation
: boolean, whether to add a CSS fade transition effect when opening and closing the popover. Defaults totrue
.delay
: _integer_|_array_, the number of milliseconds it will take to open and close the popover. Defaults to
0`.selector
: integer, the specified selector to add the popover to. Defaults to booleanfalse
.viewport
: string|array, the element within which the popover will be bounded to. Defaults to['selector' => 'body', 'padding' => 0]
.
The hint display type. If set to self::HINT_DEFAULT
, the hint will be displayed as a text block below
each input. If set to self::HINT_SPECIAL
, then the hintSettings
will be applied to display the field
hint.
CSS grid classes for horizontal layout. This must be an array with these keys:
offset
: the offset grid class to append to the wrapper if no label is renderedlabel
: the label grid classwrapper
: the wrapper grid classerror
: the error grid classhint
: the hint grid class These options are compatible and similar to yii\bootstrap\ActiveForm and provide a complete flexible container. IflabelSpan
is set in kartik\form\ActiveForm::$formConfig andwrapper
is also set, then both css options are concatenated. Ifwrapper
contains a 'col-' class wrapper, it overrides the tag fromlabelSpan
.
The default options for the label tags. The parameter passed to label() will be merged with this property when rendering the label tag.
The bootstrap grid column width (usually between 1 to 12)
Whether to render the wrapper in the template if $wrapperOptions is empty.
Whether to show errors for the field
Whether to show hints for the field
Whether to show labels for the field. Should be one of the following values:
true
: show labels for the fieldfalse
: hide labels for the fieldActiveForm::SCREEN_READER
: show in screen reader only (hide from normal display)
Whether to show required asterisk/star indicator after each field label when the model attribute is
set to have a required
validation rule. This will add a CSS class has-star
to the label and show the required
asterisk/star after the label based on CSS ::after
styles. If you want any other label markup to show a
required asterisk for a required model attribute field, then just add the CSS class has-star
to the label/span
markup element within the active field container with CSS class form-group
.
Whether to override the form layout styles and skip field formatting as per the form layout.
Defaults to false
.
The static value for the field to be displayed for the static input OR when the form is in staticOnly mode. This value is not HTML encoded.
Inherits and overrides values from parent class. The value can be overridden within kartik\form\ActiveForm::field() method. The following tokens are supported:
{beginLabel}
: Container begin tag for labels (to be used typically along with{labelTitle}
token when you do not wish to directly use the{label}
token){labelTitle}
: Label content without tags (to be used typically when you do not wish to directly use the{label
token){endLabel}
: Container end tag for labels (to be used typically along with{labelTitle}
token when you do not wish to directly use the{label}
token){label}
: Full label tag with begin tag, content and end tag{beginWrapper}
: Container for input,error and hint start tag. Uses a<div>
tag if there is a input wrapper CSS detected, else defaults to empty string.{input}
: placeholder for input control whatever it is{hint}
: placeholder for hint/help text including sub container{error}
: placeholder for error text including sub container{endWrapper}
: end tag for{beginWrapper}
. Defaults to</div>
if there is a input wrapper CSS detected, else defaults to empty string.
Options for the wrapper tag, used in the {beginWrapper}
token within $template.
Method Details
Constructor.
The default implementation does two things:
- Initializes the object with the given configuration
$config
. - Call init().
If this method is overridden in a child class, it is recommended that
- the last parameter of the constructor is a configuration array, like
$config
here. - call the parent implementation at the end of the constructor.
public void __construct ( $config = [] ) | ||
$config | array | Name-value pairs that will be used to initialize the object properties |
Adds Bootstrap 4 validation class to the input options if needed.
protected void addErrorClassBS4 ( &$options ) | ||
$options | array | |
throws | Exception |
---|
Renders the opening tag of the field container.
public string begin ( ) | ||
return | string | The rendering result. |
---|
Builds the field layout parts
protected void buildLayoutParts ( $showLabels, $showErrors ) | ||
$showLabels | boolean | Whether to show labels |
$showErrors | boolean | Whether to show errors |
throws | yii\base\InvalidConfigException |
---|
Builds the final template based on the bootstrap form type, display settings for label, error, and hint, and content before and after label, input, error, and hint.
protected void buildTemplate ( ) | ||
throws | yii\base\InvalidConfigException |
---|
Renders a checkbox. This method will generate the "checked" tag attribute according to the model attribute value.
See also https://getbootstrap.com/docs/4.1/components/forms/#checkboxes-and-radios-1 - uncheck
: string, the value associated with the uncheck state of the checkbox. If not set, it will take
the default value 0
. This method will render a hidden input so that if the checkbox is not checked and is
submitted, the value of this attribute will still be submitted to the server via the hidden input.
- label
: string, a label displayed next to the checkbox. It will NOT be HTML-encoded. Therefore you can
pass in HTML code such as an image tag. If this is is coming from end users, you should yii\helpers\Html::encode()
it to prevent XSS attacks. When this option is specified, the checkbox will be enclosed by a label tag.
- labelOptions
: array, the HTML attributes for the label tag. This is only used when the "label" option is
specified.
- container: boolean|array, the HTML attributes for the checkbox container. If this is set to false, no
container will be rendered. The special option
tag will be recognized which defaults to
div. This
defaults to:
['tag' => 'div', 'class'=>'radio']`
The rest of the options will be rendered as the attributes of the resulting tag. The values will be
HTML-encoded using yii\helpers\Html::encode(). If a value is null, the corresponding attribute will not be rendered.
public kartik\form\ActiveField checkbox ( $options = [], $enclosedByLabel = null ) | ||
$options | array | The tag options in terms of name-value pairs. The following options are specially handled:
|
$enclosedByLabel | boolean|null | Whether to enclose the radio within the label. If |
return | kartik\form\ActiveField | Object |
---|---|---|
throws | yii\base\InvalidConfigException |
Renders a list of checkbox toggle buttons.
See also http://getbootstrap.com/javascript/#buttons-checkbox-radio.
public kartik\form\ActiveField checkboxButtonGroup ( $items, $options = [] ) | ||
$items | array | The data item used to generate the checkboxes. The array values are the labels, while the array keys are the corresponding checkbox values. Note that the labels will NOT be HTML-encoded, while the values will. |
$options | array | Options (name => config) for the checkbox button list. The following options are specially handled:
where $index is the zero-based index of the checkbox button in the whole list; $label is the label for the checkbox button; and $name, $value and $checked represent the name, value and the checked status of the checkbox button input. |
return | kartik\form\ActiveField | Object |
---|---|---|
throws | yii\base\InvalidConfigException |
Renders a list of checkboxes. A checkbox list allows multiple selection, like listBox(). As a result, the corresponding submitted value is an array. The selection of the checkbox list is taken from the value of the model attribute.
See also https://getbootstrap.com/docs/4.1/components/forms/#checkboxes-and-radios-1 - unselect
: string, the value that should be submitted when none of the checkboxes is selected. By setting this
option, a hidden input will be generated.
- separator
: string, the HTML code that separates items.
- inline
: boolean, whether the list should be displayed as a series on the same line, default is false
- item: callable, a callback that can be used to customize the generation of the HTML code corresponding to a
single item in $items. The signature of this callback must be:
~~~
function ($index, $label, $name, $checked, $value)
~~~
where
$index is the zero-based index of the checkbox in the whole list;
$label is the label for the checkbox;
and
$name,
$value and
$checked` represent the name, value and the checked status of the checkbox input.
public kartik\form\ActiveField checkboxList ( $items, $options = [] ) | ||
$items | array | The data item used to generate the checkboxes. The array values are the labels, while the array keys are the corresponding checkbox values. Note that the labels will NOT be HTML-encoded, while the values will be encoded. |
$options | array | Options (name => config) for the checkbox list. The following options are specially handled:
|
return | kartik\form\ActiveField | Object |
---|---|---|
throws | yii\base\InvalidConfigException |
Create layout specific configuration
protected array createLayoutConfig ( $instanceConfig = [] ) | ||
$instanceConfig | array | The configuration passed to this instance's constructor |
return | array | The layout specific default configuration for this instance |
---|
Renders a drop-down list.
The selection of the drop-down list is taken from the value of the model attribute.
public $this dropDownList ( $items, $options = [] ) | ||
$items | array | The option data items. The array keys are option values, and the array values are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). For each sub-array, an option group will be generated whose label is the key associated with the sub-array. If you have a list of data models, you may convert them into the format described above using yii\helpers\ArrayHelper::map(). Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in the labels will also be HTML-encoded. |
$options | array | The tag options in terms of name-value pairs. For the list of available options please refer to the If you set a custom |
return | $this | The field object itself. |
---|---|---|
throws | yii\base\InvalidConfigException |
Renders the closing tag of the field container.
public string end ( ) | ||
return | string | The rendering result. |
---|
Renders a file input.
This method will generate the name
and value
tag attributes automatically for the model attribute
unless they are explicitly specified in $options
.
public $this fileInput ( $options = [] ) | ||
$options | array | The tag options in terms of name-value pairs. These will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using yii\helpers\Html::encode(). If you set a custom |
return | $this | The field object itself. |
---|---|---|
throws | yii\base\InvalidConfigException |
Generates the addon markup
protected string generateAddon ( ) | ||
throws | \yii\base\InvalidConfigException|\Exception |
---|
Generates the hint.
protected string generateHint ( $content = null ) | ||
$content | string | The hint content |
Gets bootstrap grid column CSS based on size
protected string getColCss ( $size ) | ||
$size | string | |
throws | \yii\base\InvalidConfigException|\Exception |
---|
Gets configuration parameter from formConfig
protected string|boolean getConfigParam ( $param, $default = true ) | ||
$param | string | The parameter name |
$default | mixed | The default parameter value |
return | string|boolean | The parsed parameter value |
---|---|---|
throws | Exception |
Gets custom CSS for custom controls supported in bootstrap 4.x and 5.x
protected string getCustomCss ( $type ) | ||
$type | string | |
throws | Exception |
---|
Generates a feedback icon
protected string getFeedbackIcon ( $config, $cat, $type, $prefix, $id ) | ||
$config | array | The feedback icon configuration |
$cat | string | The feedback icon category |
$type | string | The feedback icon type |
$prefix | string | The feedback icon prefix |
$id | string | The input attribute identifier |
throws | Exception |
---|
Gets a hint configuration setting value
protected mixed getHintData ( $key, $default = null ) | ||
$key | string | The hint setting key to fetch |
$default | mixed | The default value if not set |
throws | Exception |
---|
Generates the hint icon
protected string getHintIcon ( ) | ||
throws | Exception |
---|
Gets the hint icon css based on hintSettings
protected array getHintIconCss ( $type ) | ||
$type | string | Whether |
return | array | The css to be applied |
---|
Generates a toggle field (checkbox or radio)
protected kartik\form\ActiveField getToggleField ( $type = self::TYPE_CHECKBOX, $options = [], $enclosedByLabel = null ) | ||
$type | string | The toggle input type 'checkbox' or 'radio'. |
$options | array | Options (name => config) for the toggle input list container tag. |
$enclosedByLabel | boolean|null | Whether the input is enclosed by the label tag |
return | kartik\form\ActiveField | Object |
---|---|---|
throws | yii\base\InvalidConfigException |
Renders a list of checkboxes / radio buttons. The selection of the checkbox / radio buttons is taken from the value of the model attribute.
See also https://getbootstrap.com/docs/4.1/components/forms/#checkboxes-and-radios-1 - unselect
: string, the value that should be submitted when none of the checkbox / radio buttons is selected. By
setting this option, a hidden input will be generated.
- separator
: string, the HTML code that separates items.
- inline
: boolean, whether the list should be displayed as a series on the same line, default is false
- disabledItems
: array, the list of values that will be disabled.
- readonlyItems
: array, the list of values that will be readonly.
- `item: callable, a callback that can be used to customize the generation of the HTML code corresponding to a
single item in $items. The signature of this callback must be:
~~~
function ($index, $label, $name, $checked, $value)
~~~
where $index is the zero-based index of the checkbox/ radio button in the whole list; $label is the label for
the checkbox/ radio button; and $name, $value and $checked represent the name, value and the checked status
of the checkbox/ radio button input.
protected kartik\form\ActiveField getToggleFieldList ( $type, $items, $options = [], $asBtnGrp = false ) | ||
$type | string | The toggle input type 'checkbox' or 'radio'. |
$items | array | The data item used to generate the checkbox / radio buttons. The array keys are the labels, while the array values are the corresponding checkbox / radio button values. Note that the labels will NOT be HTML-encoded, while the values will be encoded. |
$options | array | Options (name => config) for the checkbox / radio button list. The following options are specially handled:
|
$asBtnGrp | boolean | Whether to generate the toggle list as a bootstrap button group |
return | kartik\form\ActiveField | Object |
---|---|---|
throws | yii\base\InvalidConfigException |
Validate label display status
protected boolean|string hasLabels ( ) | ||
return | boolean|string | Whether labels are to be shown |
---|---|---|
throws | Exception |
Renders the hint tag.
public $this hint ( $content, $options = [] ) | ||
$content | string|boolean|null | The hint content.
If |
$options | array | The tag options in terms of name-value pairs. These will be rendered as the attributes of the hint tag. The values will be HTML-encoded using yii\helpers\Html::encode(). The following options are specially handled:
|
return | $this | The field object itself. |
---|
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
public void init ( ) | ||
throws | yii\base\InvalidConfigException |
---|
Initialize the active field
protected void initActiveField ( ) | ||
throws | yii\base\InvalidConfigException |
---|
Validates and sets disabled or readonly inputs
protected void initDisability ( &$options ) | ||
$options | array | The HTML attributes for the input |
Initializes sizes and placeholder based on $autoPlaceholder
protected void initFieldOptions ( &$options ) | ||
$options | array | The HTML attributes for the input |
throws | yii\base\InvalidConfigException |
---|
Initializes field by detecting the bootstrap CSS size and sets a size modifier CSS to the field container
protected void initFieldSize ( $options, $size ) | ||
$options | array | The HTML options |
$size | string | The size to init |
throws | \yii\base\InvalidConfigException|\Exception |
---|
Initialize hint settings
protected void initHints ( ) | ||
throws | \yii\base\InvalidConfigException|\Exception |
---|
Prepares bootstrap grid col classes for horizontal layout including label and input tags and initiate private CSS variables. The process order for 'labelSpan' and 'wrapper' is as follows:
- Step 1: Check
$labelSpan
and$deviceSize
. - Step 2: Check `formConfig(['labelSpan' => x, 'deviceSize' => xy]) and build css tag.
- If
horizontalCssClasses['wrapper']
is set and no 'col-' tag then add this to css tag from Step 1. - If
horizontalCssClasses['wrapper']
is set and wrapper has 'col-' tag then override css tag completely. - If no
$labelSpan
and nohorizontalCssClasses['wrapper']
is set then use default from $_settings. Similar behavior tohorizontalCssClasses['label']
.
protected void initHorizontal ( ) | ||
throws | yii\base\InvalidConfigException |
---|
Initialize label options
protected void initLabels ( ) |
Initialize layout settings for label, input, error and hint blocks and for various bootstrap 3 form layouts
protected void initLayout ( ) | ||
throws | yii\base\InvalidConfigException |
---|
Renders an input tag.
public $this input ( $type, $options = [] ) | ||
$type | string | The input type (e.g. |
$options | array | The tag options in terms of name-value pairs. These will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using yii\helpers\Html::encode(). If you set a custom |
return | $this | The field object itself. |
---|---|---|
throws | yii\base\InvalidConfigException |
Checks whether bootstrap 4.x custom control based on options
parameter
protected boolean isCustomControl ( &$options ) | ||
$options | array | HTML attributes for the control |
throws | \yii\base\InvalidConfigException|\Exception |
---|
Generates a label tag for $attribute.
public $this label ( $label = null, $options = [] ) | ||
$label | string|null|false | The label to use. If |
$options | array|null | The tag options in terms of name-value pairs. It will be merged with $labelOptions.
The options will be rendered as the attributes of the resulting tag. The values will be HTML-encoded
using yii\helpers\Html::encode(). If a value is |
return | $this | The field object itself. |
---|
Renders a list box.
The selection of the list box is taken from the value of the model attribute.
public $this listBox ( $items, $options = [] ) | ||
$items | array | The option data items. The array keys are option values, and the array values are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). For each sub-array, an option group will be generated whose label is the key associated with the sub-array. If you have a list of data models, you may convert them into the format described above using yii\helpers\ArrayHelper::map(). Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in the labels will also be HTML-encoded. |
$options | array | The tag options in terms of name-value pairs. For the list of available options please refer to the If you set a custom |
return | $this | The field object itself. |
---|---|---|
throws | yii\base\InvalidConfigException |
Merges the parameters for layout settings
protected void mergeSettings ( $showLabels, $showErrors ) | ||
$showLabels | boolean | Whether to show labels |
$showErrors | boolean | Whether to show errors |
Renders a multi select list box. This control extends the checkboxList and radioList available in yii\widgets\ActiveField - to display a scrolling multi select list box.
public kartik\form\ActiveField multiselect ( $items, $options = [] ) | ||
$items | array | The data item used to generate the checkboxes or radio. |
$options | array | The options for checkboxList or radioList. Additional parameters
|
return | kartik\form\ActiveField | Object |
---|---|---|
throws | yii\base\InvalidConfigException |
Renders a password input.
This method will generate the name
and value
tag attributes automatically for the model attribute
unless they are explicitly specified in $options
.
public $this passwordInput ( $options = [] ) | ||
$options | array | The tag options in terms of name-value pairs. These will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using yii\helpers\Html::encode(). If you set a custom |
return | $this | The field object itself. |
---|---|---|
throws | yii\base\InvalidConfigException |
Renders a radio button. This method will generate the "checked" tag attribute according to the model attribute value.
See also https://getbootstrap.com/docs/4.1/components/forms/#checkboxes-and-radios-1 - uncheck
: string, the value associated with the uncheck state of the radio button. If not set, it will take the
default value '0'. This method will render a hidden input so that if the radio button is not checked and is
submitted, the value of this attribute will still be submitted to the server via the hidden input.
- label
: string, a label displayed next to the radio button. It will NOT be HTML-encoded. Therefore you can pass
in HTML code such as an image tag. If this is is coming from end users, you should yii\helpers\Html::encode() it to
prevent XSS attacks. When this option is specified, the radio button will be enclosed by a label tag.
- labelOptions
: array, the HTML attributes for the label tag. This is only used when the "label" option is
specified.
- container: boolean|array, the HTML attributes for the checkbox container. If this is set to false, no
container will be rendered. The special option
tag will be recognized which defaults to
div. This
defaults to:
['tag' => 'div', 'class'=>'radio']`
The rest of the options will be rendered as the attributes of the resulting tag. The values will be HTML-encoded
using yii\helpers\Html::encode(). If a value is null, the corresponding attribute will not be rendered.
public kartik\form\ActiveField radio ( $options = [], $enclosedByLabel = null ) | ||
$options | array | The tag options in terms of name-value pairs. The following options are specially handled:
|
$enclosedByLabel | boolean|null | Whether to enclose the radio within the label. If |
return | kartik\form\ActiveField | Object |
---|---|---|
throws | yii\base\InvalidConfigException |
Renders a list of radio toggle buttons.
See also http://getbootstrap.com/javascript/#buttons-checkbox-radio.
public kartik\form\ActiveField radioButtonGroup ( $items, $options = [] ) | ||
$items | array | The data item used to generate the radios. The array values are the labels, while the array keys are the corresponding radio values. Note that the labels will NOT be HTML-encoded, while the values will be encoded. |
$options | array | Options (name => config) for the radio button list. The following options are specially handled:
where $index is the zero-based index of the radio button in the whole list; $label is the label for the radio button; and $name, $value and $checked represent the name, value and the checked status of the radio button input. |
return | kartik\form\ActiveField | Object |
---|---|---|
throws | yii\base\InvalidConfigException |
Renders a list of radio buttons. A radio button list is like a checkbox list, except that it only allows single selection. The selection of the radio buttons is taken from the value of the model attribute.
See also https://getbootstrap.com/docs/4.1/components/forms/#checkboxes-and-radios-1 - unselect
: string, the value that should be submitted when none of the radio buttons is selected. By setting
this option, a hidden input will be generated.
- separator
: string, the HTML code that separates items.
- inline
: boolean, whether the list should be displayed as a series on the same line, default is false
- item: callable, a callback that can be used to customize the generation of the HTML code corresponding to a
single item in $items. The signature of this callback must be:
~~~
function ($index, $label, $name, $checked, $value)
~~~
where
$index is the zero-based index of the radio button in the whole list;
$label is the label for the radio
button; and
$name,
$value and
$checked` represent the name, value and the checked status of the radio button
input.
public kartik\form\ActiveField radioList ( $items, $options = [] ) | ||
$items | array | The data item used to generate the radio buttons. The array keys are the labels, while the array values are the corresponding radio button values. Note that the labels will NOT be HTML-encoded, while the values will. |
$options | array | Options (name => config) for the radio button list. The following options are specially handled:
|
return | kartik\form\ActiveField | Object |
---|---|---|
throws | yii\base\InvalidConfigException |
Renders the whole field.
This method will generate the label, error tag, input tag and hint tag (if any), and assemble them into HTML according to $template.
public string render ( $content = null ) | ||
$content | string|callable|null | The content within the field container.
If
|
return | string | The rendering result. |
---|---|---|
throws | yii\base\InvalidConfigException |
Render the bootstrap feedback icon
protected string renderFeedbackIcon ( ) | ||
throws | Exception |
---|
Render the label parts
protected void renderLabelParts ( $label = null, $options = [] ) | ||
$label | string|null | The label or null to use model label |
$options | array | The tag options |
Sets a hint property setting as a data attribute within self::$options
protected void setHintData ( $key ) | ||
$key | string | The hint property key |
Sets the layout element container
protected void setLayoutContainer ( $type, $css = '', $chk = true ) | ||
$type | string | The layout element type |
$css | string | The css class for the container |
$chk | boolean | Whether to create the container for the layout element |
Renders a static input (display only).
public kartik\form\ActiveField staticInput ( $options = [] ) | ||
$options | array | The tag options in terms of name-value pairs. |
return | kartik\form\ActiveField | Object |
---|---|---|
throws | Exception |
Renders a text input.
This method will generate the name
and value
tag attributes automatically for the model attribute
unless they are explicitly specified in $options
.
public $this textInput ( $options = [] ) | ||
$options | array | The tag options in terms of name-value pairs. These will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using yii\helpers\Html::encode(). The following special options are recognized:
Note that if you set a custom |
return | $this | The field object itself. |
---|---|---|
throws | yii\base\InvalidConfigException |
Renders a text area.
The model attribute value will be used as the content in the textarea.
public $this textarea ( $options = [] ) | ||
$options | array | The tag options in terms of name-value pairs. These will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using yii\helpers\Html::encode(). If you set a custom |
return | $this | The field object itself. |
---|---|---|
throws | yii\base\InvalidConfigException |
Renders a widget as the input of the field.
Note that the widget must have both model
and attribute
properties. They will
be initialized with $model and $attribute of this field, respectively.
If you want to use a widget that does not have model
and attribute
properties,
please use render() instead.
While widgets extending from kartik\base\Widget work with active field, it is preferred to use kartik\base\InputWidget as a base class.
For example to use the yii\widgets\MaskedInput widget to get some date input, you can use
the following code, assuming that $form
is your kartik\form\ActiveForm instance:
$form->field($model, 'date')->widget(\yii\widgets\MaskedInput::class, [
'mask' => '99/99/9999',
]);
If you set a custom id
for the input element, you may need to adjust the $selectors accordingly.
public $this widget ( $class, $config = [] ) | ||
$class | string | The widget class name. |
$config | array | Name-value pairs that will be used to initialize the widget. |
return | $this | The field object itself. |
---|---|---|
throws | Exception |