phpvalidationrespect-validation

Slim Application Error: Respect\Validation\Validator


I get this error when trying to Validate my OrderForm.php. If someone followed Codecourse Shopping Cart Tutorial you may know why I get this error. Here is my code in some of my files that I think is most relevant to this error and of course the error.

Error:

Message: Class 'Respect\Validation\Validator' not found
File: PATH/cart/app/Validation/Forms/OrderForm.php on 13


I will also post this image of my Directory Folders: Directory Folder Image


OrderForm.php

<?php

namespace Cart\Validation\Forms;


use Respect\Validation\Validator as v;

class OrderForm
{
    public static function rules()
    {
        return [
            'email' => v::email(),
            'name' => v::alpha(' '),
            'address1' => v::alnum(' -'),
            'address2' => v::optional(v::alnum(' -')),
            'city' => v::alnum(' '),
            'postal_code' => v::alnum(' '),
        ];
    }
}

Validator.php

<?php

namespace Cart\Validation;

use Cart\Validation\Contracts\ValidatorInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Respect\Validation\Exceptions\NestedValidationException;

class Validator implements ValidatorInterface
{
    protected $errors = [];

    public function validate(Request $request, array $rules)
    {
        foreach ($rules as $field => $rule) {
            try {
                $rule->setName(ucfirst($field))->assert($request->getParam($field));
            } catch (NestedValidationException $e) {
                $this->errors[$field] = $e->getMessages();
            }
        }

        $_SESSION['errors'] = $this->errors;

        return $this;
    }

    public function fails()
    {
        return !empty($this->errors);
    }
}

ValidatorInterface.php

<?php

namespace Cart\Validation\Contracts;

use Psr\Http\Message\ServerRequestInterface as Request;

interface ValidatorInterface
{
    public function validate(Request $request, array $rules);
    public function fails();

}

Edit: I just want to say that I changed:
use Respect\Validation\Validator as v;
to
use Cart\Validation\Validator as v;

And then I get a completely new error so that did that work.


Solution

  • Well it tells you where the error is:

    Message: Class 'Respect\Validation\Validator' not found

    Path to that class is not valid, or that file is not on that path. I'm not completely sure but if you write it like you did use Respect\Validation\Validator as v; the final path will be current namespace plus that path Cart\Validation\Forms\Respect\Validation\Validator.

    File: PATH/cart/app/Validation/Forms/OrderForm.php on 13

    This second part is just were it triggered the error, on line 13 'email' => v::email(),.

    Edit: I just saw that image, the path should be use \App\Validation\Validator