تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Adding Request Validation0:00

Okay, we still have a lot to do, so let's dive right back in. We're still working on the form here. So we've handled the happy path to contribute a link. But now, what about if validation fails, we have to display errors, stuff like that. Okay. So back to our editor. We're going to go to our CommunityLinksController. And when we store a new link, here is where we can do our validation. Now as you may know, the controller class uses this ValidatesRequests trait, which means we have simple validation here.

Now as you may know, the controller class uses this validatesRequests trait, which means we have simple validation here. This, validate the request, and then I can set my rules. So if validation fails, it's going to throw an exception, and we will automatically redirect back with the errors, all with this line of code. Okay, so let's see what we got here. We have the title. That's going to be required. Next, we're still calling it link. So let's add that.

Displaying Validation Errors0:49

Next, we're still calling it link. So let's add that. And that should be required, and it should be a real URL. So we will add that. Okay, so we will add to this, but for now, let's go back and display those errors. So right down here is where we contribute a link. And here's what we could do. So let's do this. I want to spit out the HTML for the first title error if one exists. And I'm going to wrap it within a span and give it a class of error,

I want to spit out the HTML for the first title error if one exists. And I'm going to wrap it within a span and give it a class of error, something like that, and spit out the message there. So remember, you have access to an $errors variable no matter what with every view. But if we have populated the $errors, then we can display it here. Or you could put it within a list. That's pretty common too. We're just going to do it inline in this case. And let's keep it like that. Okay, so let's try it out.

And let's keep it like that. Okay, so let's try it out. Back to Chrome, and let's just submit this. Do I have a required attribute? Not yet. Okay, so if I contribute the link, it will submit. And yeah, we see it. We can style that if we want. Another option if you want to use Twitter Bootstrap, there are these helper classes. So for example, if I add hasError to the form group, let's try it.

Bootstrap Error Styling1:53

Another option if you want to use Twitter Bootstrap, there are these helper classes. So for example, if I add hasError to the form group, let's try it. And yeah, we can see that. But now in this case, it would be added every single time, right? You would want to do that dynamically either with JavaScript or something like errors.hasTitle. If so, then tack on this class. Otherwise, don't do anything. Let's see what we got here. So we run that, contribute the link, and we dynamically add that.

Let's see what we got here. So we run that, contribute the link, and we dynamically add that. We can also do, what is the class? It's some kind of help. I'm sorry, I don't use Bootstrap as much anymore. I think it's help-block. Yeah, so that will give you just an extra bit of styling that corresponds to the class. So for example, if we do hasSuccess, then it should be green. And it will update that as well. Yeah, kind of good to know.

Reviewing Current Workflow3:06

So let's go back and review the workflow we have right now. When we try to submit a link, we perform a quick bit of validation. Now again, don't forget we haven't added the channel yet. Like I said, I think maybe the next lesson we're doing that. So we're leaving a placeholder here to come back and handle it. And then next, we have just a bit of code to contribute an Article and associate it with the User. Okay. So I think that's going to do it for this lesson. We took care of validation. We still have so much to do.

We took care of validation. We still have so much to do. So if you're curious, we have some things like, well, actually, before we finish up today, we'll extract this to its own view or to its own partial. Then also we have things like this where we are referencing relationships, but we aren't eager loading them. So we want to make sure that we get away from the n+1 problem, and we'll take care of that. We're not even displaying the paginated links just yet. So don't worry. We still have quite a bit of work to do.

Extracting Form Partial3:57

So don't worry. We still have quite a bit of work to do. Okay. So to finish up, why don't we grab all of this, and we're going to extract it to a partial. How about community.addlink? And we might have an extra div there. Okay. So let's create that, addlink.play.php. And before we add it, if we refresh, it should disappear. But if we paste it in, it should come back, and it does.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟