对象返回一个额外的NULL数组

问题描述 投票:0回答:1

所以我用Slim框架构建了一个简单的API,同时使用一个中间件来验证这里https://github.com/Respect/Validation的功能,如下所示:

https://www.codecourse.com/lessons/slim-3-authentication/769 https://www.codecourse.com/lessons/slim-3-authentication/770

除了一件具体的事情外,一切似乎都没问题:

通过本指南,我应该能够验证我的表单及其输入,然后在我的Twig页面上打印这些错误,例如通过执行此{{ errors | json_encode }}(用于测试),问题是即使验证函数本身可以正常工作,我也可以似乎得到错误,因为它们总是返回null。

通过使用var_dump我注意到“错误”在那里,但格式似乎与指南视频中显示的格式有点不同,因为在我的var_dump中似乎有一个额外的数组? (不确定)显示NULL,只有在那之后还有其他数组包含这些错误,所以我认为这就是为什么它不起作用的原因?

这是我在var_dump时得到的一个例子:

object(App\Validation\Validator)#79 (2) { ["errors":protected]=> NULL [""]=> array(1) { ["uniquevisitors"]=> array(2) { [0]=> string(30) "Uniquevisitors must be numeric" [1]=> string(31) "Uniquevisitors must be positive" } } }

那是我用get_object_vars对var_dump进行的时候:

array(1) { [""]=> array(1) { ["uniquevisitors"]=> array(2) { [0]=> string(30) "Uniquevisitors must be numeric" [1]=> string(31) "Uniquevisitors must be positive" } } }

我不认为{ ["errors":protected]=> NULL [""]=>应该在那里。我的代码与该指南完全相同,但它为他提供的结果略有不同。关于'uniquevisitors'的错误是应该如何,但我不认为null应该在那里,我认为可能是我的Twig模板无法读取它并且总是显示null的原因,但我可能是错的。

在中间件中使用全局和/或会话会出现问题吗? JFYI我也使用其他中间件,但是这一直是这样的,因为我添加了它,以防万一我再次运行它在另一个“干净”的苗条并得到相同的输出。当我做一些研究时,我发现一个案例有完全相同的问题,但是没有答案,所以我相信代码可能会有些问题?或者也许是我的环境?我使用xampp在本地运行它。


middleware.php

<?php

namespace App\Middleware;

class Middleware {
    protected $container;

    public function __construct($container) {
        $this->container = $container;
    }
}

ValidationErrorsMiddleware.php

<?php

namespace App\Middleware;

class ValidationErrorsMiddleware extends Middleware {
    protected $container;

    public function __invoke($request, $response, $next) {
        if (isset($_SESSION['errors'])) {
        $this->container->view->getEnvironment()->addGlobal('errors', $_SESSION['errors']);
        unset($_SESSION['errors']);
    }
        $response = $next($request, $response);
        return $response;
    }
    }

Validator.php

<?php

namespace App\Validation;

use Respect\Validation\Validator as Respect;
use Respect\Validation\Exceptions\NestedValidationException;

class Validator {

protected $errors;

    public function validate($request, array $rules) {

        foreach($rules as $field => $rule) {
            try {
                $rule->setName(ucfirst($field))->assert($request->getParam($field));
            } catch (NestedValidationException $e) {
                $errors='';
                $this->$errors[$field] = $e->getMessages();
            }
        }
        $_SESSION['errors'] = $this->errors;
        return $this;
    }
    public function failed(){
        $errors='';
        return !empty($this->$errors);
    }
}

出于某种原因,当我想通过使用{{ errors.field }}分别在Twig表单中发布这些错误时,它保持空白或具有null,(与errors | json_encode相同,它保持为null)。验证本身有效,var_dump表明存在这些错误但由于某种原因我不能将它们发布在树枝上,我认为这是因为我的“错误”包含一些不应该存在的额外Null。

我正在使用我在第一篇文章中提到的那个指南上发布的相同代码,它似乎适用于这个人但不适合我。

php api twig slim
1个回答
0
投票

你的验证器有一些问题,此时你正试图将错误分配给动态property$errors

$errors = '';
$this->$errors[$field] = $e->getMessages();

粗略地“翻译”这意味着,将$e->getMessages()的输出添加到属性$this->{empty string},因为$errors被定义为"",这是你与var_dump有“怪异”输出的原因

你真正想要的是这个,

$this->errors[$field] = $e->getMessages();

你的validate功能也一样

public function failed(){
    return !empty($this->errors); //and not $this->$errors
}
© www.soinside.com 2019 - 2024. All rights reserved.