代码之家  ›  专栏  ›  技术社区  ›  Mankind1023

验证数组中窗体的最有效方法是什么?

  •  2
  • Mankind1023  · 技术社区  · 15 年前

    我有一个表格正在通过以下方式验证:

    //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......
    

    $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',
    );
    

    但我不知道如何从这里开始。

    2 回复  |  直到 15 年前
        1
  •  1
  •   Gary Green    15 年前

    我会用这样的方法。。。只需非常快速地编写代码,基本上创建与post字段匹配的验证函数,并在验证通过时返回true或false。e、 如果你的帖子数据中没有任何奇怪的字符(到目前为止还没有),那么validate\u department、validate\u type、validate\u drink等就可以了

    $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>';
    }
    

    验证类。。。如果遇到任何问题,请使用$errorMsg查看错误消息

    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...............
    
    }
    
        2
  •  1
  •   Theodore R. Smith    15 年前

    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);
    }
    

    就像那样。