php – 验证阵列Laravel 5

前端之家收集整理的这篇文章主要介绍了php – 验证阵列Laravel 5前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的ajax脚本发送这样的数组:
这个数组属于Input :: get(‘questions’)
Array
(
    [0] => Array
        (
            [name] => fields[]
            [value] => test1
        )

    [1] => Array
        (
            [name] => fields[]
            [value] => test2
        )

)

在html部分用户可以添加多个字段.

你可以帮助我,我需要这样的东西:

$inputs = array(
                'fields'    => Input::get('questions')
            );

            $rules = array(
                'fields'    => 'required'
            );
            $validator = Validator::make($inputs,$rules);

                if($validator -> fails()){
                    print_r($validator -> messages() ->all());
                }else{
                    return 'success';
                }
简单:使用for-each分别验证每个问题:
// First,your 'question' input var is already an array,so just get it
$questions = Input::get('questions');

// Define the rules for *each* question
$rules = [
    'fields' => 'required'
];

// Iterate and validate each question
foreach ($questions as $question)
{
    $validator = Validator::make( $question,$rules );

    if ($validator->fails()) return $validator->messages()->all();
}

return 'success';
原文链接:https://www.f2er.com/laravel/445231.html

猜你在找的Laravel相关文章