Unable to Validate Required File Upload Correctly

Support General Unable to Validate Required File Upload Correctly

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #15292
    SLG MarketingSLG Marketing
    Participant

    Hi

    I've tried adding validation to a file upload box. This works brilliantly on the initial post creation but when I go back into the post to edit the content or title it throws a validation failure as the upload box is not filled out.

    Here is the code for my metabox:

    public function generateMetaBox($meta_boxes){
            $prefix = 'downloads';
    
            $prefix=str_replace(' ','_',$prefix);
    
            // General
            $meta_boxes[]=array(
                'id'=>'general',
                'title'=>'General Download Settings',
                'post_types' => 'downloads',
                'context'    => 'normal',
                'priority'   => 'high',
                'fields'=> array(
                    array(
                        'name'              =>  'File',
                        'id'                =>  $prefix.'_file',
                        'type'              =>  'file',
                        'desc'              =>  'Select the file from your computer to upload.',
                        'max_file_uploads'  =>  1,
                        'upload_dir'        =>  WP_CONTENT_DIR.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'downloads'
                    ),
                ),
                'validation' => array(
                    'rules'  => array(
                        '_file_downloads_file[]' => array(
                            'required'  => true,
                        ),
                    ),
                )
            );
    
            return $meta_boxes;
        }

    Is there a work around for this?

    Thanks!

    #15298
    Anh TranAnh Tran
    Keymaster

    Hi,

    Please use the 'require' => true parameter for the field. The validation doesn't work in this case.

    #15301
    SLG MarketingSLG Marketing
    Participant

    Thanks for the response. I'll make that change and pass it through to testing.

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