title: Object returning an extra NULL array inside
body: <p>So I've built a simple API with Slim framework, while using one Middleware for validation from here <a href="https://github.com/Respect/Validation" rel="nofollow noreferrer">https://github.com/Respect/Validation</a> via a function as followed through this guide: </p>
  
  <p><a href="https://www.codecourse.com/lessons/slim-3-authentication/769" rel="nofollow noreferrer">https://www.codecourse.com/lessons/slim-3-authentication/769</a><br>
  <a href="https://www.codecourse.com/lessons/slim-3-authentication/770" rel="nofollow noreferrer">https://www.codecourse.com/lessons/slim-3-authentication/770</a></p>
  
  <p>Everything seems to be alright except of one specific thing:</p>
  
  <p>By this guide I should be able to validate my forms and their input and then on my Twig page print these errors for example by doing this <code>{{ errors | json_encode }}</code> (for testing), the problem is that even though the validate function itself works as it should I can't seem to get the errors as they always return null. </p>
  
  <p>By using <code>var_dump</code> I've noticed that the "errors" are there, but the format seems a bit different than the one shown in the guide video as in my var_dump there seem to be an extra array? (not sure) showing <code>NULL</code>, and only after that there are other arrays containing these errors so I would assume that would be the reason of why it's not working? </p>
  
  <p>Here is an example of what I get when I var_dump it:</p>
  
  <pre><code>object(App\Validation\Validator)#79 (2) { ["errors":protected]=&gt; NULL [""]=&gt; array(1) { ["uniquevisitors"]=&gt; array(2) { [0]=&gt; string(30) "Uniquevisitors must be numeric" [1]=&gt; string(31) "Uniquevisitors must be positive" } } }
  </code></pre>
  
  <p>And that's when I var_dump it with get_object_vars:</p>
  
  <pre><code>array(1) { [""]=&gt; array(1) { ["uniquevisitors"]=&gt; array(2) { [0]=&gt; string(30) "Uniquevisitors must be numeric" [1]=&gt; string(31) "Uniquevisitors must be positive" } } }
  </code></pre>
  
  <p>I don't think the <code>{ ["errors":protected]=&gt; NULL [""]=&gt;</code>  should be there.
  My code is exactly as in that guide yet the results it yields for him are slightly different. The errors about the 'uniquevisitors' are how it should be, but I don't think that null should be there which I think may be the reason why my Twig template can't read it and always shows <code>null</code>, but I may be wrong.</p>
  
  <p>Can something be wrong with the usage of global and/or session within that middleware? JFYI I use other middleware too, but this has been this way since I added it and just in case I've ran it again on another "clean" slim and got the same output. When I did some research I've found one case with exact same issue but with no answer so I believe something may be off with the code? Or maybe with my environment? I am running it locally by using xampp.</p>
  
  <hr>
  
  <p>middleware.php</p>
  
  <pre><code>&lt;?php
  
  namespace App\Middleware;
  
  class Middleware {
      protected $container;
  
      public function __construct($container) {
          $this-&gt;container = $container;
      }
  }
  </code></pre>
  
  <p>ValidationErrorsMiddleware.php</p>
  
  <pre><code>&lt;?php
  
  namespace App\Middleware;
  
  class ValidationErrorsMiddleware extends Middleware {
      protected $container;
  
      public function __invoke($request, $response, $next) {
          if (isset($_SESSION['errors'])) {
          $this-&gt;container-&gt;view-&gt;getEnvironment()-&gt;addGlobal('errors', $_SESSION['errors']);
          unset($_SESSION['errors']);
      }
          $response = $next($request, $response);
          return $response;
      }
      }
  </code></pre>
  
  <p>Validator.php</p>
  
  <pre><code>&lt;?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 =&gt; $rule) {
              try {
                  $rule-&gt;setName(ucfirst($field))-&gt;assert($request-&gt;getParam($field));
              } catch (NestedValidationException $e) {
                  $errors='';
                  $this-&gt;$errors[$field] = $e-&gt;getMessages();
              }
          }
          $_SESSION['errors'] = $this-&gt;errors;
          return $this;
      }
      public function failed(){
          $errors='';
          return !empty($this-&gt;$errors);
      }
  }
  </code></pre>
  
  <p>For some reason when I want to post these errors respectively inside of a Twig form by using <code>{{ errors.field }}</code> it's staying blank or having null, (same with <code>errors | json_encode</code> it stays null). Validation itself works, and <code>var_dump</code> shows that there are these errors but for some reason I can't get them posted on twig, and I assume it's because my 'errors' contains some extra Null that shouldn't be there.</p>
  
  <p>I am using same code that was posted on that guide that I've mentioned in my first post, and it seem to work for this guy but not for me.</p>