yii2-jui icon indicating copy to clipboard operation
yii2-jui copied to clipboard

DatePicker transforms some invalid user input into invalid dates

Open PowerGamer1 opened this issue 9 years ago • 0 comments

In some cases (see example below) DatePicker transforms invalid user input into invalid date. Instead exact invalid user input must remain in the field.

Example: Yii::$app->formatter->dateFormat is set to 'php:Y-m-d' Date validator is set as: ['someDate', 'date'] DatePicker is rendered as: <?= $form->field($model, 'someDate')->widget(DatePicker::className()) ?>

When user enters value '1234567' into DatePicker input field and submits the form that input field incorrectly displays '1970-01-15' instead of original user input '1234567'. This happens because the following code tries to format value '1234567' using Yii::$app->formatter->asDate() which in turn tries to parse its first argument as UNIX timestamp value:

    protected function renderWidget()
    {
        // ..........
        if ($value !== null && $value !== '') {
            // format value according to dateFormat
            try {
                $value = Yii::$app->formatter->asDate($value, $this->dateFormat);
            } catch(InvalidParamException $e) {
                // ignore exception and keep original value if it is not a valid date
            }
        }

Don't know how to work around that problem and I think to fix it a significant redesign of DatePicker will be needed.

PowerGamer1 avatar Oct 17 '15 19:10 PowerGamer1