Giter Site home page Giter Site logo

valid-form-event's Introduction

Valid FormEvent Bundle

Scrutinizer Code Quality Code Coverage Build Status Total Downloads License

The Symfony documentation recommends keeping logic out of forms and instead doing everything inside a controller. That's probably good advice most of the time, but exceptions prove the rule.

Because when you have flexible forms, like a page builder with flexible sections, it's very helpful if the individual sections could do things once the entire form is valid. (Like uploading an image)

This bundle provides exactly this functionality.

Installation

composer require braunstetter/valid-form-event

Usage

<?php


namespace App\Form\Paragraph;

use Braunstetter\MediaBundle\Manager\FilesystemManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Image;
use Braunstetter\ValidFormEvent\Form\Event\ValidFormEvent;
use App\Entity\MyCustomPageBlock;

class MyPageBlockType extends AbstractType
{

    private FilesystemManager $filesystemManager;

    public function __construct(FilesystemManager $filesystemManager)
    {
        $this->filesystemManager = $filesystemManager->setFolder('/uploads/images/page_blocks');
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('image', FileType::class, [
                'constraints' => [
                    new Image(['maxSize' => '5M'])
                ]
            ])
            ->add('description')

            // Here is the important part.
            // Inside the 'valid' event you can do whatever you want to.
            ->addEventListener(ValidFormEvent::NAME, function (ValidFormEvent $event) {
                $form = $event->getCurrentForm() ?? $event->getForm();

                if ($image = $form->get('image')->getData()) {
                    $this->filesystemManager->upload($image);
                }
            });

    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => MyCustomPageBlock::class,
        ]);
    }
}

That's pretty self-explanatory.

Everything works exactly the same as with the other Symfony FormEvents. It should also be noted that the $event->getCurrentForm() method is also available. This gives you the current form you are in - but only if that form is a child of a parent form. The parent form can easily be reached with $event->getForm().

This event works with all forms, even if they are nested.

valid-form-event's People

Contributors

michaelbrauner avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.