PHP定义了relative formats,而Laravel对此没有一个available validation rule。例如:
/**
* 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'
];
}
我们如何验证这些格式?
更新
我现在正在使用什么:
创建规则类。
php artisan make:rule RelativeFormat
说明逻辑。
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return (bool) strtotime($value);
}
并验证:
/**
* 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]
];
}
答案 0 :(得分:3)
您可以创建自己的验证规则:
Validator::extend('relative_format', function($attribute, $value, $parameters)
{
return (bool) strtotime($value);
});
并将其添加到您的AppServiceProvider。