alexmigf/forma

WordPress Admin 简单表单 API 包

1.0.0 2023-01-25 19:06 UTC

This package is auto-updated.

Last update: 2024-09-25 22:36:59 UTC


README

WordPress Admin 简单表单 API composer 包

要求

  • PHP >= 7.0
  • WordPress >= 4.4

安装

composer require alexmigf/forma

使用

首先配置表单设置

$args = [
	'title'        => 'My form title',                    // form title (default: '')
	'classes'      => '',                                 // additional CSS form classes (default: '')
	'action'       => '',                                 // file to process the form data (default: '')
	'method'       => 'post',                             // the form request method (default: 'post')
	'enctype'      => '',                                 // encoding type (default: 'application/x-www-form-urlencoded')
	'callback'     => null,                               // callback function to process the form data (default: null)
	'nonce'        => true,                               // nonce validation, if data is handled by this package (default: false)
	'button_text'  => 'Send',                             // submit button text (default: Send)
	'redirect_uri' => '',                                 // URL for redirection after submit (default: '')
	'messages'     => [ 'error' => '', 'success' => '' ], // form custom  messages (default: null)
];

通过传递 ID 和配置数组实例化表单类

$forma = new Alexmigf\Forma( $id = 'new-form', $args );

添加分区以分组多个字段

$forma->add_section( $section_id = 'profile' );

添加单个字段

$field = [
	'type'     => 'text',
	'id'       => 'first_name',
	'label'    => __( 'First name', 'textdomain' ),
	'value'    => '',
	'required' => true,
];
$forma->add_field( $field, $section_id = 'profile' );

/* or */

$forma->add_field( $field ); // fields without section became 'orphans', non grouped

一次性添加多个字段

$fields = [
	[
		'type'     => 'text',
		'id'       => 'first_name',
		'label'    => __( 'First name', 'textdomain' ),
		'value'    => '',
		'required' => true,
	],
	[
		'type'     => 'text',
		'id'       => 'last_name',
		'label'    => __( 'Last name', 'textdomain' ),
		'value'    => '',
		'required' => true,
	],
	[
		'type'     => 'text',
		'id'       => 'company',
		'label'    => __( 'Company', 'textdomain' ),
		'value'    => '',
		'required' => false,
	],
	[
		'type'     => 'text',
		'id'       => 'position',
		'label'    => __( 'Position', 'textdomain' ),
		'value'    => '',
		'required' => false,
	],
	[
		'type'     => 'email',
		'id'       => 'email',
		'label'    => __( 'Email', 'textdomain' ),
		'value'    => '',
		'required' => false,
	],
	[
		'type'     => 'tel',
		'id'       => 'phone_number',
		'label'    => __( 'Phone number', 'textdomain' ),
		'value'    => '',
		'required' => false,
	],
];

$forma->add_fields( $fields, 'profile' );

添加自定义隐藏字段

$forma->add_hidden_field( $name = 'custom_hidden_field', $value = '1' );

添加自定义 nonce 字段

$forma->add_nonce_field( $action = 'custom_nonce_action' );

显示表单

$forma->render();

传递值到字段

如果您需要在字段中显示当前保存的数据。

$data = $database->get_entries();
$forma->add_fields( form_fields( $data ), $section_id = 'profile' );

function form_fields( $data = [] ) {
	return [
		[
			'type'     => 'text',
			'id'       => 'first_name',
			'label'    => __( 'First name', 'textdomain' ),
			'value'    => isset( $data['first_name'] ) ? $data['first_name'] : '',
			'required' => true,
		],
		[
			'type'     => 'text',
			'id'       => 'last_name',
			'label'    => __( 'Last name', 'textdomain' ),
			'value'    => isset( $data['last_name'] ) ? $data['last_name'] : '',
			'required' => true,
		],
		[
			'type'     => 'text',
			'id'       => 'company',
			'label'    => __( 'Company', 'textdomain' ),
			'value'    => isset( $data['company'] ) ? $data['company'] : '',
			'required' => false,
		],
		[
			'type'     => 'text',
			'id'       => 'position',
			'label'    => __( 'Position', 'textdomain' ),
			'value'    => isset( $data['position'] ) ? $data['position'] : '',
			'required' => false,
		],
		[
			'type'     => 'email',
			'id'       => 'email',
			'label'    => __( 'Email', 'textdomain' ),
			'value'    => isset( $data['email'] ) ? $data['email'] : '',
			'required' => false,
		],
		[
			'type'     => 'tel',
			'id'       => 'phone_number',
			'label'    => __( 'Phone number', 'textdomain' ),
			'value'    => isset( $data['phone_number'] ) ? $data['phone_number'] : '',
			'required' => false,
		],
	];
}

处理回调

如果提供了表单回调函数,则 nonce 验证在 alexmigf\forma 包内部完成,该包返回要由回调处理的请求数据。

function new_form_process_callback( $request ) {
	// $request contains the data to be processed
	// do your stuff and return bool

	$response = false;
	if ( ! empty( $request ) ) {
		$response = $database->insert( $request );
	}
	return $response;
}

支持的字段类型

  • 隐藏
  • 提交
  • 复选框
  • 电话
  • 数字
  • 电子邮件
  • 日期
  • 选择
  • 多行文本框
  • 网址
  • 文本
  • 文件