Laravel 4: check start and end date with before and after check

I want to check two date fields in the form from_date and end_date. The need for checking from_date is less than end_date.

$rules = array('from_date' => array('sometimes','date_format:"Ymd"', 'before:'.Input::get('to_date') ), 'to_date' => array('sometimes','date_format:"Ymd"', 'after:'.Input::get('from_date') ) ); 

This is what I tried. But that does not work. If I give to_date as an empty value, this will happen through an error.

0
date php validation laravel
source share
5 answers

Laravel 5:

Here is a more extensive approach that follows modern principles and is more similar to Laravel. It's a little trickier, but still easy to follow, and the end results are much cleaner.

Let's start by changing a few things. Let me reduce this to a specific problem, use the new array syntax, and apply formatting.

 $rules = [ 'from_date' => [ 'before:'.Input::get('to_date') // This is what we will learn to do ], 'to_date' => [ 'after:'.Input::get('from_date') // Do this one on your own ] ]; 

Now create a new request with php artisan make:request StoreWhateverRequest . This will create the file App/HTTP/Request/StoreWhateverRequest.php . Open it and put your rules in the returned array of the rules() function.

 return [ 'from_date' => 'date', 'to_date' => 'date|after_field:from_date' ]; 

This will not work yet, because after_field is not yet available. Let me create it. We need a new class that extends the validator. You can put it in app/Services . We need something similar:

 <?php namespace App\Services; use Illuminate\Validation\Validator; use Carbon\Carbon; class AfterFieldValidator extends Validator { public function validateAfterField($attribute, $value, $parameters) { return Carbon::parse($value) > Carbon::parse($this->data[$parameters[0]]); } } 

In the above example, we have: $attribute , which is the name of the field we are checking (to_date), $value is the value of the field we are checking, and $parameters are the parameters that we passed to Validator (from_date) in 'to_date' => 'date|afterField:from_date' . We also need other data fields passed to Validator, we can get them using $this->data . Then we just need to pre-form the logic. You really don't need Carbon here, but be sure to parse the string so that we don't compare the strings.

Now we need to download this application. To do this, enter the boot() code below in app/Providers/AppServiceProviders.php .

 Validator::resolver(function($translator, $data, $rules, $messages) { return new afterFieldValidator($translator, $data, $rules, $messages); }); 

The last step is the easiest. Just enter and enter StoreWhateverRequest into our controller.

 ... public function store(StoreWhateverRequest $request) { ... 

Done. I feel this is a pretty solid way to solve the problem.

+6
source share

Please use the code below in laravel 5.2 and it works great for checking the start and end dates.

 $this->validate($request, [ 'start_date' => 'required|before:end_date', 'end_date' => 'required', ]); 
+4
source share

Anyway

I did it like that. Thus, even if any date in the form is empty, it will be automatically filled in and check the check

 $inputs = Input::all(); if(!empty($inputs)) { $default_date_arr = array('from_date' => date('Ym-d', strtotime('-1 days')), 'to_date' => date('Ym-d')); $inputs = $inputs+$default_date_arr; } $rules = array('from_date' => array('sometimes','date_format:"Ymd"', 'before:'.$to_date) , 'to_date' => array('sometimes','date_format:"Ymd"', 'after:'.$from_date ) ); $validator = Validator::make($inputs,$rules); if($validator->fails()) { ... } 

This cannot be the answer to what I asked. But I just needed a way to end this. May be useful for others.

+2
source share

Just came across this and thought that share the answer I received: Compare attributes when checking

Basically, you would create a new Validator that extends Illuminate \ Validation \ Validator and writes its own rule there:

 public function validateEndAfter($attribute, $value, $parameters) { $start_date = $this->getValue($parameters[0]); // get the value of the parameter (start_date) return (strtotime($value) > strtotime($start_date)); } 

then in your validator use the new rule:

 $rules = [ 'start_date' => 'required', 'end_date'=> 'required|end_after:start_date', ] 
+2
source share

I know this is a question about Laravel 4 BUT, if you are using Laravel 5.3, now you can use something like:

 $rules = array( 'date_start' => 'required|date_format:Ymd|before_or_equal:date_end', 'date_end' => 'required|date_format:Ymd|after_or_equal:date_start' ); $validator = Validator::make($request->all(), $rules); 
+2
source share

All Articles