mardi 1 août 2017

Laravel 5.1 - Date Validation & Attribute casting

My model has the following validation and rules :

public $rules = [
    'hire_date'     => 'date|required|required_with:dismiss_date',
     OR
    'hire_date'     => 'date_format:"Y-m-d"...',
];

protected $casts = [
    //'hire_date'     => 'date' ,
    'dismiss_date'    => 'date' ,
];

// Or 

protected $dates = [ 
    'hire_date'   
];

If I cast the Hire Date or any other date field then it Throws an exception, as if the validation is not checking the format ; e.g 2017-08-AA

A two digit hour could not be found Data missing" on line 582 of .....nesbot\carbon\src\Carbon\Carbon.php

If I remove the attribute casting for dates the validation works as expected and throws a validation exception. A workaround to keep the casting was to use mutators for all date fields

public function setHireDateAttribute ( $date )
{
    $this->attributes['hire_date']   =   TZ::isValidateDate($date) ? $date : null;
}

So my question is there something wrong with the setup ? if not what is the point of the date or date_format validations if I have to do it manually.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire