3

I keep getting this exception using laravel's custom validation and I have no idea why. Here is the full exception message:

local.ERROR: exception 'ErrorException' with message 'Argument 2 passed to Illuminate\Validation\Factory::make() must be of the type array, object given, called in /home/vagrant/Code/Spark/my-project/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 223 and defined' in /home/vagrant/Code/Spark/my-project/vendor/laravel/framework/src/Illuminate/Validation/Factory.php:91

Here is my code:

$rules = array(
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
        'vat_id' => 'max:50|vat_id',
        'terms' => 'required|accepted',
        'address' => 'required|max:255',
        'city' => 'required|max:255',
        'state' => 'required',
        'contactName' => 'required',
        'phone' => 'numeric|required',
        'zip' => 'numeric|required',
    );

    $validator = Validator::make($request->all(), $rules);

Now, I've also tried it like this, and I get the same exception:

    $validator = Validator::make($request->all(), [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
        'vat_id' => 'max:50|vat_id',
        'terms' => 'required|accepted',
        'address' => 'required|max:255',
        'city' => 'required|max:255',
        'state' => 'required',
        'contactName' => 'required',
        'phone' => 'numeric|required',
        'zip' => 'numeric|required',
]);

As far as I can tell, I'm following the docs exactly (https://www.laravel.com/docs/5.2/validation#manually-creating-validators), and am passing an array. I've manually created validation plenty of times in laravel and never encountered this issue. I'm hoping that there's something obvious that I'm doing wrong that maybe another set of eyes can easily pick up, because this has me really stumped. Any help is very appreciated! If it makes a difference, I am using Spark.

Edit: Here's what I get when I var_dump $rules:

array (size=12)
'name' => string 'required|max:255' (length=16)
'email' => string 'required|email|max:255|unique:users' (length=35)
'password' => string 'required|confirmed|min:6' (length=24)
'vat_id' => string 'max:50|vat_id' (length=13)
'terms' => string 'required|accepted' (length=17)
'address' => string 'required|max:255' (length=16)
'city' => string 'required|max:255' (length=16)
'state' => string 'required' (length=8)
'contactName' => string 'required' (length=8)
'phone' => string 'numeric|required' (length=16)
'zip' => string 'numeric|required' (length=16)
Christian
  • 41
  • 1
  • 8
  • 1
    Can you double check that you're on Laravel 5.2 with `php artisan -V`? I ask because a lot of people have been running into weird issues with running Laravel 5.3 on Spark. – Samsquanch Sep 02 '16 at 14:10
  • @Samsquanch yep, Version 5.2.39. Thanks for the tip though – Christian Sep 02 '16 at 14:13
  • 1
    When you `var_dump($rules)` I'm assuming it comes back as type array? – Samsquanch Sep 02 '16 at 14:19
  • 1
    try `(array)$rules` – Sherif Sep 02 '16 at 14:27
  • @Samsquanch yes it does – Christian Sep 02 '16 at 14:42
  • @Sherif unfortunately that still gives me the same exception. Thanks though – Christian Sep 02 '16 at 14:45
  • 1
    I thought that `confirmed` required something like `password_confirmation` to be included along with validation? Perhaps remove `confirmed` from it to test or [see the docs](https://www.laravel.com/docs/5.2/validation#rule-confirmed) and add `password_confirmation`? – camelCase Sep 02 '16 at 15:15
  • 1
    @camelCase, that field is present. Per the docs, "The field under validation must have a matching field of `foo_confirmation`. For example, if the field under validation is password, a matching `password_confirmation` field must be present in the input." So, that field is present in the form and the input, and the confirmed rule checks that field while it is validating the `password` field, so you don't need to validate that one as well. Still, I tried removing the confirmed rule and still got the same exception. – Christian Sep 02 '16 at 15:25
  • Hmm, interesting issue. It's as if the error message doesn't match the issue. Your code looks valid and even type casting to array, just to be certain, doesn't work. Very strange. – camelCase Sep 02 '16 at 15:30

0 Answers0