ansien/rapid-form-bundle

一个帮助你快速为你的 Symfony 应用程序创建表单的套餐

安装: 315

依赖者: 0

建议者: 0

安全: 0

星标: 33

关注者: 4

分支: 2

开放问题: 1

类型:symfony-bundle

1.1.7 2024-03-14 18:22 UTC

This package is auto-updated.

Last update: 2024-09-14 19:25:55 UTC


README

Latest Version on Packagist Total Downloads GitHub

Example

该套餐的目标是使用 PHP 8 属性 在你的 DTO 上构建 Symfony 表单。

问题

在 Symfony 中制作表单相对简单。但是一旦你开始使用 DTO,你将不得不维护两个类:你的 DTO 和你的 Symfony 表单类型。这并不理想,因为它会产生不必要的劳动、维护,并且很容易导致错误。

解决方案

此套餐将显著加快你在 Symfony 应用程序中创建表单的速度。通过提供的 PHP 8 属性,你可以通过装饰你的 DTO 快速构建表单,不再需要维护两个不同的类。

安装

您可以通过 Composer 安装此包

composer require ansien/rapid-form-bundle

用法

表单

<?php

declare(strict_types=1);

namespace App\Form;

use Ansien\RapidFormBundle\Attribute\Form;
use Ansien\RapidFormBundle\Attribute\FormField;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints as Assert;

#[Form]
class ExampleForm
{
    #[FormField(TextType::class, [
        'required' => true,
    ])]
    #[Assert\NotBlank]
    public ?string $name = null;

    #[FormField(TextType::class)]
    public ?string $description = null;
}

控制器

<?php

declare(strict_types=1);

namespace App\Controller;

use Ansien\RapidFormBundle\Form\RapidFormBuilderInterface;
use App\Form\ExampleForm;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class ExampleController extends AbstractController
{
    public function __construct(private RapidFormBuilderInterface $formBuilder) 
    {
    }

    #[Route('/example', methods: ['GET', 'POST'])]
    public function __invoke(Request $request): Response
    {
        $data = new ExampleForm();
        $form = $this->formBuilder->create($data)->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            // Do something with $data
        }
        
        return $this->render('example.html.twig', [
            'form' => $form->createView(),
        ]);
    }
}

有关更多示例,请参阅 ./examples

变更日志

请参阅 变更日志 了解最近更改的详细信息。

贡献

请参阅 贡献指南 了解详细信息。

支持者

Stargazers repo roster for @ansien/RapidFormBundle

鸣谢

许可证

MIT 许可证(MIT)。请参阅 许可证文件 了解更多信息。