简体   繁体   中英

laravel validate multidimensional array

I have a page which have a select with name="action_id[]" , the user can add another action by clicking on a button. The new action is another select with name="action_id[]" so I end up with a view that has many selects with the same name.

when the user submits the form, I do this in the controller:

$actions = Input::get('action_id')

and I get an array.

How to validate these values? They have the same name, so I can not do this because it validates only one action_id :

$validation = Validator::make($actions, Actions::rules)

where Actions::rules is

public static $rules = array(
    'action_id' => 'required|integer|not_in:0'
);

How can I validate the array of actions?

you could do it with a foreach() :

foreach ($actions as $singleAction) {
    $validation = Validator::make($singleAction, Actions::rules);
    // do whatever foo with $validation
}

this assumes that your $actions is the array returned by the form. It should look like this:

array(
    0 => 'action1',
    1 => 'action2',
  // etc
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM