我的ajax脚本发送这样的数组:
这个数组属于Input :: get(‘questions’)
这个数组属于Input :: get(‘questions’)
Array ( [0] => Array ( [name] => fields[] [value] => test1 ) [1] => Array ( [name] => fields[] [value] => test2 ) )
你可以帮助我,我需要这样的东西:
$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';