ericsnguyen/php-validation

一个类似于C#和Java中带有注解的验证表单

dev-master 2021-01-05 06:41 UTC

This package is auto-updated.

Last update: 2024-09-05 14:59:29 UTC


README

这是一个JAVA的验证表单。我们通过注解来约束字段。

在C#和Java等其它语言中,我们使用属性注解来标记字段。

import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.validation.constraints.Email;
 
public class User {
 
    @NotNull(message = "Name cannot be null")
    private String name;
 
    @AssertTrue
    private boolean working;
 
    @Size(min = 10, max = 200, message 
      = "About Me must be between 10 and 200 characters")
    private String aboutMe;
 
    @Min(value = 18, message = "Age should not be less than 18")
    @Max(value = 150, message = "Age should not be greater than 150")
    private int age;
 
    @Email(message = "Email should be valid")
    private String email;

它非常优雅、简洁且美观。我在PHP上实现了一个版本。

先决条件

您的Web应用应使用DI库来管理所有对象。(例如:Laravel框架...)它将原生支持,无需添加任何代码,只需定义其日期时间的设置即可。

用法

步骤1:设置日期时间格式。

 \Validation\Configuration::setting([
            'date_format'=>'Y-m-d',
        ]);

步骤2:定义表单类。

class HiRequest extends \Validation\BaseRequest{
    /**
    * @\Validation\Annotations\NotNull
    * @\Validation\Annotations\MaxLength(max=256)
    */
    public string $name;

    /**
    * @\Validation\Annotations\Range(min=18, max=50)
    */
    public int $age;
}

这样就可以了!!

我们支持一些验证

  • NotNull
  • Min
  • Max
  • Email
  • Match
  • MinLength
  • MaxLength
  • Range
  • Length

您可以通过实现IValidator接口来创建自己的约束注解

Class YourCustomConstraint extends \Doctrine\Common\Annotations\Annotation implements \Validation\Interfaces\IValidator {     
    
    public function check($value) : bool{
        // predicate code
        return true;
    }
    
    public function getMessage() : string{
       return "what message you want!!!";
    }
}

对于旧版本。

如果您不使用DI库,在解析控制器实例的代码块中,您必须像下面这样解析。

  • 我假设您的控制器是HelloController
class HelloController{
    function sayHi(HiRequest $hiRequest){
        
    }
}
  • 您的HiRequest
class HiRequest extends \Validation\BaseRequest{
    /**
    * @\Validation\Annotations\NotNull
    * @\Validation\Annotations\MaxLength(max=256)
    */
    public string $name;
    ...
}
  • 假设这是解析控制器和处理请求方法hello/sayHi的代码。
$controller = new HelloController();
$controller->sayHi(new HiRequest);
  • 添加获取sayHi函数参数的功能
    public function getParameterOfActionMethod($className, $methodName){
        $class = new ReflectionClass($className);
        $method = $class->getMethod($methodName);
        $parameters = [];
        $actionParameters= $method->getParameters();
        foreach ($actionParameters as $parameter){
            $parameterType = (string)$parameter->getType();
            if (class_exists($parameterType)){
                $parameterInstance = new $parameterType();
                array_push($parameters, $parameterInstance);
            }else{
                // must be scalar type
                $value = $_REQUEST($parameter->name);
                if ($value==null){
                    $value = $_REQUEST(StringUtils::camelToSnake($parameter->name));
                }
                array_push($parameters, $value);
            }
        }
        return $parameters;
    }
// at controller handle code:
$controller = new HelloController();
$controller->sayHi(...$this->getParameterOfActionMethod(HelloController::class, "sayHi"));
  • 享受它吧!!
class HelloController{
    function sayHi(HiRequest $hiRequest){
        $hiRequest->name ... // validated, and do your logic.
    }
}