So I came up with the following:
I registered a new CustomValidationServiceProvider looking like the following:
class CustomValidatorServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->app['validator']->extend('days_in_between', function ($attribute, $value, $parameters) {
$end = Carbon::parse($value);
$start = Carbon::parse($parameters[2]);
$diff_in_days = $end->diffInDays($start);
if (($diff_in_days <= $parameters[0]) OR ($diff_in_days >= $parameters[1])) {
return false;
}
return true;
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
and registered it in the app.php config file
'App\Providers\CustomValidatorServiceProvider',
.. and updated my FormRequest class as such:
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$yesterday = Carbon::yesterday();
$two_weeks_from_now = $yesterday->addWeeks(2);
$start = Carbon::parse($this->start);
return [
'title' => 'required',
'description' => 'required',
'start' => 'required|date|before:end|after:' . $two_weeks_from_now . '',
'end' => 'required|date|after:' . $two_weeks_from_now . '|days_in_between:6,28,' . $start,
];
}
It works as it is supposed to.
Additionally I would like to be able to use the values entered in the custom validation rule "days_in_between" to be available in my error messages. I had a look at http://laravel.com/docs/5.0/validation#custom-error-messages but I don't know how to register a custom validation resolver to replace my :min :max :star attributes in the days_in_between validation rule.
Any hints?