DEV Community

Discussion on: How to create modal in Laravel 8 and Laravel 6/7 with AJax

Collapse
 
faeza97 profile image
Faeza Mohammed

How to make the validation work inside your modal?

Collapse
 
kingsconsult profile image
Kingsconsult

which kind of validation are you talking about?

Collapse
 
faeza97 profile image
Faeza Mohammed • Edited

Laravel validator is not working inside the modal.
how to make it work? while inputting blank values it will just refresh the page after submit button.
blade



@if ($errors->any())
<div class="alert alert-danger">
    <strong>Whoops!</strong> There were some problems with your input.<br><br>
    <ul>
        @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
        @endforeach
    </ul>
</div>
@endif
Enter fullscreen mode Exit fullscreen mode

controller

 $request->validate([
            'name' => 'required',
            'introduction' => 'required',
            'location' => 'required',
            'cost' => 'required'
        ]);
        $project->update($request->all());

        return redirect()->route('projects.index')
            ->with('success', 'Project updated successfully');
    }
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
faeza97 profile image
Faeza Mohammed • Edited

Look when I erase the input values and submit the form again it won't show the error messages for laravel validator

dev-to-uploads.s3.amazonaws.com/i/...

Thread Thread
 
christophebossens profile image
Christophe Bossens

Probably a bit late, but if it doesn't help you it might still be useful for other people. I think you are mixing two approaches:

First, you can submit a form by sending a POST request to the server, and the server will then redirect you to a new page. In that case you can use the $errors directive to catch anything that went wrong during validation.

When using a modal, you submit your data using AJAX. This means you basically never leave the page you are on. The server will still return a response, but you have to process this in your JavaScript (specifically, in the AJAX error callback function).

You can find more info here: stackoverflow.com/questions/493337...