1

I am using the following code to validate integer input fields in my form:

if (isset($_POST['MaxiVegXP']) && ctype_digit($_POST['MaxiVegXP']))
{
    $MaxiVegXP = $_POST['MaxiVegXP'];
} else {
    $MaxiVegXP = FALSE;
}

I have another 20 or so similar form input fields. Is there a quicker way of doing this with a PHP loop? I'd rather not do the above for another 20 input fields :-)

4 Answers 4

3

I would do something like @Simply Dread, but with a slight improvement, so I could explicitly indicate which fields needed to be fixed:

$validFields = array('field1' => true, 'field2' => true, 'field3' => true, ..., 'fieldN' => true);

foreach ($validFields as $field => $valid) {
    if (!isset($_POST[$field]) && !ctype_digit($_POST[$field])) {
        $validFields[$field] = false;
    }
}

With this information, I can now show errors on the appropriate fields instead of only saying that there is a problem.

Sign up to request clarification or add additional context in comments.

Comments

1

You could iterate over all fields for example:

foreach ($_POST as $key=>$value){
    if (isset($_POST[$key]) && ctype_digit($_POST[$key])) {
        $$key = $value;
    } else {
        $$key = FALSE;
    }
}

But I would instead put the code in a function and call the fuction excplicitly for every post variable:

function isDigit($value) {
    if (isset($value) && ctype_digit($value)) {
        return true;
    }
    return false;
}

$MaxiVegXP = isDigit($_POST["MaxiVegXP"]) ? $_POST["MaxiVegXP"] : false;

Comments

1

An option would be to create an array of the field names and loop over it. Doing it this way will ensure all fields have to be set and are digits. Check the validate variable afterwards and you'll know if it was successful. :-)

$fieldArray = array('fieldOne', 'fieldTwo', 'fieldThree');
$validate = true;

foreach ($fieldArray as $field) {
    if (!isset($_POST[$field]) && !ctype_digit($_POST[$field])) {
        $validate = false;
    }
}

4 Comments

That's an excellent idea! After the if should I also add an else to OK that field? So maybe if (!isset($_POST[$field]) && !ctype_digit($_POST[$field])) { $validate = false; } else { $field = $_POST[$field]; } ?
Nop, no need to do that. If you want to use all 20 fields after the validation than check if validate is still true. :-)
Your welcome, please accept an answer given that best solved your question so other users quickly know what solves it. :-)
Yes, will do shortly, just implementing a test right now :)
1

Is there a quicker way of doing this with a PHP loop?

there's a quicker to do this without a PHP loop:

I would use filter_input_array. This code will assign null to the value if it does not pass the filter. It's quite practical, you just have to add the name of the variable and it's desired filter in the array.

$vars = filter_input_array(INPUT_POST,array (
  'MaxiVegXP'=>FILTER_VALIDATE_INT,
  'myVar'=>FILTER_DEFAULT, 
// or FILTER_VALIDATE_INT. you can also define validator callbacks if need be
));

here you define the keys you wish to get from the input ($_POST in this example, but you can use INPUT_GET to get variables from the $_GET superglobal). You can also define more advanced options in the array to define a min or max range for your inputs, for instance

$vars = filter_input_array(INPUT_POST,array (
  'MaxiVegXP'=>array(
    'filter'=>FILTER_VALIDATE_INT,
    'options'=>array('min_range'=>1, 'max_range'=>10),
  ),
));

the usefulness is that you don't have to verify manually for each key if they exist and what type they're of, filter_input_array takes care of that once you've defined your array of accepted value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.