When we're editing an article first we validate the values from the farm using the validate article
function which is defined in the article.
Include scripts in the includes folder.
Now we have an article class though.
It makes more sense for this to be in there.
So let's take this out of here.
And paste it at the bottom of our article class.
When we call this function in the edit article script we passing the values you want to validate and
we get a list of errors back.
However we were assigning the values from the farm.
In other words the values we want to validate to the object's properties.
So instead of passing in these values to the function we can access them directly in the method.
While we're here let's also rename the method like that.
Let's take this.
There is variable out of here and make it a property of the class.
Placing it at the top after the other properties.
Then back in the method instead of accessing variables we need to access properties.
So everywhere we're accessing title
content and published that we need to out of the current object variable this before it.
The same with the errors variable.
Now this is a property we need to do the same for each time we access it.
Now that the array of areas is a property we can access it as such.
So we no longer need to return it from this method.
Instead let's return a billion value that denotes whether the properties are valid or not which we can
do based on whether this array is empty or not.
Like that.
And let's not forget to change the comment to.
Now we're currently calling this method from the edit articles script before we call the update method.
Now we're in the same class though.
We can just call this method directly from the update method.
So as we know into call this method from outside the class will make it protected like that.
Then inside the class in the Update method before we start the update cold we'll call the validated
method.
And if it returns true we can update as before if it returns FALSE.
Then we just returned false from the update method.
Then in our edit article script we can remove these two lines where we call the validation function
and check the areas array.
As you can see this also makes our Carlyn cold simpler and easier to read.
We can also remove this require statements as were no longer using the functions in it.
There's one more thing we need to do and that's in the article form when we're displaying errors.
Now we need to access the areas property of the article object instead.
In the if statement and in the follow up.
Let's give that a try.
If I edit this article and first enter some in data and submit the form the validation still works and
the errors are displayed.
If I correct these errors and submit the form validation passes and we can save the changes that's before.