I'm upgrading from Laravel 3 to Laravel 4. My app has some AJAX-only forms that are rendered client-side (i.e. there are no server-side views). Therefore, instead of passing validation errors to views with the withErrors() method, I've been returning the validation error objects to the client as JSON structures.
在《拉威尔3》中,我有这样一个故事:
$validation = Validator::make(Input::all(), $rules);
if($validation->fails())
{
return json_encode($validation->errors);
}
//else handle task
但在Laravel 4中,错误消息受到保护:
$validation = Validator::make(Input::all(), $rules);
if($validation->fails())
{
var_dump($validation->messages());
return json_encode($validation->messages());
}
//else handle task
var_dump($validation->messages()) returns:
object(Illuminate\Support\MessageBag)[333]
protected 'messages' =>
array (size=1)
'delete_confirm_password' =>
array (size=1)
0 => string 'The delete confirm password field is required.' (length=46)
protected 'format' => string ':message' (length=8)
json_encode($validation->messages) returns
{}
Question:如何在Laravel 4中以JSON struct 返回所有字段的所有验证错误消息?