【问题标题】:Laravel - Validation errorsLaravel - 验证错误
【发布时间】:2013-10-31 06:37:14
【问题描述】:

我想返回 $validator->errors() 并包含另一个元素“消息”来保存项目的状态。

例如:

    if ($validator->fails()) {          

        $response = array('data' => $validator->errors());
        $status = 'failed';
        // I tried this but it didn't work
        // $response = array('data' => $response, 'status' => 'failed')

    } else {

        $status = (Phone::create($post_data)) ? "success" : 'failed';
        $response = array('status' => $status);

    }

    return Response::json($response);

所以在 javascript 方面我会加载类似的内容:

    if (data.status == 'success') { console.log('success'); }
    else {
         $.each(data, function(index, value) {
            message += '<div class="text-warning"> <span class="glyphicon glyphicon-exclamation-sign"></span>  ' + (data[index]) + '</div>';
         });
    }

【问题讨论】:

    标签: javascript php jquery laravel laravel-4


    【解决方案1】:

    方法errors()返回一个MessageBag实例,需要获取一个数组:

    if ($validator->fails()) 
    {          
    
        $response = array(
                          'data' => $validator->errors()->all(), 
                          'status' => 'failed'
                         );
    
    }
    else 
    {
    
        $status = (Phone::create($post_data)) ? "success" : 'failed';
        $response = array('status' => $status);
    
    }
    
    return Response::json($response);
    

    【讨论】:

      猜你喜欢
      • 2018-09-05
      • 1970-01-01
      • 2016-03-28
      • 2019-12-07
      • 2018-12-05
      • 2017-07-16
      • 2016-05-10
      相关资源
      最近更新 更多