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 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端