Now we're displaying the article we want to edit.
We need to add the code to process the farm once it's submitted.
In the new article script when we submit the farm to create a new article we're getting the values from
the farm then validating them.
And if there are validation errors we're redisplay in the farm and displaying the error messages.
Again we don't want to repeat ourselves by copying this code as the validation rules for a new are existing.
Article will be the same.
So let's create a function to validate an article.
We will do this in here in the article include where we've already got the function to get an article.
Let's create a function called validate article.
Instead of relying on variables that already exist in the code when we call this function to encapsulate
this code as much as possible we'll Passim the values were validating as arguments to the function.
Then we can copy this validation code.
And paste it in a new function.
If one of these values is invalid then we're adding an error message to the errors array.
However we don't want to rely on a variable that might or might not exist outside of this function.
So let's add this variable to the top of our function and then at the end of the function we'll return
this variable
like that.
Finally let's be thoughtful programmers and add a p-h p up comment for function.
With the description list of parameters arguments and the return value.
Now we can use this new function in our new article script.
First we'll require the file where the function is here at the top then we can replace this validation
code.
With a call to the new function passing in the title content and published at variables and assigning
the function's return value to the errors array.
Let's just check this still works and it does.
Validating the farm and displaying the error messages.
Now we can use this same code in our edit article script.
We'll place it here after we've retrieved the article from the database.
So if the farm is submitted we'll get the values from the farm into these variables validate them and
if they're valid for now that's just outputs a message saying so and stop the script.
Let's give that a try.
If we submit the farm with valid values we get the message saying the farm is valid.
If we enter invalid data in some of the fields.
And submit the farm.
The farm is redisplay had along with the valediction error messages.
One more thing I want to mention is this air is a variable.
Earlier when we developed the new article farm we initialised the array variable at the top to avoid
an error when we first loaded the page.
However were not doing this in the edit script and we don't get the error.
This is because in the farm when we display the errors we are using the empty function to check if the
array has any errors or not.
In addition to returning true if the variable is an empty array.
This function also returns true if the variable doesn't exist.
So perhaps we don't need to initialize this variable up here in the edit script.
We don't need to do it in the new script either.
And in the browser when we first visit the new article page we don't get any errors.