Reputation: 124
I have a form in modal, I want to redirect to the modal with all the errors, but it does not seem to work, here is my validator in the controller
$validator = Validator::make($request->all(), [
'company' => 'required|string|max:255',
'description' => 'required|string|max:300',
'engineers' => 'required',
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}
Here is my jquery in the blade view where my modal is activated
@if (count($errors) > 0)
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
$('#create').modal('show');
</script>
@endif
the prototype of my modal
<div class="modal fade container" id="createProject">
{!! Form::open([ 'route' => 'projects.store']) !!}
<div class="modal-content" id="create">
@if($errors->has())
@foreach ($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
@endif
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Create Project</h4>
</div>
<div class="modal-body">
what happens is that after validation fails, it just redirects back to the view where it was called but I have to click again to trigger the modal
Upvotes: 2
Views: 1273
Reputation: 5752
Change this line
$('#create').modal('show');
to this line
(function($){
$('#createProject').modal('show');
})($);
id
you are using to show model is wrong here. And you are calling model open which may not work if DOM is not ready yet. So change line with above code, and it should work just fine.
Upvotes: 0
Reputation: 1165
Change this part:
@if (count($errors) > 0)
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
$('#create').modal('show');
</script>
@endif
To
@if (count($errors) > 0)
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
$(document).ready(function(){
$('#create').modal('show');
});
@endif
Why not submit the form with AJAX and return the error while the modal is still open.
Upvotes: 1