DatePicker Widget for Yii2 - uniqcle/Yii2 GitHub Wiki
- Устанавливаем Композер, если необходимо
- Запускаем
$ composer require 2amigos/yii2-date-picker-widget:~1.0
or add
"2amigos/yii2-date-picker-widget" : "~1.0"
to the require section of your application's composer.json file.
- Добавляем во View
use dosamigos\datepicker\DatePicker;
...
<?= $form->field($model, 'date')->widget(
DatePicker::className(), [
// inline too, not bad
'inline' => true,
// modify template for custom rendering
'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
'clientOptions' => [
'autoclose' => true,
'format' => 'dd-M-yyyy'
]
]);?>
Контроллер frontend\controllers\lib\BookshopController.php
namespace frontend\controllers\lib;
use Yii;
use frontend\models\lib\Book;
use frontend\models\lib\Publisher;
class BookshopController extends \yii\web\Controller
{
...
public function actionCreate(){
$book = new Book();
$publishers = Publisher::getPublishersList();
if($book->load( Yii::$app->request->post() ) && $book->save() ){
Yii::$app->session->setFlash('success', 'Новый автор добавлен');
return $this->redirect(['lib/bookshop/index']);
}
return $this->render('create', [
'book' => $book,
'publishers' => $publishers
]);
}
}
Модель Publisher frontend\models\lib\Publisher.php
namespace frontend\models\lib;
use Yii;
use yii\helpers\ArrayHelper;
...
public static function getPublishersList(){
$List = self::find()->asArray()->all();
return ArrayHelper::map($List, 'id', 'name');
}
}
Вью frontend\views\lib\bookshop\create.php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use yii\helpers\Url;
use dosamigos\datepicker\DatePicker;
?>
<?php $form = ActiveForm::begin(); ?>
<?php echo $form->field($book, 'title'); ?>
<?php echo $form->field($book, 'publisher_id')->dropDownList($publishers); ?>
<?= $form->field($book, 'date_published')->widget(
DatePicker::className(), [
// inline too, not bad
'inline' => true,
// modify template for custom rendering
'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
'clientOptions' => [
'autoclose' => true,
'format' => 'dd/mm/yy'
]
]);?>
<?php echo Html::submitButton('Save', ['class' => 'btn btn-success']); ?>
<?php ActiveForm::end(); ?>
<a href = "<?php echo Url::to(['lib/bookshop/index']); ?>">Назад</a>