I submit the form and have some validation mean email,require,unique email,
when validation have error message then laravel 5.2 return validation return array.
I submit the form and have some validation mean email,require,unique email,
when validation have error message then laravel 5.2 return validation return array.
I guess you want to retain the submitted data on error.
Considering a sample
public function postJobs(Request $request) {
$input = $request->all();
$messages = [
'job_title.required' => trans('job.title_required'),
];
$validator = Validator::make($request->all(), [
'job_title' => 'required'
], $messages);
if ($validator->fails()) { // redirect if validation fails, note the ->withErrors($validator)
return redirect()
->route('your.route')
->withErrors($validator)
->withInput();
}
// Do other stuff if no error
}
And, in the view you can handle errors like this:
<div class="<?php if (count($errors) > 0) { echo 'alert alert-danger'; } ?>" >
<ul>
@if (count($errors) > 0)
@foreach ($errors->all() as $error)
<li>{!! $error !!}</li>
@endforeach
@endif
</ul>
</div>
And if you want the input data, you need to redirect with ->withInput(); which can be fetch in view like:
Update
<input name= "job_title" value="{{ Request::old('job_title') }}" />
But, the best thing is to use laravel Form package so they all are handled automatically.
return redirect() ->route('your.route') ->withErrors($validator) ->withInput(); this will redirect the form without losing the form field values. so you can call it in your fields like <input value="{{ Request::old('job_title') }}" />If you just need form data, you can use Request object:
public function store(Request $request)
{
$name = $request->input('firstName');
}
https://laravel.com/docs/5.1/requests#accessing-the-request
Alternatively, you could use $request->get('firstName');
Use old() to get previous value from input. Example:
<input name="firstName" value="{{old('firstName')}}">
See documentation here https://laravel.com/docs/5.1/requests#old-input