‫Okay, so we left off with creating this allowed fields and when we submit the post data we can only
‫accept those fields.
‫We hardcoded the user id and we also applied that sanitize function to each value in the array.
‫Now we want to make sure that the required fields are actually filled in.
‫Now like I said in the last video, I don't want to make all the fields required.
‫So what we can do is create an array called required fields and specify which fields we want to be required,
‫meaning they have to have some data.
‫So let's say title.
‫Description.
‫Uh, let's see what else.
‫Let's do email.
‫So contact email just because on the on the submission page there's a button to send an email to apply.
‫And let's say city.
‫And state.
‫All right.
‫So those are the required fields.
‫Now what we can do is initialize an errors array.
‫And if any of these fields are blank then I want to add to the errors array a key of whatever field
‫it is title, description, etcetera.
‫And a value of a message that says title is required or description is required.
‫And then after that we can display it in the UI.
‫So to do that we're going to first have to loop through the required fields array.
‫So let's say for each required fields as field.
‫And then just to show you, we can inspect.
‫Our, let's say, new listing data.
‫And as a key we can use field.
‫Okay.
‫So if I do that.
‫And I resubmit this.
‫You can see these fields right?
‫Here are what I'm printing out from that for each.
‫So the title, description email city and state and email city and state are empty, which of course
‫we don't want.
‫So let's change this inspect to an if statement.
‫And let's pass this in empty.
‫So we'll wrap this whole thing in empty or and then here's where we can use our validation class.
‫We want to say if not validation that's our class.
‫And then the method string.
‫And we just want to pass into string this this new listing data field so we can pass that right in there.
‫We don't want a semicolon there.
‫This is an if statement.
‫So if that's true then we want to add on to the errors array.
‫So we'll say errors.
‫And then whatever the current field is.
‫That's empty will be the key.
‫And we want to set that to the message where I'm going to say, you see first, which will just make
‫the first letter capital and then the field name.
‫And then I'll concatenate onto that is.
‫Required.
‫Okay.
‫So we'll have an errors array.
‫And down here we have the inspect and die.
‫Let's just do errors.
‫And remember these three are empty right now.
‫So I'm going to resubmit.
‫And you can see we get an error message for each one.
‫This one has a key of email.
‫Email is required.
‫City.
‫City is required.
‫State.
‫State is required.
‫So now we can pass that into the view if those fields are empty.
‫Because if they're empty we just want to reload the view.
‫We don't want to submit to the database or anything like that.
‫So let's handle that.
‫Next we'll get rid of this inspect and die.
‫And instead we want to check for errors.
‫We'll say if not empty.
‫Errors.
‫So if that if that array is not empty then we want to reload view with errors.
‫Else.
‫Then we want to submit.
‫We'll say submit data.
‫But for now I'll just say echo.
‫Echo success, because we're not going to do that part in this video.
‫All right.
‫So we want to reload the view with errors.
‫So let's say load view.
‫The view that we want to reload is listing, slash, create, and then we want to pass in an array with
‫errors.
‫Uh, that should be in quotes.
‫So errors.
‫And pass in the errors array.
‫So now let's try this.
‫We'll reload this again, which we'll resubmit it.
‫Now we see the form.
‫We don't see the errors though because we haven't done anything with the errors we're passing in.
‫So now let's go into that view.
‫That create view.
‫And what I'm going to do is go above or let's go below the H2 right here this job info.
‫And then let's say PHP for each.
‫Actually first we want to check for the errors.
‫So if.
‫Want to say if is set.
‫Errors.
‫Right.
‫And then we'll end that up.
‫And if.
‫All right.
‫And then we'll loop through it.
‫So PHP for each.
‫Okay.
‫And we want to say for each errors.
‫As error.
‫And then what do we want to show for that?
‫Let's have a div with the class of message, class of BG red 100 and a class of MY3 for some margin.
‫And inside that we're going to show the whatever that error is.
‫Right.
‫Because the value is the message.
‫So we can use a shorthand here and just say error.
‫All right, now let's refresh this.
‫It has the submitted data and we see email is required.
‫City is required.
‫State is required.
‫Now, one other thing I want to do is if we come to this, I'm just going to come back to this page.
‫And if I submit a title, let's say test right.
‫And I try to submit.
‫So it doesn't say title is required because I had title, but it's not there anymore.
‫So if we keep it like this, then anything that wasn't anything that you put in the form is going to
‫go away and you have to retype it every time.
‫If you get errors, which can be a real pain.
‫So what we can do is pass in along with the errors we can pass in the listing data.
‫So we'll call it listing.
‫And that's going to be that new.
‫New.
‫Listing data.
‫And then what we can do is in the value for title.
‫For example, we can say value equals and let's say listing.
‫Title.
‫And now if I refresh and resubmit, that test is now there.
‫So we want to do this for all of these.
‫Now this one is a text area.
‫So we don't want value.
‫We just want to put it in the middle of the text area tags like that.
‫And just make sure you change it to description because it has to be whatever that field is.
‫All right.
‫Now I'm going to copy.
‫This value equals that and then go to the rest of these.
‫So here this one is going to be salary.
‫Okay.
‫Then we have.
‫Requirements.
‫This one is benefits.
‫Then we have company.
‫Then we have.
‫Address.
‫I know there's a lot of fields, but I wanted to make it, like realistic instead of doing like a little
‫to do with just a text, you know?
‫So usually you're going to have a bunch of fields in real applications.
‫So this one is state.
‫This one is phone.
‫An email.
‫Okay.
‫So let's actually just go back to the form and.
‫Whoa!
‫Undefined variable.
‫Wait a minute.
‫What did I do?
‫So listing is being passed in.
‫Oh, so it's looking.
‫This doesn't exist right now.
‫Right.
‫Because we didn't submit the form.
‫So what we have to do, we could do is set, or we can just use the null coalescing and we can do that.
‫So if I do that we'll watch the title.
‫Now that goes away.
‫So let's just add that on to all of these.
‫I don't know why I didn't think of that.
‫So that one.
‫Okay.
‫So it just means if it doesn't exist, it's just an empty string.
‫It's just nothing.
‫All right.
‫So if I save that and there we go.
‫So if I put stuff in here.
‫And save the data now stays in the form and I get the errors.
‫Now just let's just make sure that if we submit everything that's supposed to be submitted, that.
‫We get success, which we do.
‫Okay, good.
‫So in the next video, we'll finally get to actually inserting into the database.