The PHP Forms Class: Validation

This is the third part of the PHP Forms Class. We have already looked at the Forms Class PHP file and the JavaScript/AJAX file. Now it’s time for the actual validation PHP file. You might wonder why we will use a PHP file to validate our fields? If you remember we save a $_SESSION for each form we create and using this session object we are able to validate our fields accordingly to your validation rules we decided when creating the form. The validation PHP page is not a class. It contains 3 functions:

  • validate_field()
  • validate_value()
  • check_value()

From the JavaScript page we are calling the validation.php page with three get variables:

  • form: holding the name of the form we want to validate (makes it possible to access the correct $_SESSION object).
  • field: is the name of the current field we are going to validate.
  • value: the value found in the active form.

NOTE! This version of the validation script is no longer used!

We begin with the code answering to the request from the JavaScript file. First we assign the incoming get variables to some local variables. We see if we can find our form and the validation rules for the field. If everything is OK so far we send our value and field to the validate_field() function.

Code describing the call to the validate_field() function

The validate_field() function takes two parameters, a $value and a $field. In our case the $field parameter will contain everything we provided when we created our form object. We set up a few local variables $options, $type and $name, which are all found in the $field parameter. Then we see if we can find any ‘required’ key in the options array, and if it is true. If it is set and true we go another step in. We see if the length of the value is equals with 0 and that we don’t have a ‘exist’ key OR that the value equals 0 and the type of the field is ‘checkbox’. If any of these two are valid, the specific field is required and we will echo that out as an error. The reason we do these test is:

  • In the first expression the exist key is only available for blobs. If a blob already exist in the database we don’t want to supply it every time we edit the field. The ‘exist’ key is set when we have a value in the database and don’t want to remove that.
  • The second expression is used when we have checkboxes. Remember the JavaScript function calculating all the checkboxes checked with a specific name? The return from that function is the value in this case. If none of the checkboxes is checked and the field is required, we will echo the error.

After the ‘required’ key check we take a look at the ‘format’ key and make sure that it exists, we also make sure we haven’t already printed any error and that the length of the field is not equal to 0. If the boolean turns into true we are stepping into the validate_value() function. If we have found any error we will return the error list and echo that one out. Otherwise we will return false.

Code describing the validate_field() function.

Next up is validate_value(). This function takes three parameters, $value, $options and $name. We assign some local variables with the value from the parameters. Depending on the format of the field we use a switch/case loop to validate the content of the field. For example a ‘number’ can only match 0-9 chars. When we find our matching format we are using the check_value() function. The only case diverting from this is the ‘file’ case. Here we have a few extra checks which is available for the file input field. We are able to set a upload folder, allowed extensions, max size to name them.

Code describing the validate_value() function.

The last function, check_value() is simple. It takes 6 parameters: $value, $regexp, $name, $min, $max and $type. What we do here is to match our string with the incoming regexp. If the format isn’t valid we will mark that. If a min or max key is supplied we check those validation rules to. Finally we will return the whole error string, if any errors where found.

Code describing the check_value() function

And that’s it! That’s the end of this presentation and the end of the PHP Class series #1. As always, your comments and suggestions are welcome! The full Forms Class can be downloaded below!


About this entry