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

[5.0] Array Input Validation #5645

Closed
kalaomer opened this issue Sep 3, 2014 · 8 comments
Closed

[5.0] Array Input Validation #5645

kalaomer opened this issue Sep 3, 2014 · 8 comments

Comments

@kalaomer
Copy link

kalaomer commented Sep 3, 2014

Hi;

I was seen on 4.3 features and I like form validation so I try to use it. But I have a problem for validate to array inputs. That's like;

foo[0][bar] = 1
foo[1][bar] = 2
foo[2][bar] = 3

I have a method but it's not clean one. So I use rule templates that is like foo.*.bar. It means bar is child of foo's child. So I create true ways with this template and try to Validate inputs. It is run now but not clean, So any plan to easy way for this?

@kalaomer kalaomer changed the title Array Input Validation [4.3] Array Input Validation Sep 3, 2014
@emnsen
Copy link

emnsen commented Sep 4, 2014

👍

@kalaomer kalaomer changed the title [4.3] Array Input Validation [4.3][proposal] Array Input Validation Sep 4, 2014
@ibrasho
Copy link
Contributor

ibrasho commented Sep 5, 2014

👍

@garygreen
Copy link
Contributor

Honestly, multi-dimensional array validation is a big ball ache in general. It would be nicer if Laravel could have some form of built in support for this but once you dig into it it's a pretty complicated thing to get right.

I have built a custom package that did array validation like you have posted:

$rules = [
    'images' => 'array|min:1',
    'images.*.file' => 'image|max:5M',
    'images.*.alt' => 'max:255',
    'images.*.product_id' => 'required|exists:id,products',
];

But it was abandoned because found it to make too much assumptions and became impossible to determine what input was validated exactly, allowing us to fill our models with just that validated input.
Instead, we now build up a big list of validation rules simply by looping through the input. It's a very simple concept and gives maximum flexibility. So for example:

$input = Input::all();

$rules = [
    'images' => 'array|min:1',
];

if (isset($input['images']) && is_array($input['images']))
{
    foreach ($input['images'] as $imageKey => $image)
    {
        $ruleKey = 'images.' . $imageKey;
        $rules[$ruleKey . '.file']       = 'image|max:5M';
        $rules[$ruleKey . '.alt']        = 'max:255';
        $rules[$ruleKey . '.product_id'] = 'required|exists:id,products';
    }
}

@kalaomer kalaomer changed the title [4.3][proposal] Array Input Validation [5.0][proposal] Array Input Validation Sep 26, 2014
@kalaomer
Copy link
Author

@garygreen I just understand what you try to tell me, and I think it seems like ok. I will try to use for Array Inputs, Thanks!

@GrahamCampbell GrahamCampbell changed the title [5.0][proposal] Array Input Validation [5.0] Array Input Validation Sep 28, 2014
@JosephSilber
Copy link
Member

Isn't this what each is for?

$validator = Validator::make(Input::all(), [...]);

$validator->each('foo', [
    'bar' => 'numeric',
]);

if ($validator->passes())
{
    //
}

@GrahamCampbell
Copy link
Member

Yeh, probably.

@kalaomer
Copy link
Author

@JosephSilber, actually it's not. I just try to arrive all childs with '*' operator. And each function can't help to me because of that validate function of FormRequest class wants to an array which have validator rules.

Or I can re-write validate function on which extends FormRequest class, so I don't need to use validate function for that.

@JosephSilber
Copy link
Member

@kalaomer The FormRequest only needs the rules array if you don't apply your own validator.

You can apply your own validator in the validator method:

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

class MyFormRequest extends FormRequest {

    public function validator(Factory $factory)
    {
        $validator = $factory->make($this->all(), [...]);

        $validator->each('foo', [
            'bar' => 'numeric',
        ]);

        return $validator;
    }

}

It will then use this validator instead of creating its own from the rules array.

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

No branches or pull requests

6 participants