[英]Jquery tooltipster validate file
I want to validate a file before upload it, the field file is required and should be in pdf extension. 我想在上传之前验证文件,字段文件是必需的,并且应该是pdf扩展名。 however when I want to validate it, the script don't show me the errors messages here is my code 但是,当我想验证它时,脚本不会向我显示错误消息,这里是我的代码
<input id="faxtype_file" name="faxtype[file]" class="file custom-file-upload-hidden" style="position: absolute; left: -9999px;" tabindex="-1" type="file"><input class="file-upload-input" type="text"><button type="button" class="file-upload-button" tabindex="-1">upload</button>
and in my jquery file: 在我的jquery文件中:
jQuery.validator.addMethod("regexphone", function (value, element, regexp) {
if (regexp.constructor != RegExp)
regexp = new RegExp(regexp);
else if (regexp.global)
regexp.lastIndex = 0;
return this.optional(element) || regexp.test(value);
}, "");
$('#formulaire :input').tooltipster({
trigger: 'custom', // default is 'hover' which is no good here
onlyOne: false, // allow multiple tips to be open at a time
position: 'right' // display the tips to the right of the element
});
$('#formulaire').validate({ // initialize the plugin
errorPlacement: function (error, element) {
$(element).tooltipster('update', $(error).text());
$(element).tooltipster('show');
},
success: function (label, element) {
$(element).tooltipster('hide');
},
rules: {
'faxtype[faxList]': {
required: true,
},
'faxtype[file]':{
required:true,
extension: "docx|rtf|doc|pdf"
}
},
messages: {
'faxtype[faxList]': {
required: "{{ 'message.faxm.listnumber'|trans }}",
},
'faxtype[file]':{
required:"le fichier est obligatoir",
extension: "l'extension doit etre pdf"
}
},
});
not that the script validate the faxlist field but not the filed field. 不是脚本验证传真列表字段而不是归档字段。
in my twig : 在我的树枝上:
<div class="custom-file-upload">
<!--<label for="file">File: </label>-->
{{ form_widget(form.file, { 'attr': {'class':'file '} }) }} <br/><br/>
</div>
any idea please about how I can validate the file. 请问我如何验证文件。
You should check the mimetype of the uploaded file by adding a constraint in the attribute file in your entity. 您应该通过在实体的属性文件中添加约束来检查上载文件的mimetype。
YourEntity
....
/**
* @Assert\File(
* maxSize = "2M",
* mimeTypes = {"application/pdf", "application/x-pdf"},
* mimeTypesMessage = "You should upload a pdf file"
* )
*/
private $faxtype;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.