3.4 Custom Form Validation
You might also begin to see some holes in our calculator logic that will need to be addressed. In this lesson, we'll handle issues such as dividing by 0 and making sure that the user always enters a numeric digit in the input boxes.
Any time you start to take input from the end user, you always need to be careful to make sure that the application is handling not only the happy scenarios, but also the bad ones. So, in this lesson, I'll show you how to handle custom validation on your form submissions.
1.Introduction2 lessons, 05:24
2.Drupal Module Basics6 lessons, 48:45
3.Create the Calculator Module4 lessons, 28:15
4.Conclusion1 lesson, 02:31
3.4 Custom Form Validation
Now, I'm gonna show you a little bit on how to do some custom validation within our form in Drupal 8. So, I'm gonna come back over here into my application into my calculator form. And into the validateForm function, which we really haven't touched to this point. But now it's time. Now, remember the two scenarios that we talked about we needed to handle. The first scenario being if the user selects the divide operation. We should not allow that second digit to be a 0. That's just an invalid mathematical operation. We don't want that to happen. And then the second scenario we have is where the value that is input into either one of the digit text fields is something other than a numeric value. So we wanna be able to check for that as well. So let's start with the divide by zero scenario. So the first thing that we need to do is we need to check on that operation again. So we're once again gonna go into form_state, and we want to get the same value. And I'm already doing these operations, so [LAUGH] actually, I'm just going to copy these from down here. Let's paste them up here, and I will need this first and second digit eventually. So let's go ahead and keep it that way. So the first thing that I wanna check is, is the operation and divide and is the second digit a zero? And in that case, that's bad. We don't wanna do that. So in this case, we'll say if the op = divide and the second_digit = 0. So this is that first bad scenario that we talked about. Then what we can do is we can back into the form state and we can use another function called setErrorByName. So what this allows us to do is to set an error in the form. And you'll see it in the form when we actually try to complete this operation. We can specify the name of the input field within the form that is an error that doesn't validate. In this case, it's going to be that second digit. So we're gonna say second_digit. And then you can specify a text string that you're going to output to the end user, which is going to be something to the effect of, You are not allowed to divide by 0. Something like that, so we'll do a semicolon here. Let's go ahead and save that. So that's our first scenario. Let's go ahead and try that out. So, we'll come back in here, we'll clear our caches. Then, we can refresh our calculator, and now let's go ahead and try that scenario again. So, let's, this time, say maybe 8 and I want to divide by 0. And in this case, I'm gonna hit Submit. And I'm going to get an error box up here now that's going to say, You are not allowed to divide by 0. And you see down here that the input field that fails the validation is now highlighted in red. So you can immediately see where the problem is. So now that we've taken care of that scenario, we should be good. I can switch this to 1, hit Submit and I get 8. All right, so that's good. So now the other scenario is, once again, where I want to do something like I wanna do t divided by 1 and hit Submit. That breaks everything, and I don't want that to happen. So let's come back over here to our form again. And this is now another scenario that we need to handle. So we need to make sure that both first digit and second digits are valid numeric values. So how can we do that? So we can say, if not, there is a built in function in PHP called is_ numeric, and we can pass in first_digit. So if it's not numeric, then we're gonna do something very similar here on our form_state. So let's go ahead and copy that, paste it down here. And in this case, this is going to be first_digit. And then we'll say, Please enter a valid number for first digit, just like that. And we'll save that. And then we're gonna do the same operation, or nearly the same operation for second digit. So we'll say $second_digit. And then we'll do the same thing in here, this is going to be second digit. Please enter a valid number for Second Digit. And the reason I'm keeping these things separate, you might be saying to yourself, well, why not do ifs and if elses or something like that? Or maybe try to be a bit more elegant with this? So basically the reason I'm doing it this way is because if the user does all of these things, all of these bad operations at the same time, I want all of those errors to appear. I want all the invalid inputs within the form to be highlighted. I wanna show them everything. I don't wanna say the first digit is bad, even if the second digit's bad, and then have it just highlight the first digit, have them fix that. And then have to resubmit and go look at the second one. I just want all of the errors to show up all at once. So let's make sure everything is saved. We'll come back over here and clear our caches once again. And then we'll come back and we can refresh our form. Actually, we won't do that. Let's go ahead and clear these out, and let's just go and refresh this. There we go. Okay, so now we can go back to that first scenario where we were dividing by 0. So let's say 6 divided by 0. So we know this should be handled, so we say, you are not allowed to divide by 0. That's good. What if I wanted to say, e divided by 0? Well, now I'm getting, You are not allowed to divide by 0 and please enter a valid number for First Digit. So now we see that here, and now I can come down here and change these values. I could maybe say, Multiply instead, since we know the divide by 0 scenario was handled. So I could say j and a. Let's go ahead and hit Submit. Please enter a valid number for First Digit. Please enter a valid number for Second Digit. So as you can see here, all of these validation seem to be firing just as we would expect them. So now you have seen how we can create a basic form using the form's API within Drupal 8. How we can take those values, how we can do validations against them. How we can submit them into our form class, and how we can handle those submissions in different ways. And we can redirect out to different areas or different routes within our module, and handle them all in different ways.