3

Respect\Validation\Validator. I have a validator class validator.php

<?php

namespace app\http\validations;
use Respect\Validation\Validator as Respect;
use Respect\Validation\Exceptins\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 $ex) {
                $this->errors[$field] = $ex->getMessages();
            }

        }
        return $this;
    }

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

Then I'm using this validation in the controller and here is my controller:

<?php

namespace app\http\controllers\v1;


use Slim\Http\Request;
use Slim\Http\Response;
use Respect\Validation\Validator as v;

use app\providers\v1\CompanyServiceProvider;

class CompanyController extends BaseApiController {

    public $companyServiceProvider;

    public function __construct() {
        $this->companyServiceProvider = new CompanyServiceProvider();
    }

    public function saveBasicDetails(Request $request, Response $response) {
        $validator = new \app\http\validations\Validator();
        $validation = $validator->validate($request, [
            'company_name' => v::notEmpty()->alpha()
         ]);

        if($validation->failed()){
            print_r($validation); die;
        }

        $result = $this->companyServiceProvider->saveBasicDetails($request);
        return BaseApiController::returnResponse($response, $result);
    }

}

Here is my route, I'm calling saveBasicDetails function from the route.

$app->post('/save-basic-details', \app\http\controllers\v1\CompanyController::class. ':saveBasicDetails');

I'm getting following error : These rules must pass for Company_name

I wanted to return validation errors in JSON.

Saurabh Sharma
  • 463
  • 2
  • 7
  • 20
  • you need to set `$this->validator` to your validator from the container. it's null, so I'm guessing you didn't do that – delboy1978uk Oct 03 '18 at 10:28
  • From where is the `saverUserDetails` Method? Please add some more code. I think the issue is, that you can't access the container within youe `saveUserDetails` Method. – T K Oct 03 '18 at 12:29
  • I'm calling saveUserDetails method from the route. I also have updated that code. – Saurabh Sharma Oct 03 '18 at 12:39
  • Okay, please show your `CompanyController` Class. I think there'll be the issue. – T K Oct 03 '18 at 12:44
  • I have updated my question. Because container concept was confusing little bit. So I have removed this. I have put my controller code. – Saurabh Sharma Oct 03 '18 at 17:04

2 Answers2

1

In the controller you have to inject the container in the constructor, then define a getter to get your validator. check this link, in slim so far there is nothing like symfony autowire you have to explicitly inject the container and get the service you need afaik. Check this code below will help you!

//use Psr\Container\ContainerInterface;


/**
 * @param ContainerInterface $container
 */
public function __construct(ContainerInterface $container)
{
    $this->container = $container;
}


//Then define a getter

/**
 * @return Validator
 */
protected function getValidator()
{
    if (!$this->validator instanceof Validator) {
        $this->validator = $this->container->get('Validator');
    }

    return $this->validator;
}


//Then access the validator
$this->getValidator()->validate...
Ntwobike
  • 2,406
  • 1
  • 21
  • 27
  • I removed the container concept public function saveBasicDetails(Request $request, Response $response) { $validator = new \app\http\validations\Validator(); $validation = $validator->validate($request, [ 'company_name' => v::notEmpty()->alpha() ]); if($validation->failed()){ print_r($validation); die; } $result = $this->companyServiceProvider->saveBasicDetails($request); return BaseApiController::returnResponse($response, $result); } – Saurabh Sharma Oct 03 '18 at 17:00
  • I have removed the container concept. But still getting error but different error. – Saurabh Sharma Oct 03 '18 at 17:06
  • I'm getting following error : These rules must pass for Company_name – Saurabh Sharma Oct 03 '18 at 17:22
0

Guys I made a silly mistake there was a spelling mistake in

use Respect\Validation\Exceptins\NestedValidationException;

Changed to

use Respect\Validation\Exceptions\NestedValidationException;

Fixed my issue.

Saurabh Sharma
  • 463
  • 2
  • 7
  • 20