Laravel 4验证-嵌套索引数组? [英] Laravel 4 Validation - Nested Indexed Arrays?

查看:88
本文介绍了Laravel 4验证-嵌套索引数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有各种各样的东西...

I have an array of various things...

$foo = [];
$foo['stuff']['item'][0]['title' => 'flying_lotus'];
$foo['stuff']['item'][1]['title' => 'various_cheeses'];
$foo['stuff']['item'][2]['title' => 'the_career_of_vanilla_ice'];
$foo['stuff']['item'][3]['title' => 'welsh_cats'];

如何使用Laravel 4中的Validator方法来验证"title"键?

How would I validate the 'title' key, using the Validator method in Laravel 4?

这是我到目前为止所拥有的...

Here's what I have so far...

$validator = Validator::make($foo, ['stuff.item.???.title' => 'required']);

我完全被索引数组弄糊涂了.任何帮助都会很棒.

I'm totally flummoxed by the indexed array. Any help would be great .

推荐答案

以下答案适用于Laravel< = 5.1. Laravel 5.2引入了内置数组验证.

目前,Validator类并不旨在遍历数组数据.它可以遍历嵌套数组以查找特定值,但希望该值是单个(通常为string)值.

At this time, the Validator class isn't meant to iterate over array data. While it can traverse through a nested array to find a specific value, it expects that value to be a single (typically string) value.

从我的角度来看,您有几种选择:

The way I see it, you have a few options:

1:使用字段名称中的数组键创建规则.

基本上,您实际上正在做的事情,除了您需要确切地弄清您的['stuff']['item']数组有多少个值.我做了这样的事情,结果很好:

Basically essentially what you're doing already, except you'd need to figure out exactly how many values your ['stuff']['item'] array has. I did something like this with good results:

$data = [
    'stuff' => [
        'item'  => [
            ['title' => 'flying_lotus'],
            ['title' => 'various_cheeses'],
            ['title' => ''],
            ['title' => 'welsh_cats'],
        ]
    ]
];

$rules = [];

for ($i = 0, $c = count($data['stuff']['item']); $i < $c; $i++)
{
    $rules["stuff.item.{$i}.title"] = 'required';
}

$v = Validator::make($data, $rules);

var_dump($v->passes());

2:创建自定义验证方法.

这将允许您创建自己的规则,可以在其中期望一个数组值,并在必要时对其进行迭代.

This will allow you to create your own rule, where you can expect an array value and iterate it over as necessary.

此方法有其警告,因为A)您将没有特定值的错误消息,因为它将对整个数组产生错误(例如,如果您将stuff.item传递为要检查的值),则B ),您需要在自定义函数中检查数组的所有可能值(我假设您不仅仅可以通过title进行验证).

This method has its caveats, in that A) you won't have specific value error messages, since it'll error for the entire array (such as if you pass stuff.item as the value to check), and B) you'll need to check all of your array's possible values in your custom function (I'm assuming you'll have more than just title to validate).

您可以使用 Validator::extend() 来创建验证方法通过将课程完全扩展到其他地方.

You can create the validation method by using the Validator::extend() or by fully extending the class somewhere else.

3:扩展Validator类并替换/父级相关规则以接受数组.

创建您自己的扩展Validator类,并实现自定义规则或重命名现有规则,从而使这些规则可以接受数组值(如果同时发生).这与#2自定义规则选项有一些类似的警告,但如果您打算经常验证迭代数组,则可能是最佳实践".

Create your own extended Validator class, and either implement custom rules, or rename existing ones, enabling those rules to accept an array value if one happens along. This has some similar caveats to the #2 custom rule option, but may be the "best practice" if you intend on validating iterated arrays often.

这篇关于Laravel 4验证-嵌套索引数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆