assegaiphp/forms 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

assegaiphp/forms

最新稳定版本:0.9.0

Composer 安装命令:

composer require assegaiphp/forms

包简介

A simple PHP library for dealing with HTML form submissions.

README 文档

README

Assegai Logo

Latest release Tests PHP 8.4+ License Status active

A progressive PHP framework for building effecient and scalable server-side applications.

Description

Assegai is a framework for building efficient, scalable PHP server-side applications. It uses modern PHP (PHP 8.4+) and combines elements of OOP (Object Oriented Programming) and FP (Functional Programming).

Overview

The AssegaiPHP Forms Library is a powerful and flexible tool for managing HTML forms submitted using POST, PUT, or PATCH requests. This library is designed to simplify the process of handling form data, validation, and submission in PHP web applications. It provides a clean and intuitive interface for creating, processing, and validating forms, making it easier for developers to build robust and secure web applications.

Contribution workflow

For commit and pull request conventions in this repo, see:

Git hooks for this repository live in .githooks. Running composer install or composer update will automatically configure core.hooksPath for this clone so the committed pre-push checks are used. If you need to apply the hook configuration manually, run:

composer run hooks:install

Features

  • Form Creation: Easily create HTML forms programmatically using a simple and intuitive syntax.
  • Form Fields: Support for various types of form fields such as text fields, numeric fields, and more.
  • Data Binding: Automatically populate form fields with data from your models or arrays.
  • Validation: Define validation rules for form fields and perform server-side validation effortlessly.
  • Error Handling: Automatically retrieve validation errors for form fields.
  • Customization: Highly customizable rendering and extending capabilities.
  • Compatibility: Works well with modern PHP applications and follows best practices.

Installation

You can install the AssegaiPHP Forms Library using Composer:

composer require assegaiphp/forms

Quick Start

  1. Create a Form:
    use Assegai\Forms\Form;
    use Assegai\Forms\Enumerations\HttpMethod;
    
    $form = new Form(
        method: HttpMethod::POST,
        selector: '#contact-form'
    );
  2. Add Form Fields:
    $form->set('name', '');
    $form->set('email', '');
    $form->set('message', '');
  3. Process Form Submission:
    if ($form->isSubmitted()) {
        // Validate the form
        $form->validate();
        if ($form->isValid()) {
            // Process the form data
            $data = $form->getData();
            // ...
        } else {
            $errors = $form->getErrors();
            // Handle validation errors
        }
    }
  4. Render the Form:
    echo $form->render();

Advanced Usage

Adding Validation Rules

use Assegai\Forms\Form;
use Assegai\Forms\Enumerations\HttpMethod;
use Assegai\Forms\FormControls\TextField;

$form = new Form(method: HttpMethod::POST, selector: '#user-form');

// Create a field with validation rules
$nameField = new TextField('name', '', ['required', 'min:3']);
$form->addField($nameField);

// Or add fields and set validation rules later
$form->set('email', '');
$emailField = $form->getField('email');
$emailField->addValidationRules('required', 'email');

Getting Form Data

// Get data as an associative array
$data = $form->getData();

// Get data as a stdClass object
$dataObject = $form->getData(asObject: true);

Working with Individual Fields

// Check if a field exists
if ($form->has('name')) {
    // Get a specific field value
    $name = $form->getFieldValue('name');

    // Get the field object
    $nameField = $form->getField('name');

    // Remove a field
    $form->removeField('name');
}

Form HTTP Methods

The form supports multiple HTTP methods:

use Assegai\Forms\Enumerations\HttpMethod;

// POST forms
$postForm = new Form(method: HttpMethod::POST, selector: '#form');

// GET forms
$getForm = new Form(method: HttpMethod::GET, selector: '#search');

// PUT/PATCH forms for updates
$updateForm = new Form(method: HttpMethod::PUT, selector: '#update-form');
$patchForm = new Form(method: HttpMethod::PATCH, selector: '#patch-form');

Form Selector

The selector parameter can be used to set the form's ID, CSS class, or action URL:

// Set the form ID
$form = new Form(selector: '#my-form');

// Set the form CSS classes
$form = new Form(selector: '.form-class1.form-class2');

// Set the form action URL
$form = new Form(selector: '/submit-form');

For more detailed usage and customization options, please refer to the Documentation.

Contributing

We welcome contributions from the community! If you'd like to contribute to the AssegaiPHP Forms Library, please follow our Contribution Guidelines.

License

The AssegaiPHP Forms Library is open-source software licensed under the MIT License.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固