Custom Fields
Re-Envisioned
Support Meta Box AIO File extension validation not working

  • Creator
    Topic
  • #30687
    team@novify.pt
    Participant

    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #30692
    Long Nguyen
    Moderator

    Hi,

    Thank you for your feedback.

    I've escalated this issue to the development team to fix it in the next update.

    #30719
    Long Nguyen
    Moderator

    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;
    }
    #30798
    team@novify.pt
    Participant

    form validation label working

    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?

    #30805
    Long Nguyen
    Moderator

    Hi,

    I will check that issue and get back to you later.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.