Reputation: 332
My model when validate input date, convert the format type and it cause trouble.
<?php $form = yii\widgets\ActiveForm::begin([
'options' => ['class' => ''],
'fieldConfig' => ['template' => "{label}\n{input}\n{hint}\n{error}"]
]); ?>
<div class="col-md-4">
<?= $form->field($model, 'startDate')->widget(DatePicker::classname(),
[
'language' => 'pt-BR',
'dateFormat' => 'dd/MM/yyyy',
'clientOptions' => [
'yearRange' => '2005:+0',
'defaultDate' => date('d/m/Y', strtotime('-7days'))
],
'options' => [
'class' => 'form-control',
'maxlength' => '10',
'placeholder' => 'Data inicial',
]
]
)->label(false) ?>
</div>
<div class="col-md-4">
<?= $form->field($model, 'endDate')->widget(DatePicker::classname(),
[
'language' => 'pt-BR',
'dateFormat' => 'dd/MM/yyyy',
'clientOptions' => [
'yearRange' => '2005:+0',
'defaultDate' => date('d/m/Y')
],
'options' => [
'class' => 'form-control',
'maxlength' => '10',
'placeholder' => 'Data final',
]
]
)->label(false) ?>
</div>
<div class="col-md-4">
<?= Html::submitButton('Filtrar', ['class' => 'btn btn-primary']) ?>
<?php yii\widgets\ActiveForm::end(); ?>
</div>
And models:
<?php
namespace frontend\models;
use yii\base\Model;
use Yii;
class Analysis extends Model
{
public $startDate;
public $endDate;
public function rules()
{
return [
['startDate', 'required'],
['startDate', 'date', 'format' => 'dd/MM/yyyy'],
['startDate', 'checkDateToday'],
['startDate', 'checkGAMinDate'],
['endDate', 'required'],
['endDate', 'date', 'format' => 'dd/MM/yyyy'],
['endDate', 'checkDateToday'],
['endDate', 'checkGAMinDate'],
['endDate', 'checkDateGreaterThan']
];
}
public function checkDateToday($attribute, $params)
{
if($this->$attribute <= date('d/m/Y')){
return true;
} else {
var_dump($this->$attribute);
$this->addError($attribute, $attribute . ' não pode ser maior que hoje: ' . date('d/m/Y'));
}
}
public function checkGAMinDate($attribute, $params)
{
if($this->$attribute > date('01/01/2005')){
return true;
} else {
$this->addError($attribute, $attribute . ' não pode ser menor que 01/01/2005');
}
}
public function checkDateGreaterThan($attribute, $params)
{
if($this->endDate < $this->startDate){
return true;
} else {
$this->addError($attribute, 'Data final não pode ser maior que data incial.');
}
}
public function attributeLabels()
{
return [
'startDate' => Yii::t('app', 'Data inicial'),
'endDate' => Yii::t('app', 'Data final'),
];
}
}
When i put a date like "25/12/2015" ('d/m/Y'), the model return after validate: "12/25/2015". Whats going wrong?
Upvotes: 1
Views: 1646
Reputation: 4076
The problem is Datepicker's value property. As the documentation says, it uses Yii::$app->formatter->asDate()
to generate the value.
Since the value will be a string always, it must be a valid date format. And as you can see there is no dd/mm/yyyy
format, but there is a mm/dd/yyyy
.
I can not find a way to cancel this formatting, so you can either change your format or, if you want to maintain this format, you could just change the value of the model to another valid format (IE: dd-mm-yyyy
), only to print it correctly.
Something like this before the form:
$model->startDate = str_replace('/', '-', $model->startDate);
$model->endDate = str_replace('/', '-', $model->endDate);
Upvotes: 2