'How do I call Validator from a namespace with an already existing Validator class
I'm trying to test a function in phpspec which calls Laravel's Validator::make
function (http://laravel.com/docs/4.2/validation)
However, I'm trying to call that same function from a namespace where the Validator
class name is already taken. How can I call that function described in the docs?
Failed solutions:
Attempt 1
return \Illuminate\Validation\Validator::make($values,$rules);
gives me
Call to undefined method Illuminate\Validation\Validator::make()
Attempt 2
return \Illuminate\Validation\Factory::make($values,$rules);
gives me
Using $this when not in object context in /vendor/laravel/framework/src/Illuminate/Validation/Factory.php on line 92. Factory
Attempt 3
use \Validator;
gives me
Cannot declare class Isoform\Validator because the name is already in use
Attempt 4
use \Validator as DefaultValidator;
gives me
Class 'DefaultValidator' not found
Solution 1:[1]
return \Illuminate\Support\Facades\Validator::make($values,$rules);
This will cause errors in phpspec, but that cannot be avoided. Although Validator::make
looks like a static function - behind the scenes it is really returning an instance. Because I was using phpspec, that instance was not created hence the error.
Solution 2:[2]
Try just a single forward slash to refer to the global namespace:
return \Validator::make($values,$rules);
Solution 3:[3]
I solved that one. Simply delete use Validator
from vendor\laravel\framework\src\Illuminate\Validation\Validator.php
and then add use Validator
code to your controller. Then you are ready for action.
$validator = Validator::make($request->all(), $rules);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Aakil Fernandes |
Solution 2 | |
Solution 3 | Nagibaba |