我在Laravel 5中创建了一个表单,其中包含产品名称,产品主图像和次映像。图像字段和辅助字段都是可选的。
public String findMovieBy(ModelMap modelMap, @RequestParam(required = true) String searchCriteria,
@RequestParam(required = true) String criteriaValue) {
Movie movie = movieService.findByProperty(searchCriteria, criteriaValue);
我在请求文件中的验证是:
<div class="form-group {{ ($errors->has('title')) ? 'has-error' : '' }}">
<label>Title *</label>
<input class="form-control validate[required]" name="title" type="text" value="{{ Input::old('title') }}">
{{ ($errors->has('title') ? $errors->first('title') : '') }}
</div>
<div class="form-group">
<label for="featured_image">Cover Image
<small>(optional)</small>
</label>
<input type="file" placeholder="" id="featured_image" name="featured_image">
<small class="description"> Maximum file size: 2 MB.</small>
{{ ($errors->has('title') ? $errors->first('featured_image') : '') }}
</div>
<div class="form-group">
<label for="gallery_images">Gallery Images
<small>(optional)</small>
</label>
<input type="file" placeholder="" id="gallery_images" name="gallery_images[]" multiple="">
<small class="description">Maximum file size: 2 MB.</small>
{{ ($errors->has('title') ? $errors->first('gallery_images') : '') }}
</div>
但它始终检查图像上传是否上传。哪个不正确,只有在上传图像时才检查图像类型。
谢谢。
答案 0 :(得分:0)
您应该在上传之前进行这些检查以获得最佳效果。下面的例子使用了ajax form jquery插件
$(document).ready(function() {
var options = {
target: '#output',
beforeSubmit: beforeSubmit,
uploadProgress: OnProgress,
success: afterSuccess,
resetForm: true
};
$('#my-upload-form').submit(function() {
$(this).ajaxSubmit(options);
return false;
});
});
function OnProgress(event, position, total, percentComplete) {
// Some in progress thingy
}
function afterSuccess() {
// OK msg
}
function beforeSubmit() {
if (window.File && window.FileReader && window.FileList && window.Blob) {
if( !$('#image').val()) {
return false
}
var fsize = $('#image')[0].files[0].size;
var ftype = $('#image')[0].files[0].type;
switch(ftype) {
case 'image/jpeg': case 'image/pjpeg':
break;
default:
// Input not supported
return false
}
if(fsize>10485760) {
// Input too large
return false
}
} else {
// Upgrade browser msg
return false;
}
}
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 Bytes';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
}