我想返回$ validator-> errors()并包含另一个元素“message”来保存项目的状态。
例如:
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>';
});
}
答案 0 :(得分: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);