text 使用表单作为另一个表单的字段模板。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 使用表单作为另一个表单的字段模板。相关的知识,希望对你有一定的参考价值。

add_filter( 'gform_form_post_get_meta_7', 'add_form_fields' );
function add_form_fields( $form ) {

	// Don't make the parent form messy
	if ( ! GFCommon::is_form_editor() ) {

		// form we are adding fields to
		$parent_id = $form['id'];
		// template form ID
		$childId = 5;
		// we need a unique prefix to prevent field ID conflicts
		$prefix = '891794134';
		// get template form using Gravity Forms API
		$template = GFAPI::get_form( $childId );

		if ( $template ) {

			foreach ( $template['fields'] as $field ) {

				// Create unique field id using prefix and previous field ID
				$field_id = $prefix . $field['id'];

				// Map field inputs id to prefix
				$field_inputs = $field['inputs'];
				if ( ! empty ( $field_inputs ) ) {
					foreach ( $field_inputs as $key => $input ) {
						$inputId                    = explode( '.', $input['id'], 2 );
						$field_inputs[ $key ]['id'] = $field_id . '.' . $inputId[1];
					}
				}

				// Map conditional logic fieldId to prefix
				$field_conditions = $field['conditionalLogic'];
				if ( ! empty ( $field['conditionalLogic'] ) ) {
					foreach ( $field['conditionalLogic']['rules'] as $key => $rule ) {
						$newId                                        = $prefix . $rule['fieldId'];
						$field_conditions['rules'][ $key ]['fieldId'] = $newId;
					}
				}

				// Map field parameters
				$field_names[] = GF_Fields::create( array(
					'label'             => $field['label'],
					'adminLabel'        => $field['adminLabel'],
					'type'              => $field['type'],
					'id'                => $field_id,
					'formId'            => $parent_id,
					'pageNumber'        => $field['pageNumber'],
					'dbParam'           => $field['dbParam'],
					'nameFormat'        => $field['nameFormat'],
					'inputs'            => $field_inputs,
					'choices'           => $field['choices'],
					'isRequired'        => $field['isRequired'],
					'visibility'        => $field['visibility'],
					'defaultValue'      => $field['defaultValue'],
					'dateFormat'        => $field['dateFormat'],
					'dateType'          => $field['dateType'],
					'conditionalLogic'  => $field_conditions,
					'errorMessage'      => $field['errorMessage'],
					'calendarIconType'  => $field['calendarIconType'],
					'phoneFormat'       => $field['phoneFormat'],
					'size'              => $field['size'],
					'inputMask'         => $field['inputMask'],
					'inputMaskValue'    => $field['inputMaskValue'],
					'inputMaskIsCustom' => $field['inputMaskIsCustom'],
					'cssClass'          => $field['cssClass'],
					'allowsPrepopulate' => $field['allowsPrepopulate'],
					'oldId'             => $field['id']
				) );

			}

			// Add fields to form
			foreach ( $field_names as $newField ) {
				$form['fields'][] = $newField;
			}
		}
	}

	return $form;
}

以上是关于text 使用表单作为另一个表单的字段模板。的主要内容,如果未能解决你的问题,请参考以下文章

如何从表单中排除字段?

使用更改 django 模板中表单字段的名称属性

使用自定义模板时 Django 表单字段的初始值

Jquery复制表单输入密钥

重新排序模型表单字段

如何将表单视图作为 django 中的上下文传递给另一个视图?