PHP defines the relative formats and Laravel doesn't seen to have an available validation rule for that. For example:
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'created-at-from' => 'relative_format',
'created-at-until' => 'nullable|relative_format|gte:created-at-from'
];
}
How can we validate those formats?
UPDATE
What I'm using now:
Create a rule class.
php artisan make:rule RelativeFormat
Put the logic.
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return (bool) strtotime($value);
}
And validates:
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'created-at-from' => [new RelativeFormat],
'created-at-until' => ['nullable', new RelativeFormat]
];
}
You can just create your own validation rule:
Validator::extend('relative_format', function($attribute, $value, $parameters)
{
return (bool) strtotime($value);
});
And add it to your AppServiceProvider.