简体   繁体   中英

The format of Date is invalid

I have an array like:

 $holiday = array(array('h_date' => $new_year, 'd_name' => 'New Year'),
    array('h_date' => $memorial_day, 'd_name' => 'Memorial Day')
 foreach($holiday as $holidays){
 $date = $holidays["h_date"];
 $name = $holidays["d_name"]


when i save in mysql database

 $model = new Holiday();
 $model->holiday_date = $date;
 $model->display_name = $name;
 $model->save(); 

when i write

 $model->save(false); 



when saw validation error than error:

 Array ( [holiday_date] => Array ( [0] => The format of Holiday Date is invalid.))


we use

protected function beforeSave(){
    if(parent::beforeSave()){
        $this->holiday_date=date('Y-m-d',strtotime($this->holiday_date));

        return TRUE;
    }

    return false;
}

You should change the date format on beforeValidate or either change the validation rules for your holiday_date attribute, as beforeSave is executed after the validation is passed.

public function beforeValidate()
{
    if ($this->isNewRecord)
    {
        $this->holiday_date = date('Y-m-d', strtotime($this->holiday_date));
    }
    return parent::beforeValidate();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM