dops57958 2016-11-25 04:02 采纳率: 0%
浏览 287
已采纳

我们如何在laravel 5.3中对多维数组进行自定义验证

I am using a 2 dimensional array of fields in Laravel 5.3. I wish to validate all the values of that as required and to display the individual error messages. I used the validation in the controller function as follows

$this->validate($request, [
            'training_id' =>'required',            
            'survey_name' =>'required',  
            'question.*' =>'required',
            'answer_option.*.*' =>'required'
        ],
        ['question.*.required' => 'The question field is required.',
        'answer_option.*.*.required' => 'The answer field is required.']);

In the view page while i tried to display all the error messages, only the first row of answers will be validated and the others won't. The error messages are as

Array ( [0] => The question field is required. [1] => The question field is required. [2] => The answer_option.0.0 field is required. [3] => The answer_option.0.1 field is required. [4] => The answer_option.0.2 field is required. [5] => The answer_option.0.3 field is required. [6] => The training id field is required. [7] => The survey name field is required. ) 
  • 写回答

1条回答 默认 最新

  • dougutuo9879 2017-04-10 06:48
    关注

    To Validate all the fields with custom error message individually, Creating validator manually is the best solution....

    step-1:

    use Validator; (after namespace of any controller where validation is needed)
    

    step-2:

    Add a method (suppose the method name check_data()). And inside the following code will be placed....

    public function check_data(Request $request){
    
        //Rules for form fields
        $rules = array(
            'training_id' =>'required',
            'survey_name' =>'required',
            'question' =>'required',
            'answer_option' =>'required'
        );
    
        //Custom message for individual fields
        $messages = array(
            'training_id.required' => 'Training Id should not be empty...',
            'survey_name.required' => 'Survey Name  is Essential...',
            'question.required' => 'Question is urgent...',
            'answer_option.required' => 'Answer option can be any one but required...'
    
        );
    
        $validator = Validator::make($request->all(), $rules, $messages);
    
        //Check for validation
        if ($validator->fails()) {
            //if validation fails
            return redirect()->back()->withInput()->withErrors($validator);
        } else {
            //Validation is successful and do as you wish
            return "All data are validated !!";
    
        }
    
    
    
    
    }
    

    Hopefully, It will work

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题