Require a minimum number of files in multi file uploader

Experimental Snippet 🧪

Code

Filename: gw-minimum-file-count.php

<?php
/**
 * Gravity Wiz // Gravity Forms // Require a minimum number of files in multi file uploader
 * https://gravitywiz.com/
 *
 * Experimental Snippet 🧪
 */
add_filter( 'gform_validation', function ( $result ) {
	$form = $result['form'];

	/* Customize the following variables. */
	$minimum_number_of_files = 4;
	$form_id_to_validate     = 2;
	$field_id_to_validate    = 1;
	$validation_message      = 'Minimum number of files not met.';
	/* End customizing */

	if ( $form['id'] !== $form_id_to_validate ) {
		return $result;
	}

	foreach ( $form['fields'] as &$field ) {

		if ( $field->id !== $field_id_to_validate ) {
			continue;
		}

		if ( empty( GFFormsModel::$uploaded_files[ $form['id'] ] ) ) {
			continue;
		}

		$input_name     = 'input_' . $field->id;
		$uploaded_files = GFFormsModel::$uploaded_files[ $form['id'] ][ $input_name ];

		if ( count( $uploaded_files ) < $minimum_number_of_files ) {
			$field['failed_validation']  = true;
			$field['validation_message'] = $validation_message;
			$result['is_valid']          = false;
		}
	}

	$result['form'] = $form;

	return $result;
} );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.