Skip to main content

Form Validation and Multi-Submit Prevention

Premium
4 min read

Our form doesn't have validation, so let's add it in this lesson.


Back-End Validation

First, let's add the validation on the back-end. For this, we will use Form Request.

php artisan make:request StorePostRequest

app/Http/Requests/StorePostRequest.php:

class StorePostRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
 
public function rules(): array
{
return [
'title' => 'required|min:3',
'content' => 'required',
];
}
}

app/Http/Controllers/PostController.php:

use App\Http\Requests\StorePostRequest;
 
class PostController extends Controller
{
// ...
 
public function store(Request $request): RedirectResponse
public function store(StorePostRequest $request): RedirectResponse
{
Post::create([
'title' => $request->input('title'),
'content' => $request->input('content'),
]);
Post::create($request->validated());
 
return redirect()->route('posts.index');
}
}

We don't need anything else on the back-end.


Showing Errors on the Front-End

For the front-end, there are a couple of...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 09 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

No comments yet…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.