Laravel 5: How to check data input in datetime format from 4 input fields?

I have a form that includes a deadline, and the user must set a deadline in the four input fields as follows:

<div class="form-group col-lg-3"> {!! Form::label('year', 'Year', ['class' => 'control-label']) !!} {!! Form::selectYear('year',$year, $year +1, null , ['class' => 'form-control']) !!} </div> <div class="form-group col-lg-3"> {!! Form::label('month', 'Month', ['class' => 'control-label']) !!} {!! Form::selectRange('month', 1, 12 , null , ['class' => 'form-control']) !!} </div> <div class=" form-group col-lg-3"> {!! Form::label('day', 'Day', ['class' => 'control-label']) !!} {!! Form::selectRange('day', 1, 31 , null , ['class' => 'form-control']) !!} </div> <div class=" form-group col-lg-3"> {!! Form::label('hour', 'Hour', ['class' => 'control-label']) !!} {!! Form::selectRange('hour', 6, 23 , null , ['class' => 'form-control']) !!} </div> 

In formRequest, I collect these four fields as a deadline. So my formRequest looks like this:

 public function rules() { $this->prepInput(); return [ ]; } public function prepInput(){ $input=$this->all(); ... $input['deadline']=$this->prepDeadline($input['hour'], $input['month'], $input['day'], $input['year']); ... $this->replace($input); } public function prepDeadline($hour,$month,$day, $year){ $time = jDateTime::mktime($hour, 0, 0, $month, $day, $year); return $deadline = strftime("%Y-%m-%d %H:%M:%S", $time); } 

The deadline is the Jalali datetime, and I need to check if the user has selected a valid date or not (e.g. 1394/12/31 is not a valid date). The jDatetime package has a checkdate method that works just like php checkdate . Where and how can I confirm the date in thisRequest form and notify the user of the choice of a valid date? Actually, I need this check to be done before the deadline is passed to prepInput .

+5
source share
2 answers

To solve this problem, I needed middleware to check the date, and I found a solution on Laracasts Details can be found there.

-1
source

Laravel Validator has a date_format rule, so when setting up the rules in the form request, you just need to add something like:

 public function rules() { $this->prepInput(); return [ 'deadline' => 'date_format:Ymd H:i:s' ]; } 

Then, in your opinion:

 @if ($errors->has('deadline')) {{ $errors->first('deadline') }} @endif 

You can also simplify the prepInput method by simply combining the year / month / day / hours into a string to create a deadline; it will be almost the same.

+4
source

All Articles