Learn
Introduction to PHP Form Validation
Using Options with filter_var()

The filter_var() function accepts an optional third argument that allows us to fine-tune the operation of a given filter. This argument, often called $options, takes the form of a nested associative array.

For example, the $options argument can help us validate that an integer is within a specified range when using the integer validation filter FILTER_VALIDATE_INT. To do this, we set $options to a nested array containing the"min_range" and "max_range" keys in a specific format, shown in the following example:

function validateAdult ($age){ $options = ["options" => ["min_range" => 18, "max_range" => 124]]; if (filter_var($age, FILTER_VALIDATE_INT, $options)) { echo("You are ${age} years old."); } else { echo("That is not a valid age."); } } validateAdult(18); // Prints: You are 18 years old. validateAdult(124); // Prints: You are 124 years old. validateAdult(8); // Prints: That is not a valid age. validateAdult(200); // Prints: That is not a valid age.

In the code above, we wrote a function validateAdult() which takes in an $age integer argument. We then used the filter_var() function to validate the integer was between 18 and 124 (inclusive) by using the FILTER_VALIDATE_INT filter and an $options argument with the value ["options" => ["min_range" => 18, "max_range" => 124]].

You can see which filters accept options in the PHP manual.

Instructions

1.

Take a minute to familiarize yourself with the provided code. We declared four variables: $message, $month_error, $day_error, and $year_error. Note: these correspond to keys in the $_POST array.

  • These error variables provide the inner HTML to the "error" class <span> elements that follow each input on the form.
  • The $message variable provides the inner HTML of the <p> element after the form.

Right now these variables are all assigned empty strings. Notice how when you submit the form, the value attribute of each input field persists, but you don’t receive any other feedback. You’ll change that in the next tasks!

2.

We’re going to be using filter_var() function with the FILTER_VALIDATE_INT validation filter and options arrays.

Create the three options arrays you’ll need:

  • Create a variable $month_options. This variable should be assigned an array with a key of "options". The value of the "options" key should be an array providing an acceptable integer range for a valid month: a month can be between 1 and 12 (inclusive).
  • Create a variable $day_options. This variable should be assigned an array with a key of "options". The value of the "options" key should be an array providing an acceptable integer range for a valid day: a day can be between 1 and 31 (inclusive).
  • Create a variable $year_options. This variable should be assigned an array with a key of "options". The value of the "options" key should be an array providing an acceptable integer range for a valid birth year: we should consider a valid birth year to be any year between 1903 and the current year (inclusive).
3.

To save us from some repetitive code, you’re going to write a function validateInput(). This function will make sure that a user input adheres to specified validation requirements.

Your function should have three parameters:

  • The first parameter, $type, will be a string representing the input which is being validated (eg. "month", "day", or "year").
  • The second parameter will be an error message passed by reference (&$error). This variable represents the error message that corresponds to the input being checked.
  • The third parameter, $options_arr, will be the options array which corresponds to the current input.

Your function should invoke the filter_var() function with the relevant user input from the $_POST array, a filter for validating inputs, and the provided options array.

If the input is deemed invalid, your function should reassign the $error to "* Invalid ${type}" and return FALSE. Otherwise, it should return TRUE.

4.

Great! Now that your function is written, we can actually use it! Uncomment the code after $test_month, after $test_day, and after $test_year. These lines will assign each of those variables to the result of invoking your validateInput() function with each input type.

Once those are uncommented, the form should be working correctly! Test the form with valid and invalid inputs to make sure.

Folder Icon

Take this course for free

Already have an account?