php 重力Wiz //重力形式//条件提交按钮:全部必需

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 重力Wiz //重力形式//条件提交按钮:全部必需相关的知识,希望对你有一定的参考价值。

<?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,
) );

以上是关于php 重力Wiz //重力形式//条件提交按钮:全部必需的主要内容,如果未能解决你的问题,请参考以下文章

php 重力Wiz //重力形式//显示下拉和单选按钮产品字段的价格

php 重力Wiz //重力形式//签到

php 重力Wiz //重力形式//时间敏感选择

php 重力Wiz //重力形式//货币转换器

php 重力Wiz //重力形式//修改自定义字段的日期格式

php 重力Wiz //重力形式//修改条目中的日期字段格式