I want to validate a file before upload it, the field file is required and should be in pdf extension. 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.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.
YourEntity
....
/**
* @Assert\File(
* maxSize = "2M",
* mimeTypes = {"application/pdf", "application/x-pdf"},
* mimeTypesMessage = "You should upload a pdf file"
* )
*/
private $faxtype;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.