定制 simp/forms 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

simp/forms

最新稳定版本:v1.1.7

Composer 安装命令:

composer require simp/forms

包简介

This is form builder library

README 文档

README

Overview

This PHP-based form library simplifies form creation and management. It provides a structured way to define form fields, validate inputs, and render forms efficiently.

Features

  • Modular form field definitions
  • Custom field validation
  • Extendable form builder
  • Default field implementations
  • Easy-to-use API for seamless integration

Installation

Ensure Composer is installed, then run:

composer install simp/forms

Usage

Example Form

The Example.php file demonstrates form creation:

<?php

require_once "vendor/autoload.php";

use Simp\FormBuilder\FormBase;

class Example extends FormBase
{
    public function getFormId(): string
    {
        return "example_form";
    }

    public function buildForm(array &$form): array
    {
        $this->setFormAction('index1.php');

        $form[] = [
            'name' => 'first_name',
            'label' => 'First Name',
            'type' => 'text',
            'id' => 'first_name',
            'class' => ['form-control', 'form-control-sm'],
            'options' => ['placeholder' => 'Enter your first name'],
            'required' => true,
        ];

        $form[] = [
            'name' => 'gender',
            'label' => 'Gender',
            'type' => 'select',
            'id' => 'gender',
            'class' => ['form-control', 'form-control-sm'],
            'default_value' => 'male',
            'option_values' => ['male' => 'Male', 'female' => 'Female', 'other' => 'Other'],
            'handler' => \Simp\Default\SelectField::class,
        ];

        $form[] = [
            'name' => 'profile_image[]',
            'label' => 'Profile Image',
            'type' => 'file',
            'id' => 'profile_image',
            'class' => ['form-control', 'form-control-sm'],
            'options' => ['accept' => 'image/*', 'multiple' => 'multiple'],
            'handler' => \Simp\Default\FileField::class,
            'required' => true,
        ];

        $form[] = [
            'type' => 'submit',
            'name' => 'submit',
            'id' => 'submit',
            'class' => ['btn', 'btn-primary'],
            'default_value' => 'Submit',
        ];

        return $form;
    }

    public function validateForm(array $form): void
    {
        foreach ($form as $field) {
            $value = $field->getValue();
            if (empty($value) && !empty($field->getRequired())) {
                $field->setError($field->getLabel() . ' is required!');
            }
        }
    }

    public function submitForm(array &$form): void
    {
        print_r($form);
    }
}

Rendering the Form

Use index.php to render the form:

require_once "vendor/autoload.php";
require_once "Example.php";

try {
    $form_base = new \Simp\FormBuilder\FormBuilder(new Example());
    
    $form_base->getFormBase()->setFormMethod('POST');
    $form_base->getFormBase()->setFormEnctype('multipart/form-data');
    $form_base->getFormBase()->isFormSilent(true);
    $form_base->getFormBase()->setSilentHandler(['submit_handler']);
    $form_base->getFormBase()->setIsJsAllowed(true);
    $form_base->getFormBase()->setJsLibrary(['/main.js']);
    $form_base->getFormBase()->setFieldJsEvents('change', ['handle_on_change']);
    
    echo $form_base;
} catch (Exception $e) {
    echo "Error rendering form: " . $e->getMessage();
}

Extending Form Fields

To create custom fields, extend the FieldBase class. Check the Default field implementations for reference.

Contributing

Feel free to contribute by submitting issues or pull requests to improve this library.

License

This project is licensed under the MIT License.

统计信息

  • 总下载量: 101
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-03