kartik gridview中的Yii2 datepicker无法正常工作

时间:2015-02-04 09:04:59

标签: php gridview yii datepicker yii2

我正在使用kartik gridview,我想在搜索字段中添加datepicker 我也在使用datecontrol

这是我得到的,但验证无效:

Gridview日期选择器:

        [
            'attribute'=>'date',
            'value' => function ($model, $index, $widget) {
                return Yii::$app->formatter->asDate($model->date);
            },
            'filterType' => GridView::FILTER_DATE,
            'filterWidgetOptions' => [
                'pluginOptions' => [
                    'format' => 'dd-mm-yyyy',
                    'autoclose' => true,
                    'todayHighlight' => true,
                ]
            ],
            'width' => '160px',
            'hAlign' => 'center',
        ],

Datecontrol设置:

    'datecontrol' =>  [
        'class' => 'kartik\datecontrol\Module',

        // format settings for displaying each date attribute (ICU format example)
        'displaySettings' => [
            Module::FORMAT_DATE => 'php:d-m-Y',
            Module::FORMAT_TIME => 'php:H:m:s a',
            Module::FORMAT_DATETIME => 'dd-MM-yyyy HH:mm:ss a',
        ],

        // format settings for saving each date attribute (PHP format example)
        'saveSettings' => [
            Module::FORMAT_DATE => 'php:U', // saves as unix timestamp
            Module::FORMAT_TIME => 'php:H:i:s',
            Module::FORMAT_DATETIME => 'php:Y-m-d H:i:s',
        ],

        // set your display timezone
        'displayTimezone' => 'Europe/Brussels',

        // set your timezone for date saved to db
        'saveTimezone' => 'Europe/Brussels',

        // automatically use kartik\widgets for each of the above formats
        'autoWidget' => true,

        // default settings for each widget from kartik\widgets used when autoWidget is true
        'autoWidgetSettings' => [
            Module::FORMAT_DATE => ['pluginOptions' => [
                'autoclose' => true,
                'todayHighlight' => true,
                //'todayBtn' => true
            ]],
            Module::FORMAT_DATETIME => [], // setup if needed
            Module::FORMAT_TIME => [], // setup if needed
        ],
        // Use custom convert action
        'convertAction' => '/cms/parse/convert-date-control'
    ],

Searchmodel:

<?php

namespace infoweb\news\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;

/**
 * NewsSearch represents the model behind the search form about `infoweb\empoyees\models\News`.
 */
class NewsSearch extends News
{
/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['active'], 'integer'],
        [['title', 'date'], 'safe'],
    ];
}

/**
 * @inheritdoc
 */
public function scenarios()
{
    // bypass scenarios() implementation in the parent class
    return Model::scenarios();
}

/**
 * Creates data provider instance with search query applied
 * @return ActiveDataProvider
 */
public function search($params)
{

    // Transform the date to a unix timestamp for usage in the search query
    if (isset($params['NewsSearch']['date'])) {
        $origDate = $params['NewsSearch']['date'];
        $params['NewsSearch']['date'] = strtotime($params['NewsSearch']['date']);
    }

    $query = News::find();

    $query->andFilterWhere(['language' => Yii::$app->language]);

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort' => ['defaultOrder' => ['date' => SORT_ASC]],
        'pagination' => [
            'pageSize' => 50,
        ],
    ]);

    // Join the entity model as a relation
    $query->joinWith(['translations']);

    // enable sorting for the related column
    $dataProvider->sort->attributes['title'] = [
        'asc' => ['title' => SORT_ASC],
        'desc' => ['title' => SORT_DESC],
    ];

    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }

    $query->andFilterWhere([
        'id' => $this->id,
        'date' => $this->date,
        'active' => $this->active,
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ]);

    $query->andFilterWhere(['like', 'title', $this->title]);

    // Format the date for display
    if (isset($params['NewsSearch']['date'])) {
        $this->date = $origDate;
    }

    return $dataProvider;
}
}

屏幕:

Date field validation is wrong

2 个答案:

答案 0 :(得分:0)

添加规则

[['title','date'], 'safe'],

答案 1 :(得分:0)

添加

// Convert date to Unix timestamp
if (!empty($params[StringHelper::basename(self::className())]['date'])) {
    $query->andFilterWhere([
        'date' => strtotime($params[StringHelper::basename(self::className())]['date'])
    ]);
}

之后

if (!($this->load($params) && $this->validate())) {
    return $dataProvider;
}

并将[['date'], 'safe'],添加到您的规则