0

当我正在探索可用于简单验证的 PHP 库时,我偶然发现了Respect\Validation看起来很有希望的 PHP 库。但是我遇到了一个问题,即不同的异常消息输出不同的信息。

尽管,

$postValidator = Valid::attribute('title',Valid::length(2,3)->stringType())
            ->attribute('description',Valid::stringType())
            ->attribute('author',Valid::intType()->length(1,2))
            ->attribute('user',Valid::intVal()->length(1,2));
try {
    $postValidator->assert($input);
} catch (NestedValidationException $e) {
    echo json_encode($e->getMessages());
}

给了这个。

{
    "title": "title must be of type string",
    "author": "author must have a length between 1 and 2",
    "user": "Attribute user must be present"
}

使用$e->getFullMessage()而不是$e->getMessages()输出这个。

 - All of the required rules must pass for title\n    
    - title must have a length between 2 and 3\n    
    - title must be of type string\n  
 - All of the required rules must pass for author\n    
    - author must be of type integer\n    
    - author must have a length between 1 and 2\n    
 - Attribute user must be present

后者有更多信息。有没有一种方法可以获得所有$e->getMessages()更方便处理的信息,因为它不仅仅是一个字符串?

尊重\验证版本:2.2.3

4

0 回答 0