ride/lib-form

Ride框架的表单库

1.2.0 2024-06-26 08:35 UTC

README

Ride PHP框架的表单库。

库中包含的内容

RowFactory

使用 RowFactory 接口可以根据简单名称创建一个 Row。使用此工厂增加了表单的灵活性,因为如果需要,每个行都可以用另一个实现覆盖。

GenericRowFactory 类提供了默认实现。

Row

使用 Row 接口来实施行类型。这可以是一个标量类型,如字符串、数字等,所有通用HTML表单元素。它也可以是一个组件或组件集合。

此库提供的以下行

  • button
  • collection
  • component
  • date
  • email
  • file
  • hidden
  • image
  • label
  • number
  • object
  • option
  • password
  • select
  • string
  • text
  • time
  • website
  • wysiwyg

Component

使用 Component 接口可以将多个行的组合分组为单个行或表单。例如,日期时间组件可以将日期行和时间行组合成一个行。创建组件将增加您表单的可重用性。

Widget

使用 Widget 接口来显示 Row。某些行可以有相同的逻辑但不同的视图表示。考虑一个选项行,它可以表示为复选框列表(或单选按钮)或选择字段。

View

使用 View 接口将表单发送到UI。它删除了构建表单所需的所有内容,并确保表单可序列化。

代码示例

查看此代码示例以了解此库的一些可能性

<?php

function createForm(Form $form) {
    // id of the form
    $form->setId('form-example');
    // action to catch submission of different forms on one page
    $form->setAction('submit-example');
    
    $form->addRow('name', 'string', array(
        'label' => 'Name',
        'description' => 'Enter your name',
        'filters' => array(
            'trim' => array(),
        ), 
        'validators' => array(
            'required' => array(),
        ),
    ));
    $form->addRow('gender', 'option', array(
        'label' => 'Gender',
        'description' => 'Select your gender',
        'default' => 'F',
        'options' => array(
            'F' => 'Female',
            'M' => 'Male',
            'O' => 'Other', 
        ),
        'validators' => array(
            'required' => array(),
        ),
    ));
    $form->addRow('extra', 'string', array(
        'label' => 'Extra',
        'description' => 'Extra row to show off some other options',
        'multiple' => true,
        'disabled' => false,
        'readonly' => false,
        'attributes' => array(
            'data-extra' => 'An extra attribute for the HTML element',
        ),
    ));
    
    return $form->build();
}

相关模块

安装

您可以使用 Composer 来安装此库。

composer require ride/app-form