This episode is for members only

Sign up to access "Build Your Own PHP Framework" right now.

Get started
Already a member? Sign in to continue
Playing
40. Manually adding validation errors

Episodes

0%
Your progress
  • Total: 4h 45m
  • Played: 0m
  • Remaining: 4h 45m
Join or sign in to track your progress

Transcript

00:00
OK, so let's cover adding manual validation messages when, for example, we log in and the credentials are incorrect. First of all, I've just realized that this copy and paste kept the email.
00:13
So we're going to go ahead and just change that to password. OK, let's go over and try and hit that again. And yet there we go. We get password must not be empty. OK, so if I enter my email address, but I fill in an incorrect password, we're just redirected back at the moment. All we need to do in the logging controller is just flash a message in here.
00:33
So we're going to do exactly the same thing that we did before and manually flash a message to errors. But what we're going to do is create our own structure to this, because we always know that we are using an email key to show the email error. We can just add a custom message in here. So we could say something like could not sign you in or log you in with those details.
00:59
And let's head over and try this out. So the validation works as usual. But if I enter an incorrect password, you can see that we still get this flashed with the validation. Of course, what you could also do is flash that general global message at the top if you wanted to change this up. But you can basically now just add any of these to your errors message back and have whatever you need to be shown in each of the fields.

Episode summary

In this episode, we're diving into how to manually add validation errors in our login process—specifically when a user enters incorrect credentials. You'll see how, after a quick code tweak (we fix a copy-paste error with the password field), the validation logic checks that fields like the password aren't empty and shows the appropriate message if something's missing.

But what about when the credentials are incorrect? Right now, if you try to log in incorrectly, nothing much happens except a redirect. We'll improve that by flashing a friendly error message—like “could not sign you in with those details”—for the user to see. We show you how to structure these custom error messages so you can control what shows up for each scenario, not just the built-in validation responses.

By the end, you'll have the tools to flash manual validation errors anywhere you need them, giving you total control over the feedback your users get at login. Plus, we chat about displaying global error messages if you want to put something at the top of your forms instead of just per-field. Super useful for making your forms user-friendly!

Episode discussion

No comments, yet. Be the first!