Skip to content

getExtraConditions Validation Undefined Offset: 1 Fault #28697

@gofish543

Description

@gofish543
  • Laravel Version: 5.8.19
  • PHP Version: 7.3.6
  • Database Driver & Version:

Description:

While running validation tests within FormRequests I kept receiving a "Undefined offset: 1".

ErrorException : Undefined offset: 1
 vendor/laravel/framework/src/Illuminate/Validation/Concerns/ValidatesAttributes.php:841

Steps To Reproduce:

RegisterRequest.php

<?php

namespace App\Http\Requests\Auth;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class RegisterRequest extends FormRequest {

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize() {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules() {
        return [
//                        'recaptchaToken' => ['required', new CaptchaRule()],
            'username' => [
                'required',
                'string',
                'max:255',
                'unique:users,username',
            ],
            'password' => [
                'required',
                'string',
                'min:4',
                'max:255',
                'confirmed',
            ],
            'email' => [
                'required',
                'email',
                'max:255',
                'unique:users,email',
            ],
            'phone' => [
                'required',
                'string',
                'regex:/\d{3}-\d{3}-\d{4}/',
            ],
            'first' => [
                'required',
                'string',
                'max:255',
            ],
            'last' => [
                'required',
                'string',
                'max:255',
            ],
            'street' => [
                'sometimes',
                'string',
                'max:255',
            ],
            'street_2' => [
                'sometimes',
                'string',
                'max:255',
            ],
            'city' => [
                'sometimes',
                'string',
                'max:255',
            ],
            'state' => [
                'sometimes',
                'string',
                'max:255',
            ],
            'zip' => [
                'sometimes',
                'string',
                'max:5',
            ],

            'do_email' => [
                'required',
                'boolean',
                'max:1',
            ],
            'do_phone' => [
                'required',
                'boolean',
                'max:1',
            ],
            'registration_code' => [
                'required',
                'string',
                'size:8',
                'regex:/\d{4}-\d{3}/'
            ],
            'club_id' => [
                'required',
                Rule::exists('clubs', 'id')->where('registration_code', $this->input('registration_code', ''))
            ],
        ];
    }

    /**
     * @return array
     */
    protected function validationData() {
        $data = $this->all();

        $data['street'] = $data['street'] ?? '';
        $data['street_2'] = $data['street_2'] ?? '';
        $data['city'] = $data['city']?? '';
        $data['state'] = $data['state'] ?? '';
        $data['zip'] = $data['zip'] ?? '';

        $data['do_email'] = $data['do_email'] ?? false;
        $data['do_phone'] = $data['do_phone'] ?? false;

        return $data;
    }
}

TestCase

<?php

namespace Tests\Unit\Http\Requests\Auth;

use App\Http\Requests\Auth\RegisterRequest;
use App\Http\Requests\Resource\Club\ClubUpdateRequest;
use App\Role;
use Illuminate\Support\Arr;
use Tests\TestCase;

class RegisterRequestTest extends TestCase {

    /** @var string */
    protected $url;

    public function setUp(): void {
        parent::setUp();

        $this->url = route('register');
    }

    public function testSuccessfulValidation() {
        $this->post(route('register'), $this->getData());
        //$this->formRequest(RegisterRequest::class, $this->url, 'POST', null, $this->getData());
        //dd($this->errors);
//            ->assertValidationPassed()
//            ->assertAuthorized();
    }

    private function getData($except = []) {
        $password = $this->faker->password;

        return Arr::except([
            '_token' => csrf_token(),

            'username' => $this->faker->userName,
            'password' => $password,
            'password_confirmation' => $password,
            'email' => $this->faker->email,
            'phone' => $this->faker->numerify('###-###-####'),
            'first' => $this->faker->firstName,
            'last' => $this->faker->lastName,
            'role_id' => Role::NO_ROLE,

            'do_email' => true,
            'do_phone' => true,

            'club_id' => $this->club->id,
//            'registration_code' => $this->club->registration_code,
        ], $except);
    }
}

StackTrace

Testing started at 10:46 PM ...
[ssh://gofish543@localhost:22]:/usr/bin/php /mnt/d/TrapStats/vendor/phpunit/phpunit/phpunit --configuration /mnt/d/TrapStats/phpunit.xml --filter "/(::testSuccessfulValidation)( .*)?$/" Tests\\Unit\\Http\\Requests\\Auth\\RegisterRequestTest /mnt/d/TrapStats/tests/Unit/Http/Requests/Auth/RegisterRequestTest.php --teamcity
PHPUnit 8.1.6 by Sebastian Bergmann and contributors.

http://trapstats.local/register 2019-06-02 03:46:10 POST 302 Symfony

ErrorException : Undefined offset: 1
 /mnt/d/TrapStats/vendor/laravel/framework/src/Illuminate/Validation/Concerns/ValidatesAttributes.php:841
 /mnt/d/TrapStats/vendor/laravel/framework/src/Illuminate/Validation/Concerns/ValidatesAttributes.php:677
 /mnt/d/TrapStats/vendor/laravel/framework/src/Illuminate/Validation/Concerns/ValidatesAttributes.php:658
 /mnt/d/TrapStats/vendor/laravel/framework/src/Illuminate/Validation/Validator.php:398
 /mnt/d/TrapStats/vendor/laravel/framework/src/Illuminate/Validation/Validator.php:277
 /mnt/d/TrapStats/vendor/laravel/framework/src/Illuminate/Validation/Validator.php:302
 /mnt/d/TrapStats/vendor/laravel/framework/src/Illuminate/Validation/ValidatesWhenResolvedTrait.php:25
 /mnt/d/TrapStats/tests/Assertions/FormRequestAssertionTrait.php:69
 /mnt/d/TrapStats/tests/Assertions/FormRequestAssertionTrait.php:57
 /mnt/d/TrapStats/tests/Unit/Http/Requests/Auth/RegisterRequestTest.php:24
 


Time: 880 ms, Memory: 30.00 MB


ERRORS!
Tests: 1, Assertions: 5, Errors: 1.

Process finished with exit code 2

I've identified the error to be here, but as I do not have a firm grasp of the laravel core and it's inner workings I'll leave the further fixing to the pros

    /**
     * Get the extra conditions for a unique / exists rule.
     *
     * @param  array  $segments
     * @return array
     */
    protected function getExtraConditions(array $segments)
    {
        $extra = [];

        $count = count($segments);

        for ($i = 0; $i < $count; $i += 2) {
            $extra[$segments[$i]] = $segments[$i + 1];
        }

        return $extra;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions