How to make date input field greater than or equal to another date field using validation in laravel

Hi, I am developing an application using laravel, is there a way to make the date input field larger or equal to another date field using validation.

I know that I can achieve this through jquery, and I already have this work, but I want to know as much as possible with the laravel check, since laravel has some predefined checks.

Example

protected $validationRules = array ( 'a' => 'date', 'b' => 'date|(some validation so that the b value is greater than or equal to that of 'a')' ); 

EDIT

If there is any other approach to solving the problem using the laravel concept, please tell me

I tried

 Validator::extend('val_date', function ($attribute,$value,$parameters) { return preg_match("between [start date] and DateAdd("d", 1, [end date])",$value); }); 

Thank you in advance

+6
source share
4 answers

I had the same problem. before and after do not work if the dates can be the same. Here is my short solution:

NOTE. Laravel 5.3.25 and later have new built-in rules: before_or_equal and after_or_equal


 // 5.1 or newer Validator::extend('before_or_equal', function($attribute, $value, $parameters, $validator) { return strtotime($validator->getData()[$parameters[0]]) >= strtotime($value); }); // 5.0 & 4.2 Validator::extend('before_or_equal', function($attribute, $value, $parameters) { return strtotime(Input::get($parameters[0])) >= strtotime($value); }); 

 $rules = array( 'start'=>'required|date|before_or_equal:stop', 'stop'=>'required|date', ); 

+21
source

Emil Aspman's answer is correct, but it does not work for Laravel 5.2. This solution works for Laravel 5.2:

  Validator::extend('before_equal', function($attribute, $value, $parameters, $validator) { return strtotime($validator->getData()[$parameters[0]]) >= strtotime($value); }); 
+8
source

The correct solution would be if you extended Validator with your own rule. A simple example from the docs:

 Validator::extend('foo', function($attribute, $value, $parameters) { return $value == 'foo'; }); 

More here

0
source

Yes, you can use after:date or before:date as follows:

 protected $rules = array( 'date' => 'after:'.$yourDate ); 

or alternatively

 protected $rules = array( 'date' => 'before:'.$yourDate ); 

He will do what you described. Also check out the official documentation. You can also specify your own rules using custom validation rules .

-1
source

Source: https://habr.com/ru/post/978400/


All Articles