0

我想返回 $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>';
         });
    }
4

1 回答 1

3

该方法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);
于 2013-10-22T12:09:06.913 回答