views:

63

answers:

2

I have a form being validated in the following manner:

//Clear all variables
$formCheck = '';
$rep = '';
$name = '';
$department = '';
$location = '';
$email = '';
$phone = '';
$type = '';
$drink = '';
$notes = '';
$lastVisited = '';
$nextVisit = '';

$clean_formCheck = '';
$clean_rep = '';
$clean_name = '';
$clean_department = '';
$clean_location = '';
$clean_email = '';
$clean_phone = '';
$clean_type = '';
$clean_drink = '';
$clean_notes = '';
$clean_lastVisited = '';
$clean_nextVisit = '';

function validateRep($rep){
...some code...
}

$formCheck = $_POST["formCheck"];
$rep = $_POST["rep"];
$name = $_POST["name"];
$department = $_POST["department"];
$location = $_POST["location"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$type = $_POST["type"];
$drink = $_POST["drink"];
$notes = $_POST["notes"];
$lastVisited = $_POST["lastVisited"];
$nextVisit = $_POST["nextVisit"];

if (validateRep($rep)){
    $clean_rep = $rep;
}else{
    echo "Invalid Rep";
    exit();
}
//.....and so on......

I was wondering if it would be more efficient / cleaner to validate using an an array instead of individual variable? If so, how would I go about that, and how would I write the different validation functions all in one (eg. right now I have a separate function to validate each field), would it be possible with a loop through the array? i was experimenting and so far this is what I have:

$unclean['formCheck'] = $_POST["formCheck"];
$unclean['rep'] = $_POST["rep"];
$unclean['name'] = $_POST["name"];
$unclean['department'] = $_POST["department"];
$unclean['location'] = $_POST["location"];
$unclean['email'] = $_POST["email"];
$unclean['phone'] = $_POST["phone"];
$unclean['type'] = $_POST["type"];
$unclean['drink'] = $_POST["drink"];
$unclean['notes'] = $_POST["notes"];
$unclean['lastVisited'] = $_POST["lastVisited"];
$unclean['nextVisit'] = $_POST["nextVisit"];


$clean = array(
        'rep', 'name', 'department', 'location', 'email', 'phone', 'type', 'drink', 'lastVisited', 'nextVisit',
);

but I'm not sure how to proceed from here.

+1  A: 

use filter_input.

$rep = filter_input(INPUT_POST, "rep", FILTER_SANITIZE_STRING);
$name = filter_input(INPUT_POST, "name", FILTER_SANITIZE_STRING);
$department = filter_input(INPUT_POST, "department", FILTER_SANITIZE_STRING);
$location = filter_input(INPUT_POST, "location", FILTER_SANITIZE_STRING);

if (filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL))
{
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
}

Like that.

hopeseekr
+1  A: 

I would use something along these lines... Just coded this very quickly, basically you create validation functions that match the post fields and return true or false if the validation passed. e.g. validate_department, validate_type, validate_drink, etc. Will work if your post data doesn't have any strange characters in (which so far it doesn't)

$post_fields = array('rep',
                      'name',
                      'department',
                      'location',
                      'email',
                      'phone',
                      'type',
                      'drink',
                      'lastVisited',
                      'nextVisit'
               );

$validate = new Validate();

foreach ($post_fields as $post_var)
{
  if (isset($_POST[$post_var]))
   {
      $validate->validate_data($post_var, $_POST[$post_var]);
   }
}

if ($validate->all_fields_valid() === true)
{
  echo 'congrats, all validation passed!';
}
else
{
   echo 'oh no! error in validation process. please see below errors: <p>' .
         $validate->get_error_msg() . '</p>';
}

And the validate class... Use $errorMsg to see the error messages should you run into any issues

class Validate
{
  var $valid = 0,
      $error = 0,
      $errorMsg = '';

  function validate_data($var, $data)
  {
    if (method_exists($this, 'validate_'.$var))
    {
      if (call_user_func(array($this, 'validate_'.$var), $data) === true)
      {
        $this->valid++;
      }
      else
      {
        $this->throwError('validation for: "'.$var.'" was not considered valid');
      }
    }
    else
    {
      $this->throwError('validation function for: "'.$var.'" does not exist');
    }
  }

  function throwError($msg = '')
  {
    if ($msg) $this->errorMsg .= $msg . '<br/>';
    $this->error++;
  }

  function all_fields_valid()
  {
    if (!$this->error) return true;
    return false;
  }

/***********************************************
*************************************************
 Custom validation functions go below here
   Function format: validate_<postFieldName>
   Returns: true or false if the data passed is valid or not
*************************************************
*************************************************/

  function validate_type($type)
  {
    if (is_numeric($type)) return true;
    return false;
  }

  function validate_lastVisited($data)
  {


  }

  //etc...............

}
Gary Green
Im getting an error on "$this->error++;", is there supposed to be a semicolon on the line above?
Murtez
Yes, sorry that was my error. I'll correct it when I'm back on my pc. Other than that does everything work? :) glad I could be of help
Gary Green
im actually having some problems getting it to work, I was attempting to modify the code to see if I can figure it out but its taking longer than expected :), anyways, here are some errors: the for loop should is foreach probably, there is also a "Warning: Missing argument 1 for Validate::validate_type() in /home/compudat/public_html/vt/scripts/validation.php on line 48" error, at this point the script stops.
Murtez
Your right again I was being too hasty. I've corrected the for to foreach and the missing argument should be correct as well. Please give it a second try. Anymore issues/suggestions I'll be happy to help or talk you through what each function does :)
Gary Green
Fatal error: Using $this when not in object context in /home/serverra/public_html/vt/scripts/add_item.php on line 62
Murtez
Right I'm back at my PC. `$this` shouldn't of been where it was I've corrected it to `$validate->` also corrected a few more bugs in $errorMsg. Tested the function now and it SHOULD *fingers crossed* work!
Gary Green