FAQ: Introduction to PHP Form Validation - Using Options with filter_var()

This community-built FAQ covers the “Using Options with filter_var()” exercise from the lesson “Introduction to PHP Form Validation”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn PHP

FAQs on the exercise Using Options with filter_var()

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Using Options with filter_var()

Hello, i was doing the lesson and finished it well.

But when i tested my software i realized that it was returning * Invalid Input month and day when i inputed days or months like this:

02/07/1994

But it worked when i inputed like this:

2/7/1994

How should i fix this?
Thanks in advance!

4 Likes

You can add (int) just before $_POST[$type] in the function validateInput() to cast the strings (“02”, “07”, and so on) to integers.

function validateInput($type, &$error, $options_arr) {
  if (filter_var((int) $_POST[$type], FILTER_VALIDATE_INT, $options_arr)) {
...

filter_var() internally casts the input into a string. So for example “02” is converted to the integer 2 by (int) and then to the string “2” again to be validated.

1 Like

HI there. Bit confused with this part of filter_var form validation lesson.

We have this code…

function validateInput($type, &$error, $options_arr){
  if (!filter_var($_POST[$type], FILTER_VALIDATE_INT, $options_arr)) {
 
  } else {
 
  } 
}

It is the variable $type that is bothering me. I see what it does, it’s assigned to the $_POST[$type] from each kind of input. But I don’t see were it’s assigned to type value.

Month: <input type="number" name="month" value="<?= $_POST["month"];?>">
	<span class="error"><?= $month_error;?>		</span>
  <br>

I have looked all through the code and I can’t see $type = ‘SOMETHING’ anywhere. Since we’re writing the function, how can it have any predefined arguments? I’m stumped by this to be honest.

Step 3 of the instructions states:

  • The first parameter, $type, will be a string representing the input which is being validated (eg. "month", "day", or "year")

So $type is the parameter name and the actual argument assigned to this parameter will be one of the three strings mentioned above.

In our form, we have different input fields. Let us look at one of the fields mentioned in your post.

Month: <input type="number" name="month" value="<?= $_POST["month"];?>">
  <span class="error"><?= $month_error;?>   </span>

The type="number" is just HTML specifying what kind of input is to be displayed on the screen. This type has nothing to do with the function parameter $type (you can find a list of HTML input types here) What is important is name="month". Basically, when you submit the form, a $_POST array is sent back with lots of information. The names of our input fields will be present in this array as keys, while whatever we entered in the input fields will be the values assigned to these keys. For example, if you entered 7 in the month field and 22 in the day field, then in the $_POST array you will find the key/value entries [..."month" => 7, "day" => 22, ...].

When calling out validateInput function, the first argument we are going to provide is going to be one of the strings “month” OR “day” OR “year”. This argument will be assigned to the parameter $type. In step 4 of the instructions, we are asked to uncomment some code such as:


if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Uncomment the code below:
    $test_month = validateInput("month", $month_error, $month_options);

This is where the argument “month” is being passed to our function. Within our function, the string “month” will be assigned to the parameter $type. In our if condition and the call to filter_var, the expression $_POST[$type] will become $_POST["month"]. The value assigned to the “month” key in our $_POST array will be retrieved and then sent to FILTER_VALIDATE_INT.

2 Likes

Yeah, I still don’t see the link. I just don’t see where $type is defined, is it a predefined parameter of a php function? Am I being a luddite for needing to see $type = SOMETHING? Without it I just don’t understand how the code knows what it means.

No, $type is not predefined.
When we define a function (not call it, just define it), then we want a degree of abstraction and re-usability. Even if the function is defined at the top of our page, the code within the function will not run until the function is called. So, in the function definition
function validateInput($type, &$error, $options_arr)
the parameters $type, &$error and $options_arr don’t have any actual values assigned to them yet. When a call to this function is made, then we trust that whoever has called the function will provide the actual values that are to be assigned to these parameters.
And indeed that is exactly what happens when these calls are made:

$test_month = validateInput("month", $month_error, $month_options);
$test_day = validateInput("day", $day_error, $day_options);
$test_year = validateInput("year", $year_error, $year_options);

Now, we are providing the actual values that are to be assigned to the parameters. The first value in the function call will be assigned to the first parameter, the second value to the second parameter and the third value to the third parameter. So in the first function call, the first value which is just a string i.e. "month" will be assigned to the first parameter i.e. $type. That is equivalent to what you are suggesting i.e. explicitly seeing $type = SOMETHING. When we make the function call, “month” is assigned to $type.
Perhaps, I am misunderstanding your question.

1 Like

Thank you!

I see it now, I was missing the connection of the function being called with those arguments. I think I am just finding it difficult sometimes to sort through the php and the HTML together, some more time taken going over each line would help, but I think that time I just couldn’t see the wood for the trees.

Really appreciate you taking the time to help.

1 Like