I have a date field on a form that I would like to insert a value into the database. I created some error messages for the form. I am using form requests for validation logic. When I submit the form I have an error message about an empty date value. How can I solve this problem?
Thanks, Advance
Blade Template
<form method="POST" action="{{ route('store.education') }}" class="wt-formtheme wt-userform"> <fieldset> <div class="form-group form-group-half"> <input type="text" name="edu_organisation[]" class="form-control @error('edu_organisation') is-invalid @enderror" id="education_organisation" placeholder="Company Title" data-edu="1"> </div> <div class="form-group form-group-half"> <input type="date" name="edu_start[]" class="form-control @error('edu_start') is-invalid @enderror" id="education_start" placeholder="Starting Date" data-edu="1"> </div> <div class="form-group form-group-half"> <input type="date" name="edu_end[]" class="form-control @error('edu_end') is-invalid @enderror" id="education_end" placeholder="Ending Date *" data-edu="1"> </div> <div class="form-group form-group-half"> <input type="text" name="edu_position[]" class="form-control @error('edu_position') is-invalid @enderror" placeholder="Your Job Title"> </div> <div class="form-group"> <textarea name="edu_description[]" class="form-control @error('edu_description') is-invalid @enderror" placeholder="Your Job Description"></textarea> </div> <div class="form-group"> <span>* Leave ending date empty if its your current job</span> </div> </fieldset> </form> @error('edu_organisation') <div class="alert alert-danger alert-dismissible fade show"> <span> {{ $message }}</span> <a href="javascript:void(0)" class="close" data-dismiss="alert" aria-label="Close"><i class="fa fa-close"></i></a> </div> @enderror @error('edu_start') <div class="alert alert-danger alert-dismissible fade show"> <span> {{ $message }}</span> <a href="javascript:void(0)" class="close" data-dismiss="alert" aria-label="Close"><i class="fa fa-close"></i></a> </div> @enderror @error('edu_end') <div class="alert alert-danger alert-dismissible fade show"> <span> {{ $message }}</span> <a href="javascript:void(0)" class="close" data-dismiss="alert" aria-label="Close"><i class="fa fa-close"></i></a> </div> @enderror @error('edu_position') <div class="alert alert-danger alert-dismissible fade show"> <span> {{ $message }}</span> <a href="javascript:void(0)" class="close" data-dismiss="alert" aria-label="Close"><i class="fa fa-close"></i></a> </div> @enderror @error('edu_description') <div class="alert alert-danger alert-dismissible fade show"> <span> {{ $message }}</span> <a href="javascript:void(0)" class="close" data-dismiss="alert" aria-label="Close"><i class="fa fa-close"></i></a> </div> @enderror
Controller
public function storeEducation(StoreEducationRequest $request) { $validated = $request->validated(); for ($i=0; $i <count($validated['edu_organisation']) ; $i++) { Education::updateOrCreate( ['profile_key' => Auth::user()->profile->key],[ 'organisation' => $validated['edu_organisation'][$i], 'start' => $validated['edu_start'][$i], 'end' => $validated['edu_end'][$i], 'position' => $validated['edu_position'][$i], 'description' => $validated['edu_description'][$i] ]); } }
FormRequest
class StoreEducationRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'edu_organisation' => ['required','array'], 'edu_start' => ['required','array','date'], 'edu_end' => ['required','array','date'], 'edu_position' => ['required','array'], 'edu_description' => ['required','array'] ]; } public function attributes() { return [ 'edu_organisation' => 'Organisation', 'edu_start' => 'Date of start', 'edu_end' => 'Date of end', 'edu_position' => 'Position', 'edu_description' => 'Description' ]; } }
Schema
public function up() { Schema::create('educations', function (Blueprint $table) { $table->id(); $table->string('profile_key'); $table->string('organisation'); $table->date('start'); $table->date('end'); $table->string('position'); $table->text('description'); $table->foreign('profile_key')->references('key')->on('profiles'); }); }
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The rule of edu_start
is array, so when you want to validate date
you should set edu_start
array items not the edu_start
.
Please try to change your rules to:
public function rules() { return [ 'edu_organisation' => ['required','array'], 'edu_start' => ['required','array'], 'edu_start.*' => ['date'], 'edu_end' => ['required','array'], 'edu_end.*' => ['date'], 'edu_position' => ['required','array'], 'edu_description' => ['required','array'] ]; }
Method 2
In your model Education
set dates columns inside dates
var:
class Education extends Model { protected $dates = ['start', 'end'] }
In that case laravel will support your dates.
** If that still not working please post here the input you`ve got in the server, and the error that appeared.
Method 3
Set default date values in form group in html , relevant to date inputs
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0