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
A F
  • 7,424
  • 8
  • 40
  • 52
  • 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. – mopo922 Jan 15 '15 at 22:14
  • @mopo922 If I have the wrong class, where is the right class? – A F Jan 15 '15 at 22:15
  • 1
    You might be able to get Laravel's validator by using `\Validator::make($values, $rules)`. – user1669496 Jan 15 '15 at 22:22
  • @user3158900 that returns `Class 'Validator' not found` – A F Jan 15 '15 at 22:25
  • In Attempt 4, where does `DefaultValidator` come from? What if you also add `use \DefaultValidator` ? – mopo922 Jan 15 '15 at 22:41
  • Fixed. Should be more obvious now. It's basically attempt 3 but I aliased it to a different class. – A F Jan 15 '15 at 22:43

3 Answers3

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.

A F
  • 7,424
  • 8
  • 40
  • 52
0

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

return \Validator::make($values,$rules);
mopo922
  • 6,293
  • 3
  • 28
  • 31
  • 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 static – A F Jan 15 '15 at 22:21
  • still nada: `Class 'Validator' not found`. Doing research into Facades, so thanks for the pointer. – A F Jan 15 '15 at 22:24
  • That's puzzling. Are you using the "other" Validator class? If not, maybe `use \Validator;` at the top of your file would work? – mopo922 Jan 15 '15 at 22:27
  • 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) – A F Jan 15 '15 at 22:37
-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);
Nagibaba
  • 4,218
  • 1
  • 35
  • 43