Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DanielFuerst's avatar

Validation Start/End Date with min and max amount of days inbetween

Hi there I am trying to implement a Validation rule that restricts the min and max amounts of days between a start and an end date.

What is the best way to do this?

  1. Add a hidden field in the form that calculates the differences in days e.g. by javascript and than validate this field in a FormRequest controller with between?

  2. Or add a Custom validation rule? However I am not sure how to achieve this.

Here is how my FormRequest Class looks like so far:

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $yesterday = Carbon::yesterday();

        $two_weeks_from_now = $yesterday->addWeeks(2);

        $end = Carbon::parse($this->end);
        $start =  Carbon::parse($this->start);

        $diff_in_days = $end->diffInDays($start);

        return [
            'title' => 'required',
            'description' => 'required',
            'start' => 'required|date|before:end|after:' . $two_weeks_from_now,
            'end' => 'required|date|after:' . $two_weeks_from_now,
        ];
    }

Do you have an idea?

0 likes
2 replies
DanielFuerst's avatar

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?

shalonteoh's avatar

@DANIELFUERST - Just add this after validator extend in CustomValidatorServiceProvider

$this->app['validator']->replacer('days_in_between', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':values', $parameters[0], $message);
  });

Please or to participate in this conversation.