Reputation: 187
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);
value successfully saved but without "false" data not saved
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;
}
Upvotes: 3
Views: 1630
Reputation: 10104
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();
}
Upvotes: 1