kartik-v / yii2-date-range

A Date Range Picker for Bootstrap useful for reports and filtering.
http://demos.krajee.com/date-range
Other
93 stars 81 forks source link

invalid date error on empty date #108

Closed hhniao closed 6 years ago

hhniao commented 7 years ago

im using it on yii2 filter of girdview; ` [ 'attribute' => 'created_at', 'format' => ['date', 'php:Y-m-d H:i:s'], 'filter' => DateRangePicker::widget([ 'name' => 'AdvertisementQuery[created_at]', 'value' => $searchModel->created_at, 'startAttribute' => 'start', 'endAttribute' => 'end',

                'pluginOptions' => [
                    'opens' => 'left',
                    'locale' => [
                        'format' => 'YYYY/MM/DD',
                    ],
                ],
            ]),
        ],

` when i set startAttribute and endAttribute and then show me invalid date. because on this configuration situation it will generate javascript code like this:

jQuery("#w1").daterangepicker(daterangepicker_93cc6fdc, function(start,end,label){
var val=start.format('YYYY/MM/DD') + ' - ' + end.format('YYYY/MM/DD');
jQuery("#w1").val(val).trigger('change');
});

if value or startDate or endDate is null of daterangepicker_93cc6fdc, daterangepicker will show invalid date error.

so kartik\daterange\DateRangePicker::initSettings must do not set startDate and endDate and value in pluginOptions on line 340,345,346;

please modify it. thanks!

hhniao commented 7 years ago

im writen a new class extends from your code, and modify follow line.

if (!empty($this->value)) {
            $dates = explode($this->_separator, $this->value);
            if (count($dates) > 1) {
                $this->pluginOptions['startDate'] = $dates[0];
                $this->pluginOptions['endDate'] = $dates[1];
                $this->initRangeValue('start', $dates[0]);
                $this->initRangeValue('end', $dates[1]);
            }
//this line has been modify,may be must change to jadgement is valid date
        } elseif (!empty($this->value) && $this->startAttribute && $this->endAttribute) {
            $start = $this->getRangeValue('start');
            $end = $this->getRangeValue('end');
            $this->value = $start . $this->_separator . $end;
            if ($this->hasModel()) {
                $attr = $this->attribute;
                $this->model->$attr = $this->value;
            }
            $this->pluginOptions['startDate'] = $start;
            $this->pluginOptions['endDate'] = $end;
        }