如何使用面向对象技术验证 PHP 中的表单字段

Ami*_*eem 5 php forms oop validation

我创建了一个类“validate”来验证两个字段,即“firstname”和“lastname”。它工作不正常,当字段为空时显示错误,但是当我提交包含非空字段的表单时,错误仍然存​​在。如何在表单提交时执行此操作?

 <?php

  class validation {

  public $firstName, $lastName, $errorFirstName = '', $errorLastName = '';

  function __constructor($fName, $lName){
    $this->firstName = $fName;
    $this->lastName = $lName;
  }

  function check(){

      if($_SERVER["REQUEST_METHOD"] == "POST"){
        if(empty($this->firstName)){
        $this->errorFirstName = 'First name is required';
       } else {
        $this->errorFirstName = 'Input is okay';
       }

     if(empty($this->lastName)){
         $this->errorLastName = 'Last name is required';
      }  else {
       $this->errorLastName = 'Input is okay';
      }
    }
   }
  }

 $obj = new validation($_POST['firstname'], $_POST['lastname']);
 $obj->check();
 $errorF = $obj->errorFirstName;
 $errorL = $obj->errorLastName;

 ?>

  <!DOCTYPE html>
  <html lang = "en-US" dir = "ltr">
   <head>
    <title>Home</title>
    <meta charset = "UTF-8"/>
   </head>
   <body>
   <form method = "POST" action="<?php echo $_SERVER["PHP_SELF"]?>">
    <label>First Name: </label>
    <input type = "text" name = "firstname" placeholder = "John"/>
    <p class = "error"><?php echo $errorF;?></p>
    <label>Last Name: </label>
    <input type = "text" name = "lastname" placeholder = "Doe"/>
    <p class = "error"><?php echo $errorL;?></p>
    <input type="submit">
   </form>
  </body>
 </html>
Run Code Online (Sandbox Code Playgroud)

ter*_*ško 5

大家总是做“数据库类”和“验证类”。呃.... whaaaaay?

不要制作验证类。它永远不会奏效。用于验证用户输入的最 .. emm ... 可持续选项是:

  • 在域实体中执行验证
  • 使用值对象

通过使用实体进行验证,这非常简单。在您的情况下,您将Profile在有方法的地方上课setFirstName(string $name)。然后在此方法中进行验证并在错误时抛出自定义异常,例如InvalidFirstName.. 或类似的东西。

使用值对象有点棘手,但它可以防止代码重复。例如,您需要验证电子邮件地址。因此,您希望使用它的方式如下所示:

try {
    $profile = new Profile;
    $profile->setEmail(new EmailAddress($_POST['email']));
} catch (InvalidArgumentException $e){
    // validation failed
}
Run Code Online (Sandbox Code Playgroud)

因此,要获得这种行为,您可以像这样定义类:

class EmailAddress
{
    private $email;


    public function __construct(int $emailId = null, string $email = null)
    {
        if (!$this->isValid($email)) {
            throw new InvalidArgumentException('Not valid email address');
        }
        $this->email = $email;
    }


    private function isValid($email)
    {
        return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
    }


    public function __toString()
    {
        return $this->email;
    }
}
Run Code Online (Sandbox Code Playgroud)

这种方法更具表现力,但在与持久层交互时,它往往会变得很粗糙。

在实践中,最好的选择是结合使用这两种解决方案:

  • 将验证保留在实体中,对于规则,这是唯一的
  • 将值对象用于经常重复的约束