Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Laravel's dot validation #73

Merged
merged 3 commits into from May 3, 2017
Merged

Conversation

felixkiss
Copy link
Owner

Closes #57
Closes #64
Closes #72

More info: Validating Arrays in the Laravel Validation docs

This PR adds support for:

Dot access for nested arrays

$data = [
    'name' => [
        'first' => 'Foo',
        'middle' => 'Bar',
        'last' => 'Baz',
    ],
];
$rules = [
    'name.first' => 'required|unique_with:users,name.first = first_name,name.middle = middle_name, name.last = last_name',
    'name.middle' => 'required',
    'name.last' => 'required',
];
$validator = Validator::make($data, $rules);

Wildcard access to validate multiple elements in an array:

$data = [
    'users' => [
        [
            'first' => 'Foo',
            'middle' => 'Bar',
            'last' => 'Baz',
        ],
        [
            'first' => 'Homer',
            'middle' => 'J.',
            'last' => 'Simpson',
        ],
        [
            'first' => 'Philip',
            'middle' => 'J.',
            'last' => 'Fry',
        ],
    ],
];
$rules = [
    'users.*.first' => 'required|unique_with:users,users.*.first = first_name, users.*.middle = middle_name, users.*.last = last_name',
    'users.*.middle' => 'required',
    'users.*.last' => 'required',
];
$validator = Validator::make($data, $rules);

Since Laravel 5.0 doesn’t support array validation, we drop support for
it with this change.
@felixkiss felixkiss merged commit 451e660 into master May 3, 2017
@felixkiss felixkiss deleted the dot-validation branch May 3, 2017 04:26
@rscmendes
Copy link

rscmendes commented Sep 16, 2017

I get an error when validating arrays of the type "user_id": [1, 2, 3].
For example:
$data = [ "user_id": [ 4, 6 ], "post_id": 5 ];
$rules= [ 'user_id.*' => 'unique_with:post_user, post_id' ];
$validator = Validator::make($data, $rules);

This returns the following error:

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id.0' in 'where clause' (SQL: select count(*) as aggregate from post_user where user_id.0 = 4 and post_id = 5)"

Am I doing something wrong here? Or is this not supported yet?
Thank you

@felixkiss
Copy link
Owner Author

@bluetrickpt You need to specify the name of the column in the DB nanually when using arrays. Like so:

'user_id.*' => 'unique_with:post_user, user_id.* = user_id, post_id'

Let me know if this helps. If not, feel free to open a new issue for your problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants