yii\helpers
or any widget class for rendering the from and to fields.\kartik\form\ActiveField
that allows you to add custom addons to prepend and append to your inputs.\kartik\widgets
. Special enhanced support for
\kartik\widgets\DatePicker
to render date ranges.
The FieldRange validation routine displays only the first error encountered in validation of either of the attributes. The extension tries its best to trap yii ActiveForm client events to display client validation errors. There may be some scenarios where this could be improved, if there is an ability to trap events client validation errors better from the core framework. Having said this, it is to be noted, that the validation at form submission works more perfectly.
For setting up your model validation rules to use with this extension, refer this wiki.
You can use this extension with Bootstrap 5.x / 4.x / 3.x horizontal form layouts. The widget
by default supports the horizontal form layout setting of \kartik\form\ActiveForm
.
Refer this wiki for
using different form layouts.
Not seeing the updated content on this page! Hard refresh your browser to clean cache for this page (e.g. SHIFT-F5 on Windows Chrome)
FieldRange
supports configuration of the bootstrap library version so that you can use this either with any Bootstrap version 3.x and above. For setting up the bootstrap version for your extension, you can configure the FieldRange::bsVersion
property to one of the following.
To use with bootstrap 3 library - you can set FieldRange::bsVersion
property to any string starting with 3 (e.g. 3
or 3.3.7
or 3.x
)
To use with bootstrap 4 library - you can set FieldRange::bsVersion
property to any string starting with 4 (e.g. 4
or 4.6.0
or 4.x
)
To use with bootstrap 5 library - you can set FieldRange::bsVersion
property to any string starting with 5 (e.g. 5
or 5.1.0
or 5.x
)
Generally, you may also want to set a default version globally for all Krajee Extensions (instead of setting it for each widget or extension separately). In order to do this, you can setup the bsVersion
property within Yii 2 application params (i.e. Yii::$app->params['bsVersion']
). To set this up, add this section of code to your application params configuration file (e.g. config/params.php
):
'params' => [ 'bsVersion' => '5.x', // this will set globally `bsVersion` to Bootstrap 5.x for all Krajee Extensions // other settings // 'adminEmail' => 'admin@example.com' ]
If FieldRange::bsVersion
property is set, in addition to Yii::$app->params['bsVersion']
, the extension level setting (FieldRange::bsVersion
property) will override the Yii::$app->params['bsVersion']
. If FieldRange::bsVersion
property is not set, and Yii::$app->params['bsVersion']
is also not set, FieldRange::bsVersion
property will default to 3.x
(i.e. Bootstrap 3.x version will be assumed as default).
You need to install one of yiisoft/yii2-bootstrap
or yiisoft/yii2-bootstrap4
or yiisoft/yii2-bootstrap5
extensions manually in your application to enable Bootstrap 3.x or 4.x or 5.x functionality respectively. This dependency has not been pre-built into the composer configuration for Krajee extensions, to allow better control to the developers in configuring their bootstrap library version. If bsVersion
is set to 5.x
and yiisoft/yii2-bootstrap5
is not installed, then an exception message will be thrown mentioning you to install the yiisoft/yii2-bootstrap5
extension. If bsVersion
is set to 4.x
and yiisoft/yii2-bootstrap4
is not installed, then an exception message will be thrown mentioning you to install the yiisoft/yii2-bootstrap4
extension. Similarly, if bsVersion
is set to 3.x
and yiisoft/yii2-bootstrap
is not installed, an exception message will be thrown mentioning you to install the yiisoft/yii2-bootstrap
extension.
To install yiisoft/yii2-bootstrap5
, add the repo to the require
section of your application's composer.json.
"yiisoft/yii2-bootstrap5": "@dev"
To install yiisoft/yii2-bootstrap4
, add the repo to the require
section of your application's composer.json.
"yiisoft/yii2-bootstrap4": "@dev"
To install yiisoft/yii2-bootstrap
, add the repo to the require
section of your application's composer.json.
"yiisoft/yii2-bootstrap": "@dev"
The Krajee extension asset bundle(s) by default depend on one of the following asset bundles to load the Bootstrap CSS and JS:
yii\bootstrap\BootstrapAsset
and/or yii\bootstrap\BootstrapPluginAsset
for bootstrap 3.x (bsVersion = 3
setting)
yii\bootstrap4\BootstrapAsset
and/or yii\bootstrap4\BootstrapPluginAsset
for bootstrap 4.x ( bsVersion = 4
setting)
yii\bootstrap5\BootstrapAsset
and/or yii\bootstrap5\BootstrapPluginAsset
for bootstrap 5.x (bsVersion = 5
setting)
This is controlled by the property bsDependencyEnabled
within the asset bundle (which defaults to true
). One can override this and prevent the default yii2 bootstrap assets (CSS & JS) from loading by doing one or all of the following:
Global Override: Set Yii::$app->params['bsDependencyEnabled']
to false
in your Yii 2 application config params.php
. This setting will be applied for all Krajee Extension Asset Bundles that depend on Bootstrap assets.
'params' => [ 'bsDependencyEnabled' => false, // this will not load Bootstrap CSS and JS for all Krajee extensions // you need to ensure you load the Bootstrap CSS/JS manually in your view layout before Krajee CSS/JS assets // // other params settings below // 'bsVersion' => '5.x', // 'adminEmail' => 'admin@example.com' ]
Asset Bundle Specific Override: Set bsDependencyEnabled
to false
for the specific asset bundle within Yii2 Asset Manager component in your Yii 2 application config file.
// ... 'components' => [ 'assetManager' => [ 'bundles' => [ 'kartik\form\ActiveFormAsset' => [ 'bsDependencyEnabled' => false // do not load bootstrap assets for a specific asset bundle ], ], ], ],
When setting bsDependencyEnabled
to false
, you need to ensure that your app code/view layout loads the Bootstrap CSS and JS on your view before the Krajee CSS/JS are loaded to ensure that the Krajee extension JS plugins and CSS styles do not get broken.
Bootstrap 5.x / 4.x does not include glyphicons or any other icons framework bundled with the library. Krajee extensions therefore will use Font Awesome 5.x icons instead of glyphicons when working with Bootstrap 5.x / 4.x. You can download Font Awesome 5.x icons from the icons website. Alternatively, you can load the free version of Font Awesome from their CDN.
For Krajee extensions and demos, the Font Awesome Free version is used and loaded as the Icons Display Package on all the Yii2 demo layouts. To include font awesome assets on your page, include the following markup on the HEAD
section of your view layout file, when bsVersion
is set to 4.x
or 5.x
.
Option 1: Font CSS version of Font Awesome:
<!-- on your view layout file HEAD section --> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css">
Option 2: SVG / JS version of Font Awesome (recommended for cleaner scaling vector icons and features like icon layers):
<!-- on your view layout file HEAD section --> <script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js" crossorigin="anonymous"></script>
Alternatively, you can use the FontAwesomeAsset
from the kartik-v/yii2-icons
package to load the SVG/JS version.
// on your view layout file use kartik\icons\FontAwesomeAsset; FontAwesomeAsset::register($this);
The yii2-field-range
extension can be installed automatically or manually using one of these options:
Installation via Composer is the recommended and most easy option to install Krajee Yii2 extensions. You can install yii2-field-range
via composer
package manager. Either run:
$ php composer.phar require kartik-v/yii2-field-range "dev-master"
or add:
"kartik-v/yii2-field-range": "dev-master"
to your application's composer.json
file.
You may also manually install the extension to your project (in case your composer install does not work). Just download the source ZIP or TAR ball and extract the extension asset files and folders into your project. You may need to install dependencies manually and also set the namespaces to the extensions in your Yii2 extensions configurations manually.
form
: ActiveForm, the form instance, if used with yii active form.
model
: Model, the data model that this widget is associated with.
type
: string, the input type for rendering the range attributes. If not provided,
this defaults to FieldRange::INPUT_TEXT
.
All common input types are supported including widgets. This should be one of the following:
FieldRange::INPUT_TEXT
or 'textInput'
FieldRange::INPUT_PASSWORD
or 'passwordInput'
FieldRange::INPUT_TEXTAREA
or 'textArea'
FieldRange::INPUT_CHECKBOX
or 'checkbox'
FieldRange::INPUT_RADIO
or 'radio'
FieldRange::INPUT_LIST_BOX
or 'listBox'
FieldRange::INPUT_DROPDOWN_LIST
or 'dropDownList'
FieldRange::INPUT_CHECKBOX_LIST
or 'checkboxList'
FieldRange::INPUT_RADIO_LIST
or 'radioList'
FieldRange::INPUT_FILE
or 'fileInput'
FieldRange::INPUT_HTML5_INPUT
or 'input'
FieldRange::INPUT_WIDGET
or 'widget'
, use this for any custom widget class to be used
FieldRange::INPUT_DEPDROP
or '\kartik\depdrop\DepDrop'
FieldRange::INPUT_SELECT2
or '\kartik\select2\Select2'
FieldRange::INPUT_TYPEAHEAD
or '\kartik\widgets\Typeahead'
FieldRange::INPUT_SWITCH
or '\kartik\widgets\Switch'
FieldRange::INPUT_SPIN
or '\kartik\touchspin\TouchSpin'
FieldRange::INPUT_STAR
or '\kartik\widgets\StarRating'
FieldRange::INPUT_DATE
or '\kartik\widgets\DatePicker'
FieldRange::INPUT_TIME
or '\kartik\widgets\TimePicker'
FieldRange::INPUT_DATETIME
or '\kartik\widgets\DateTimePicker'
FieldRange::INPUT_RANGE
or '\kartik\range\RangeInput'
FieldRange::INPUT_COLOR
or '\kartik\color\ColorInput'
FieldRange::INPUT_RATING
or '\kartik\widgets\StarRating'
FieldRange::INPUT_FILEINPUT
or '\kartik\widgets\FileInput'
widgetClass
: string, the widget class to use if type is FieldRange::INPUT_WIDGET
label
: string, the label to be displayed. Positioning of the label can be controlled by the template
property. Defaults to an empty string.
labelOptions
: array the HTML attributes for the label.
template
: string, the template to render the widget. The following special tags will be replaced:
{label}
: will be replaced by the value set in the label
property
{widget}
: will be replaced by the generated input markup for the field range widget.
{error}
: will be replaced by the common error block for the range widget.
separator
: string, the field separator string between first and second field. Defaults to ← to →
.
useAddons
: boolean, whether to use Bootstrap library addons using \kartik\form\ActiveField
. If set to true
,
the form instance must be based on \kartik\form\ActiveForm
. If set to false
you can use your own form widget based on \yii\widgets\ActiveForm
.
Defaults to true
.
attribute1
: string, the first field's model attribute that this widget is associated with.
fieldConfig1
: array, the active field configuration for attribute1
, which is used when the form
property is set. You can use \kartik\form\ActiveField
advanced features like Bootstrap addons.
name1
: string, the first field's input name. This must be set if model
and attribute1
are not set.
value1
: string, the first field's input value when model
is not set.
items1
: array the option data items for first field if type
is dropDownList, listBox, checkBoxList, or radioList. Refer
the \yii\helpers\Html::dropDownList
documentation on how this is to be rendered.
options1
: array the HTML attributes for the first field's input tag.
widgetOptions1
: array the widget options for the first field if type
is FieldRange::INPUT_WIDGET
or one of the inputs from '\kartik\widgets'.
attribute2
: string, the second field's model attribute that this widget is associated with.
fieldConfig2
: array, the active field configuration for attribute2
, which is used when the form
property is set. You can use \kartik\form\ActiveField
advanced features like Bootstrap addons.
name2
: string, the second field's input name. This must be set if model
and attribute2
are not set.
value2
: string, the second field's input value when model
is not set.
items2
: array the option data items for second field if type
is dropDownList, listBox, checkBoxList, or radioList. Refer
the \yii\helpers\Html::dropDownList
documentation on how this is to be rendered.
options2
: array the HTML attributes for the second field's input tag.
widgetOptions2
: array the widget options for the second field if type
is FieldRange::INPUT_WIDGET
or one of the inputs from '\kartik\widgets'.
options
: array the HTML attributes for the generated widget input. This has the input-group
CSS class set by default.
widgetContainer
: array the HTML attributes for the widget container.
errorContainer
: array the HTML attributes for the common error block container.
use kartik\field\FieldRange; use kartik\\form\ActiveForm; $form = ActiveForm::begin(); // Simple string/text range validation configured with the Html::dropDownList. echo FieldRange::widget([ 'form' => $form, 'model' => $model, 'label' => 'Enter start and end place', 'attribute1' => 'start_place', 'attribute2' => 'end_place', 'type' => FieldRange::INPUT_DROPDOWN_LIST, 'items1' => [''=>'Select...', 1 => 'New York', 2 => 'London', 3 => 'Bangalore'], 'items2' => [''=>'Select...', 1 => 'New York', 2 => 'London', 3 => 'Bangalore'], ]); // Simple numeric/amounts range validation using \kartik\widgets\Spinner widget. FieldRange::widget([ 'form' => $form, 'model' => $model, 'label' => 'Enter amount range', 'attribute1' => 'from_amount', 'attribute2' => 'to_amount', 'type' => FieldRange::INPUT_SPIN, ]); // Simple text validation configured using Html::textInput with custom separator and // addons. Use \kartik\\form\ActiveField features to generate your own Bootstrap library addons. echo FieldRange::widget([ 'form' => $form, 'model' => $model, 'label' => 'Enter the two files', 'attribute1' => 'attachment_1', 'attribute2' => 'attachment_2', 'type' => FieldRange::INPUT_TEXT, 'separator' => 'and', 'fieldConfig1' => ['addon'=>[ 'prepend' => ['content'=>'<i class="fas fa-paperclip"></i>'], 'append' => ['content'=>'.txt'] ]], 'fieldConfig2' => ['addon'=>[ 'prepend' => ['content'=>'<i class="fas fa-paperclip"></i>'], 'append' => ['content'=>'.txt'] ]], ]); // Date range validation using \kartik\widgets\DatePicker widget and special additional // RANGE validation routine available within DatePicker plugin. echo FieldRange::widget([ 'form' => $form, 'model' => $model, 'label' => 'Enter date range', 'attribute1' => 'begin_date', 'attribute2' => 'end_date', 'type' => FieldRange::INPUT_DATE, ]); // Advanced usage with the \kartik\datecontrol\DateControl extension, which in turn is configured to // use \kartik\widgets\DatePicker for displaying date inputs. The saved formats are displayed below // each field (for this example, the dates are saved as Unix Timestamp). use kartik\datecontrol\DateControl; FieldRange::widget([ 'form' => $form, 'model' => $model, 'label' => 'Enter date range', 'attribute1' => 'from_date', 'attribute2' => 'to_date', 'type' => FieldRange::INPUT_WIDGET, 'widgetClass' => DateControl::classname(), 'widgetOptions1' => [ 'saveFormat' => 'php:U' ], 'widgetOptions2' => [ 'saveFormat' => 'php:U' ], ]); // DateTime range validation using \kartik\widgets\DateTimePicker widget. echo FieldRange::widget([ 'form' => $form, 'model' => $model, 'label' => 'Enter time range', 'attribute1' => 'datetime_start', 'attribute2' => 'datetime_end', 'type' => FieldRange::INPUT_DATETIME, ]); // Time range validation using \kartik\widgets\TimePicker widget. echo FieldRange::widget([ 'form' => $form, 'model' => $model, 'label' => 'Enter time range', 'attribute1' => 'datetime_start', 'attribute2' => 'datetime_end', 'type' => FieldRange::INPUT_DATETIME, ]); ?> ActiveForm::end(); // Usage of the widget with Bootstrap library HORIZONTAL form layout and using // \kartik\datecontrol\DateControl widget. The horizontal layout formatting // only works with \kartik\\form\ActiveForm use kartik\\form\ActiveForm; $form = ActiveForm::begin([ 'type' => ActiveForm::TYPE_HORIZONTAL, 'formConfig' => ['labelSpan' => 3, 'deviceSize' => ActiveForm::SIZE_SMALL], ]); echo $form->field($model, 'username'); echo FieldRange::widget([ 'form' => $form, 'model' => $model, 'label' => 'Enter date range', 'attribute1' => 'from_date_1', 'attribute2' => 'to_date_1', 'type' => FieldRange::INPUT_WIDGET, 'widgetClass' => DateControl::classname(), 'widgetOptions1' => [ 'saveFormat'=>'php:U', 'options'=>[ 'pluginOptions' => ['autoclose' => true,], ], ], 'widgetOptions2' => [ 'saveFormat'=>'php:U', 'options'=>[ 'pluginOptions' => ['autoclose' => true,], ], ], ]); ActiveForm::end();
yii2-field-range is released under the BSD-3-Clause
License. See the bundled LICENSE.md for details.
Comments & Discussion
Note
You can now visit the Krajee Webtips Q & A forum for searching OR asking questions OR helping programmers with answers on these extensions and plugins. For asking a question click here. Select the appropriate question category (i.e. Krajee Plugins) and choose this current page plugin in the question related to field.
The comments and discussion section below are intended for generic discussions or feedback for this plugin. Developers may not be able to search or lookup here specific questions or tips on usage for this plugin.