【发布时间】:2015-06-01 23:53:54
【问题描述】:
我尝试使用谷歌搜索并看到此论坛上发布的其他问题,但找不到任何解决我的问题的方法。我正在使用 Jquery ajaxForm 方法提交表单。我的表单中也包含一个 file 字段,可用于上传图片。我已经在我的模型中定义了验证。但问题是即使我上传了正确的jpg 文件,我仍然收到错误消息
Argument 1 passed to Illuminate\\Validation\\Factory::make() must be of the type array, object given.
Javascript 代码
$('#create_form').ajaxForm({
dataType:'JSON',
success: function(response){
alert(response);
}
}).submit();
控制器代码
if ($file = Input::file('picture')) {
$validator = Validator::make($file, User::$file_rules);
if ($validator->fails()) {
$messages = $validator->messages();
foreach ($messages->all(':message') as $message) {
echo $message; exit;
}
return Response::json(array('message'=>$response, 'status'=>'failure'));
} else {
// do rest
}
}
型号代码
public static $file_rules = array(
'picture' => 'required|max:2048|mimes:jpeg,jpg,bmp,png,gif'
);
POST 请求
我知道我在模型中定义的验证需要一个数组。但是通过在验证器中传递$file,传递了一个对象。然后我将代码更改为:
$validator = Validator::make(array('picture' => $file->getClientOriginalName()), User::$file_rules);
现在我收到错误:
The picture must be a file of type: jpg, JPEG, png,gif.
【问题讨论】:
标签: validation file-upload laravel-4 ajaxform