spivurno
4/16/2018 - 2:12 PM

Gravity Wiz // Gravity Forms // Conditional Submit Button: All Required

Gravity Wiz // Gravity Forms // Conditional Submit Button: All Required

<?php
/**
 * Gravity Wiz // Gravity Forms // Conditional Submit Button: All Required
 *
 * This snippet will automatically apply the necessary conditional logic to hide the submit button until all required
 * fields have been completed.
 *
 * NOTE: This will override any configured
 *
 * @version  0.1
 * @author   David Smith <david@gravitywiz.com>
 * @license  GPL-2.0+
 * @link     http://gravitywiz.com/...
 *
 * Plugin Name:  Gravity Forms Conditional Submit Button
 * Plugin URI:   http://gravitywiz.com/
 * Description:  This snippet will automatically apply the necessary conditional logic to hide the submit button until all required fields have been completed.
 * Author:       Gravity Wiz
 * Version:      0.1
 * Author URI:   http://gravitywiz.com
 */
class GW_Conditional_Submit_Button {

    public function __construct( $args = array() ) {

        // set our default arguments, parse against the provided arguments, and store for use throughout the class
        $this->_args = wp_parse_args( $args, array(
            'form_id'  => false
        ) );

        // do version check in the init to make sure if GF is going to be loaded, it is already loaded
        add_action( 'init', array( $this, 'init' ) );

    }

    public function init() {

        add_filter( 'gform_pre_render', array( $this, 'add_submit_button_conditional_logic' ) );
	    add_filter( 'gform_pre_render', array( $this, 'load_form_script' ) );

    }

    public function add_submit_button_conditional_logic( $form ) {

    	if( ! $this->is_applicable_form( $form ) ) {
    		return $form;
	    }

    	$logic = array(
		    'actionType' => 'hide',
		    'logicType'  => 'any',
		    'rules'      => array(),
	    );

    	foreach( $form['fields'] as $field ) {
    		if( $field->isRequired ) {
			    $logic['rules'][] = array(
				    'fieldId'  => $field->id,
				    'operator' => 'is',
				    'value'    => '',
			    );
		    }
	    }

	    $form['button']['conditionalLogic'] = $logic;

		return $form;
    }

	public function load_form_script( $form ) {

		if( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
			add_action( 'wp_footer', array( $this, 'output_script' ) );
			add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
		}

		return $form;
	}

	public function output_script() {
		?>

		<script type="text/javascript">

			( function( $ ) {

				/**
				 * Add support for matching field-is-empty conditional logic rules for Checkbox and Radio Button fields.
				 */
				gform.addFilter( 'gform_is_value_match', function( isMatch, formId, rule ) {

					var inputId         = rule['fieldId'],
						inputIndex      = gformExtractInputIndex( inputId ),
						isInputSpecific = inputIndex !== false,
						$inputs;

					if( isInputSpecific ) {
						return isMatch;
					}

					$inputs = $( 'input[id="input_{0}_{1}"], input[id^="input_{0}_{1}_"], input[id^="choice_{0}_{1}_"], select#input_{0}_{1}, textarea#input_{0}_{1}'.format( formId, rule.fieldId ) );

					var isCheckable = $.inArray( $inputs.attr( 'type' ), [ 'checkbox', 'radio' ] ) !== -1;
					if( isCheckable && rule.value == '' && $inputs.filter( ':checked' ).length <= 0 ) {
						isMatch = true;
					}

					return isMatch;
				} );

			} )( jQuery );

		</script>

		<?php
	}

    public function is_applicable_form( $form ) {

        $form_id = isset( $form['id'] ) ? $form['id'] : $form;

        return empty( $this->_args['form_id'] ) || $form_id == $this->_args['form_id'];
    }

}

# Configuration

new GW_Conditional_Submit_Button( array(
	'form_id' => 123,
) );