Creating a custom input widget

Yii has a very good set of form widgets, but as with every framework out there, Yii does not have them all. In this recipe, we will learn how to create your own input widget. For our example, we will create a range input widget.

Getting ready

Create a new application by using the Composer package manager, as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-start-installation.html.

How to do it...

  1. Create a widget file, @app/components/RangeInputWidget.php, as follows:
    <?php namespace app\components; use yii\base\Exception; use yii\base\Model; use yii\base\Widget; use yii\helpers\Html; class RangeInputWidget extends Widget { public $model; public $attributeFrom; public $attributeTo; public $htmlOptions ...

Get Yii2 Application Development Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.