Laravel form requests for validation

13024
0

Form requests encapsulate validation logic in dedicated classes, keeping controllers thin and focused. Each form request extends FormRequest and defines rules() and optionally authorize() methods. The authorize() method checks if the user can perform the action, returning a 403 if false. Custom error messages go in the messages() method, while attribute names for error display use attributes(). Form requests support conditional validation—rules that apply only when certain conditions are met. The validated() method returns only validated data, preventing mass assignment vulnerabilities. Failed validation automatically redirects back with errors in session. I create separate requests for create and update operations since validation rules often differ.