b2pweb/bdf-form-bundle

BDF 表单的组件包

安装次数: 1,236

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 0

公开问题: 0

类型:symfony-bundle

v1.1.2 2024-09-25 14:38 UTC

This package is auto-updated.

Last update: 2024-09-25 14:40:57 UTC


README

BDF 表单组件包

build Packagist Version Total Downloads

安装

composer require b2pweb/bdf-form-bundle

然后将其添加到 config/bundles.php 文件中

<?php

return [
    // ...
    Bdf\Form\Bundle\FormBundle::class => ['all' => true],
];

配置

要启用自定义表单和构建器的自动注册,只需启用 autoconfigure 并加载包源

services:
    _defaults:
        autowire: true
        autoconfigure: true

    App\Form\:
        resource: './src/Form/*'

要使用 CSRF,别忘了启用 CSRF 服务

framework:
    csrf_protection:
        enabled: true

使用方法

简单使用容器实例化自定义表单。

注意:容器将自动注入所有依赖

// Declare the form
class MyForm extends \Bdf\Form\Custom\CustomForm
{
    /**
     * @var MyService 
     */
    private $service;
    
    // You can declare dependencies on the constructor 
    public function __construct(MyService $service, ?\Bdf\Form\Aggregate\FormBuilderInterface $builder = null) 
    {
        parent::__construct($builder);
        
        $this->service = $service;
    }

    protected function configure(\Bdf\Form\Aggregate\FormBuilderInterface $builder) : void
    {
        // Configure fields
    }
}

// The controller
class MyController extends AbstractController
{
    public function save(Request $request)
    {
        // Create the form using the container
        $form = $this->container->get(MyForm::class);
        
        // Submit data
        if (!$form->submit($request->request->all())->valid()) {
            throw new FormError($form->error());
        }
        
        $this->service->save($form->value());
        
        return new Reponse('ok');
    }
}

PHP 8 属性

此组件包支持 BDF 表单属性

使用 composer 安装库

composer require b2pweb/bdf-form-attribute

将配置添加到 config/packages/form.yaml 文件中

form:
  attributes:
    compile: true # enable compilation of attributes to native PHP code
    configuratorClassPrefix: 'GeneratedForm\' # Define base namespace (or class prefix) for generated classes
    configuratorClassSuffix: 'Configurator' # Define generated classes suffix
    configuratorBasePath: '%kernel.build_dir%/form' # Define the save path

要禁用开发过程中的代码生成,设置配置 config/packages/dev/form.yaml

form:
  attributes:
    compile: false # disable compilation to build dynamically forms

配置完成后,您可以直接声明表单,例如 示例