Custom Fields
Re-Envisioned
Support General Unable to Validate Required File Upload Correctly

This topic contains 2 replies, has 2 voices, and was last updated by  SLG Marketing 4 months, 1 week ago.

  • Creator
    Topic
  • #15292

    SLG 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!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #15298

    Anh Tran
    Keymaster

    Hi,

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

    #15301

    SLG Marketing
    Participant

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

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.