简体   繁体   English

laravel验证多维数组

[英]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. 我有一个带有name="action_id[]"选择的页面,用户可以通过单击按钮来添加另一个动作。 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. 新动作是另一个带有name="action_id[]"选择,因此我最终得到一个视图,其中包含多个具有相同名称的选择。

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 : 它们具有相同的名称,所以我不能这样做,因为它仅验证一个action_id

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

where Actions::rules is Actions::rules在哪里

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()来做到这一点:

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. 假设您的$actions是表单返回的数组。 It should look like this: 它看起来应该像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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