Laravel 通过 jquery submit() 提交表单后的文件验证;
Laravel file validation after submitting form via jquery submit();
当我尝试通过 jquery 提交表单后验证图像文件时
$('#logo').submit();
我总是收到验证错误
The img field is required
这是我用来验证文件的验证
$input = array('img' => Input::file('img'));
$rules = array(
'img' => 'required',
);
// Now pass the input and rules into the validator
$validator = Validator::make($input, $rules);
// check if the validator failed -----------------------
if ($validator->fails()) {
// get the error messages from the validator
$messages = $validator->messages();
// redirect our user back to the form with the errors from the validator
print_r($messages);
}
我没能确定脚本有什么问题,
帮助我克服这个问题。
提前致谢。
如果您提交的表单包含 <input type="file">
和 ajax/jquery
,这几乎是不可能的,除非您使用类似 Jquery
file upload
或 dropzone.js
, 等。但是您仍然可以使用 XMLHttpRequest
自行完成此操作。下面的例子:
var forms = document.querySelector('form#yourFormId');
var request = new XMLHttpRequest();
var formDatas = new FormData(forms);
request.open('post','upload');
//Here "upload" is your post route
request.send(formDatas);
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (request.status === 200) {
//The request was successful
}else{
//Request wasn't OK
}
那么你应该可以在控制器中调用你输入的文件名:
Input::get('inputFileName')
要将 jQuery + ajax 的文件提交到 Laravel,我遵循以下方法:
示例表格:
<form class="form-horizontal" role="form" method="POST" id="imageUpload" action="some/backend/method" enctype="multipart/form-data">
<div class="form-group">
<div class="alert alert-danger" style="display: none;"></div>
<label class="col-sm-3 control-label no-padding-right"> <i class="fa fa-asterisk light-red"></i> Image</label>
<div class="col-sm-9">
<input type="file" name="image" class="form-control" required/>
</div>
</div>
</form>
Note: set an ID to the form.
示例ajax请求:
$.ajax({
type: 'POST',
data: new FormData($("#imageUpload")[0]),
url: 'some/backend/method',
processData: false,
contentType: false
}).done(function (result) {
if ($.isEmptyObject(result.error)) {
window.location.reload();
} else {
var messages = '';
for (var key in result.error) {
if (result.error.hasOwnProperty(key)) {
messages += result.error[key] + '<br />';
}
}
$("#imageUpload .alert").html(messages).css('display', 'block');
}
}, "json");
Note: processData: false
& contentType: false
are quite important.
在后端 (Laravel):
$validator = Validator::make(Input::all(), [
'image' =>'required'
]);
if ($validator->fails()) {
return Response::json(['error' => $validator->messages()]);
} else {
return Response::json(['success' => true]);
}
当我尝试通过 jquery 提交表单后验证图像文件时
$('#logo').submit();
我总是收到验证错误
The img field is required
这是我用来验证文件的验证
$input = array('img' => Input::file('img'));
$rules = array(
'img' => 'required',
);
// Now pass the input and rules into the validator
$validator = Validator::make($input, $rules);
// check if the validator failed -----------------------
if ($validator->fails()) {
// get the error messages from the validator
$messages = $validator->messages();
// redirect our user back to the form with the errors from the validator
print_r($messages);
}
我没能确定脚本有什么问题, 帮助我克服这个问题。 提前致谢。
如果您提交的表单包含 <input type="file">
和 ajax/jquery
,这几乎是不可能的,除非您使用类似 Jquery
file upload
或 dropzone.js
, 等。但是您仍然可以使用 XMLHttpRequest
自行完成此操作。下面的例子:
var forms = document.querySelector('form#yourFormId');
var request = new XMLHttpRequest();
var formDatas = new FormData(forms);
request.open('post','upload');
//Here "upload" is your post route
request.send(formDatas);
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (request.status === 200) {
//The request was successful
}else{
//Request wasn't OK
}
那么你应该可以在控制器中调用你输入的文件名:
Input::get('inputFileName')
要将 jQuery + ajax 的文件提交到 Laravel,我遵循以下方法:
示例表格:
<form class="form-horizontal" role="form" method="POST" id="imageUpload" action="some/backend/method" enctype="multipart/form-data">
<div class="form-group">
<div class="alert alert-danger" style="display: none;"></div>
<label class="col-sm-3 control-label no-padding-right"> <i class="fa fa-asterisk light-red"></i> Image</label>
<div class="col-sm-9">
<input type="file" name="image" class="form-control" required/>
</div>
</div>
</form>
Note: set an ID to the form.
示例ajax请求:
$.ajax({
type: 'POST',
data: new FormData($("#imageUpload")[0]),
url: 'some/backend/method',
processData: false,
contentType: false
}).done(function (result) {
if ($.isEmptyObject(result.error)) {
window.location.reload();
} else {
var messages = '';
for (var key in result.error) {
if (result.error.hasOwnProperty(key)) {
messages += result.error[key] + '<br />';
}
}
$("#imageUpload .alert").html(messages).css('display', 'block');
}
}, "json");
Note:
processData: false
&contentType: false
are quite important.
在后端 (Laravel):
$validator = Validator::make(Input::all(), [
'image' =>'required'
]);
if ($validator->fails()) {
return Response::json(['error' => $validator->messages()]);
} else {
return Response::json(['success' => true]);
}