Support Forum
Support › Meta Box AIO › File extension validation not working
Hi,
I'm having troubles with the file extension validation in a frontend form. Here's the code with the only relevant field.
add_filter( 'rwmb_meta_boxes', 'nvf_register_mb_candidatura_form_input' );
function nvf_register_mb_candidatura_form_input( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'Input Formulário', 'nvf-gb-front' ),
'id' => 'candidatura-form-details',
'post_types' => ['candidatura_bolsa'],
'fields' => [
[
'name' => __( 'Upload Requested Files', 'nvf-gb-front' ),
'id' => $prefix . 'uploaded_files',
'type' => 'file',
'max_file_uploads' => 20,
'force_delete' => true,
],
],
'validation' => [
'rules' => [
$prefix . 'uploaded_files' => [
'extension' => 'pdf',
],
],
'messages' => [
$prefix . 'uploaded_files' => [
'extension' => 'file extension not allowed',
],
],
],
];
return $meta_boxes;
}
Any clue on how to make it work?
I'm using MB AIO Version 1.14.3 and MB Version 5.4.6
Thanks in advance.
Hi,
Thank you for your feedback.
I've escalated this issue to the development team to fix it in the next update.
Hi,
The validation feature uses jQuery to check the input name instead of the input ID (field ID) so please change a little bit of the field ID in the rules
for two fields file
and image
to this '_file_' . $prefix . 'uploaded_files[]'
. The code should be:
add_filter( 'rwmb_meta_boxes', 'nvf_register_mb_candidatura_form_input' );
function nvf_register_mb_candidatura_form_input( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'Input Formulário', 'nvf-gb-front' ),
'id' => 'candidatura-form-details',
'post_types' => ['candidatura_bolsa'],
'fields' => [
[
'name' => __( 'Upload Requested Files', 'nvf-gb-front' ),
'id' => $prefix . 'uploaded_files',
'type' => 'file',
'max_file_uploads' => 20,
'force_delete' => true,
],
],
'validation' => [
'rules' => [
'_file_' . $prefix . 'uploaded_files[]' => [
'extension' => 'pdf',
],
],
'messages' => [
'_file_' . $prefix . 'uploaded_files[]' => [
'extension' => 'file extension not allowed',
],
],
],
];
return $meta_boxes;
}
Thank you for your support. The validation field messages are working now.
However, contrary to what happens with the other jQuery validation rules, these errors on the uploaded file extensions do not prevent a successful form submission, ending up with all files in the DB. Making the field required had no effect.
Any clue of that might be happening?
Hi,
I will check that issue and get back to you later.
Hi, Long.
I was wondering if you had any updates on this topic?
We're getting closer to deliver the project to the client. Is there any workaround you would recommend in case there's no fixing of this bug by the dev team meanwhile?
Thanks,
Henrique