How do I call Validator from a namespace with an already existing Validator class
Asked Answered
W

3

2

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
Whitefish answered 15/1, 2015 at 22:9 Comment(6)
Possible hint: "Call to undefined method" means that it did find a class at that location, but it's probably not the class you're looking for.Sylphid
@Sylphid If I have the wrong class, where is the right class?Whitefish
You might be able to get Laravel's validator by using \Validator::make($values, $rules).Nervine
@user3158900 that returns Class 'Validator' not foundWhitefish
In Attempt 4, where does DefaultValidator come from? What if you also add use \DefaultValidator ?Sylphid
Fixed. Should be more obvious now. It's basically attempt 3 but I aliased it to a different class.Whitefish
W
2
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.

Whitefish answered 15/1, 2015 at 23:41 Comment(0)
S
0

Try just a single forward slash to refer to the global namespace:

return \Validator::make($values,$rules);
Sylphid answered 15/1, 2015 at 22:17 Comment(4)
Thanks mopo, but its returning Using $this when not in object context in /vendor/laravel/framework/src/Illuminate/Validation/Factory.php on line 92. Factory has a make function but its not staticWhitefish
still nada: Class 'Validator' not found. Doing research into Facades, so thanks for the pointer.Whitefish
That's puzzling. Are you using the "other" Validator class? If not, maybe use \Validator; at the top of your file would work?Sylphid
That's exactly the case. My namespace has a Validator class, but I'm trying to access the one in the docs without renaming my current class. Both use \Validator and use \Validator as DefaultValidator still don't work (edited original question with why)Whitefish
A
-1

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);
Andonis answered 19/3, 2017 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.