0

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.

2 Answers2

2

It seems to me that you may be missing a dependency file such as respect/validation as some elements are installed during the video tutorials, I would recommend watching the video(s) concerning any of the validation routines, as the codecourse "Authentication with Slim 3:", installs additional addons/components with package managers such as composer, during the tutorial which may have been missed.

1

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

Siniša
  • 154
  • 1
  • 4
  • That was the thing I tried to do before but got another error so did undo. If you want to help me @Siniša with this new error I would be very happy. – Jonathan Thunberg May 19 '17 at 20:28
  • Call to undefined method Cart\Validation\Validator::email() – Jonathan Thunberg May 19 '17 at 20:37
  • That means that there is no static method called `email()` in that class. You see `Validator::email()` is a call to a static method `public static function email()` inside a class called `Validator`. Laravel is reporting that no such method exists there... Or that it's not static... You need to take a look inside that class... – Siniša May 19 '17 at 20:43
  • Oh okay but the problem is this is full downloaded file/folder. I got it from codecourse as told in the first section. So I don't know what the method should be in the Validator.php file. Got any tips how I should do? – Jonathan Thunberg May 19 '17 at 20:47
  • Did you open Validator.php and looked if there is `email()` method inside? Also check that Tut, maybe you skipped a step or wrote something down wrong... – Siniša May 19 '17 at 20:51
  • Yeah I can check the video but the thing is I downloaded it and got the whole thing in one package. So it shouldn't be any issues. And as you can see if you scroll up I've a script section there my Validator.php is – Jonathan Thunberg May 19 '17 at 20:55
  • I failed. Just googled that, turns out it's a dependency https://github.com/Respect/Validation . You need to install it via Composer > https://github.com/Respect/Validation/blob/master/docs/INSTALL.md . So that path `use Respect\Validation\Validator as v;` is probably gonna be fine after you install it. – Siniša May 19 '17 at 21:08
  • Thank you so much @Sinisa, now everything works and that because of you. Thank you for your help – Jonathan Thunberg May 19 '17 at 23:29