In this episode, we tackle an essential task for any web application: validating the login form. Now that we have session flashing set up, it’s finally possible to provide users with helpful feedback when they submit invalid data.
We start by pulling in the "respect/validation" library using Composer, although you can swap it for another validation library if you prefer. We alias the validator for convenience and define validation rules for the email and password fields—making sure the email is both not empty and a valid email address, and the password isn’t empty.
You’ll get to see how the validation library throws exceptions when the rules are broken. We wrap this with a try-catch block, extract error messages from the exception, and—using our session's flash bag—store them for display. Then we redirect back to the login form, where we pick up and show those errors using Twig. You’ll also see how to present these messages in the UI next to the respective form fields.
By the end of the episode, you’ll know how to flesh out meaningful, per-field validation for login and, by extension, other forms (like registration). We also briefly touch on better ways to pass errors to your templates, which we’ll explore later. For now, you have everything you need to make login validation actually help your users out with clear messages.