Jump to content

Why add $_POST at the end of a validation function?


eldan88

Recommended Posts

Hi,

 

I was going to through a PHP tutorial, and came across a form validation user defined function . Below is an example..

 

function check_required_fields($fields){
$field_errors = array(); 
foreach ($fields as $requried_fields) {
if (!isset($_POST[$requried_fields]) || (empty($_POST[$requried_fields]) && $_POST[$requried_fields] != 0))
if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0))
{
 $errors[] = $requried_fields;
     }
   }
   return $field_errors;
}

 

When the user defined function was called they added an $_POST and the end of the user defined function. Here is a the code snipped of how it looks

 

$errors = array();

	// perform validations on the form data
	$required_fields = array('username', 'password');
	$errors = array_merge($errors, check_required_fields($required_fields, $_POST));

 

If the validation can be preformed with out the $_POST, why bother inserting it?

 

Thanks!

 

Link to comment
Share on other sites

Hello eldan88. Since check_required_fields() is a custom function with its own paramaters made by the creator, it will only required those paramaters unless otherwise stated. So having $_POST as the second paramater when the function is being called is pointless and does nothing, you can remove it if that's what you're getting at.

Link to comment
Share on other sites

Hello eldan88. Since check_required_fields() is a custom function with its own paramaters made by the creator, it will only required those paramaters unless otherwise stated. So having $_POST as the second paramater when the function is being called is pointless and does nothing, you can remove it if that's what you're getting at.

 

I see ... thanks  :D

Link to comment
Share on other sites

I'm trying to understand what this set of conditions does:

 

if (!isset($_POST[$requried_fields]) || (empty($_POST[$requried_fields]) && $_POST[$requried_fields] != 0))

 

Check if it's not set OR is empty && does not equal 0.  The last 2 cancel eachother out.  You're checking to see if it's empty and also if it's not empty.

 

And nowhere in that function is $fieldname set, yet you're using it in your second condition block.

Link to comment
Share on other sites

if (!isset($_POST[$requried_fields]) || (empty($_POST[$requried_fields]) && $_POST[$requried_fields] != 0))

 

Check if it's not set OR is empty && does not equal 0.  The last 2 cancel eachother out.  You're checking to see if it's empty and also if it's not empty.

 

empty() will return true if there is a value of 0. So, if you want to allow the value "0" you can't rely on empty().

Although since there is no strict comparison to 0, it is still flawed... since empty == 0. It should be:

empty($_POST[$requried_fields]) && $_POST[$requried_fields] !== 0)

Link to comment
Share on other sites

if (!isset($_POST[$requried_fields]) || (empty($_POST[$requried_fields]) && $_POST[$requried_fields] != 0))

 

Check if it's not set OR is empty && does not equal 0.  The last 2 cancel eachother out.  You're checking to see if it's empty and also if it's not empty.

 

empty() will return true if there is a value of 0. So, if you want to allow the value "0" you can't rely on empty().

Although since there is no strict comparison to 0, it is still flawed... since empty == 0. It should be:

empty($_POST[$requried_fields]) && $_POST[$requried_fields] !== 0)

 

Ya, I know all that :P

 

I don't see why a username or password would have a value of 0 though, which is why I don't believe his/her intentions were to allow a value of 0.  Wouldn't make any sense, anyways.  Feel like I'm talking myself into a circle lol.

 

Assuming zero(0) is not an option, that statement should only be checking the isset() and empty() on $_POST[$required_fields].

 

Without proper parenthesis to give distinction on which conditions should fire when, you're left with the natural order of precedence (&& before ||).

 

Having " .. && $_POST[$required_fields] != 0 .. " in conjunction with the isset() and empty(), and having && take precedence over the || you're left with, basically (rewritten):

 

if ((empty($_POST[$required_fields]) && ($_POST[$required_fields] != 0)) || !isset($_POST[$required_fields])) {

 

Which, in Layman's terms, is like saying: $_POST[$required_fields] must be empty AND must contain a value.

 

I'm basing my thoughts off of the code as I see it, not how I try and decode it in my head.

 

Had they used a strict comparison on the final condition, it would have been easier to see what was going on and their intentions of that condition.

 

Don't take this as a pissing contest, I just like a little back-and-forth problem solving from time-to-time :D

Link to comment
Share on other sites

I don't see why a username or password would have a value of 0 though, which is why I don't believe his/her intentions were to allow a value of 0.  Wouldn't make any sense, anyways.

 

I believe the function is supposed to be a general purpose function, not limited to just this snippet.

Link to comment
Share on other sites

the 0 was a radio button value for visible or not visible.

 

1 for visible

 

0 for not visible

 

I took this snippet on a little tutorial project i am working on. Since 0 is considered empty i make sure its also not equal to 0 in order for it to validate.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.