在Laravel验证输入:: JSON() [英] Validating Input::json() in Laravel
问题描述
验证不输入:: json的工作。香港专业教育学院尝试使用json_de code /使用数组,但仍然没有运气的不同方式。这里是我的code:
Validation does not work with Input::json. Ive tried different ways using json_decode/using arrays but still no luck. Here's my code:
//routes.php
Route::get('create', function() {
$rules = array(
'username' => 'required',
'password' => 'required',
);
$input = Input::json();
//var_dump($input); //outputs std obj with correct inputs
$validation = Validator::make($input, $rules);
if ($validation->fails()) { //throws exeption "Call to a member function to_array() on a non-object"
return Response::json($validation->errors->all());
}
}
我张贴采用了棱角分明的资源数据......但它总是抛出一个错误调用一个成员函数to_array()一个非对象......我不能贴上我的角度code,因为我不能正确格式化和计算器不允许我这样做。
I'm posting the data using Angular Resource...but it always throws an error "Call to a member function to_array() on a non-object"...I cant paste my angular code since I can't format it correctly and stackoverflow doesn't allow me to do that.
推荐答案
这是因为在输入:: JSON()返回一个对象,并验证方法既期待数组或对象的口才。你可以做的是将对象转换为一个数组。
That is because in the input::json() returns an object and validation method expects either array or eloquent object. What you can do is convert the object to an array.
$input = Input::json();
$input_array = (array)$input;
$validation = Validator::make($input_array, $rules);
更新:
与@Ryan讨论后,我发现这个问题是不是从验证,但在响应::佞()与数组而不是一个雄辩传入的对象。
After discussing with @Ryan, I noticed the problem is not from the validation, but in the the response::eloquent() was passed with an array instead of an eloquent object.
这篇关于在Laravel验证输入:: JSON()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!