定制 paperscissorsandglue/form-builder 二次开发

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

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

paperscissorsandglue/form-builder

最新稳定版本:0.33

Composer 安装命令:

composer require paperscissorsandglue/form-builder

包简介

A flexible form builder for PHP applications

README 文档

README

FormBuilder is a flexible PHP library for creating and rendering HTML forms. It provides an intuitive API for defining form fields, setting attributes, and handling form submissions.

Installation

You can install FormBuilder via Composer:

composer require paperscissorsandglue/formbuilder

Usage
Basic Usage
use PaperScissorsAndGlue\FormBuilder\FormBuilder;

$form = new FormBuilder();

$form->add('name', 'text', ['label' => 'Name'])
     ->add('email', 'text', ['label' => 'Email'])
     ->add('submit', 'submit', ['label' => 'Submit'])
     ->method('POST')
     ->url('/submit')
     ->class('my-form');

echo $form->render();

Field Types
FormBuilder supports the following field types:

text
textarea
checkbox
radio
select
number
hidden
submit


Adding Fields
Use the add method to add fields to your form:
$form->add('fieldName', 'fieldType', [
    'label' => 'Field Label',
    'attr' => ['class' => 'form-control'],
    // Other options...
]);

Field Options
Each field type supports various options:

label: The label for the field
attr: An array of HTML attributes for the field
placeholder: Placeholder text for text inputs
choices: An array of options for select, radio, and checkbox fields
empty_value: An initial empty option for select fields
wrapper: Wrap the field in a container (e.g., for Bootstrap)
label_attr: Attributes for the label element
errors: Configure error display for the field


Form Attributes
Set form attributes using method chaining:
$form->method('POST')
     ->url('/submit')
     ->class('my-form');

CSRF Protection
Add CSRF protection to your form:
$form->csrf('your_csrf_token_here');

Model Binding
Bind a model to pre-fill form values:
$user = new User(['name' => 'John Doe', 'email' => 'john@example.com']);
$form->model($user);

Embedded Forms
You can embed one form inside another:
$addressForm = new FormBuilder();
$addressForm->add('street', 'text', ['label' => 'Street'])
            ->add('city', 'text', ['label' => 'City']);

$mainForm = new FormBuilder();
$mainForm->add('name', 'text', ['label' => 'Name'])
         ->add('address', 'form', [
             'form' => $addressForm,
             'formOptions' => ['class' => 'embedded-form']
         ]);

Rendering
Render the entire form:
echo $form->render();

Or render individual fields:
echo $form->name;
echo $form->email;

Customization
FormBuilder is designed to be easily customizable. You can extend the FormBuilder class to add your own field types or modify existing ones.

Contributing
Contributions are welcome! Please feel free to submit a Pull Request.

License
This project is open-sourced software licensed under the MIT license.

This updated README.md file includes information about the new functionality you've added, such as:

1. Additional field types (number, hidden)
2. More detailed explanation of field options
3. Information about embedded forms
4. CSRF protection
5. Model binding
6. Rendering individual fields

The README now provides a comprehensive guide to using the FormBuilder class, covering all the main features and usage patterns.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-29