Now we have the contact farm.
We need to add code to process the farm when it's submitted.
As with other farms we're submitting using post and not specifying the action.
So the farm is submitted to itself.
So let's check with this at the top and then let's validate the values from the farm.
Let's get them into variables.
Then initialize an array to hold the validation error messages.
Then we can validate the values we'll use the filter method with the email filter to validate the email
address.
Adding a message to the errors array.
If it's invalid.
Will validate that the subject isn't empty and the same for the message.
Then if there are no validation errors we can send the email.
First though we need to display the validation error messages if there are any.
So before the form in the astm l..
If the error is array isn't empty.
We can loop around the errors in the array and display each error message inside an ordered list.
Now if I submit an empty farm we see the validation error messages and if I enter some values.
The field is validated correctly when the farm is redisplay.
However the previously entered values aren't re displayed.
To fix this.
First let's initialize these variables to empty values at the top.
Far when the page is first loaded and hasn't been submitted yet.
Then in the.
Value attributes of the e mail and subject fields we can display this value making sure we escaped the
value using the astm special chars function as these could contain values entered by the user.
Will do the same inside the text area element.
Now if I enter a couple of values but leave one out when I submit the farm we see the validation message
and the previously entered values I remembered.
We always need to validate data on the server like this.
But let's also do it in Javascript instead.
First let's identify the farm by adding an idea.
We'll use the j query validation plugin just as we did with the article farm.
We'll start by calling the invalidate method on the form element and then we add validation rules.
The email is required and should also be a valid email.
The subject is a required field and so is the message.
Now if I submit an empty farm we get the validation in the browser as I enter valid values.
The farm is validated
and the messages disappear when all is valid.
We can submit the farm and the server side validation passes too.