gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:05:34 26 4
gpt4 key购买 nike

我创建了一个类“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>

最佳答案

Everyone always makes "database class" and "validation class". Ehh .... whaaaaay?



不要制作验证类。它永远不会奏效。用于验证用户输入的最 .. emm ... 可持续选项是:
  • 在域实体中执行验证
  • 使用 value objects

  • 通过使用实体进行验证,这非常简单。在您的情况下,您将拥有类(class) Profile你有方法的地方 setFirstName(string $name) .然后在这个方法中你做验证并在错误时抛出一个定制的 exception , 喜欢 InvalidFirstName .. 或类似的东西。

    使用值对象有点棘手,但它可以防止代码重复。例如,您需要验证电子邮件地址。因此,您希望使用它的方式如下所示:
    try {
    $profile = new Profile;
    $profile->setEmail(new EmailAddress($_POST['email']));
    } catch (InvalidArgumentException $e){
    // validation failed
    }

    因此,要获得这种行为,您可以像这样定义类:
    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;
    }
    }

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

    在实践中,最好的选择是结合使用这两种解决方案:
  • 对于唯一的规则,将验证保留在实体中
  • 将值对象用于经常重复的约束
  • 关于php - 如何使用面向对象技术验证 PHP 中的表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43373785/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com