gpt4 book ai didi

php - 验证数组 Laravel 5

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:41:12 25 4
gpt4 key购买 nike

我的 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';

关于php - 验证数组 Laravel 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29952448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com