Sortie du snippet Symfony "Ajouter une validation conditionnelle à un formulaire Symfony"

Environement :

  • Strangebuzz Snippet VM : 2.2.1
  • Symfony : 5.1.3

Sortie du code exécuté :

Form is valid! start_date: 2019-03-26
Form is not valid: ERROR: The end date must be greater than the start date.

Form is valid! start_date: 2019-03-28

 Retour au code  ≪ this.showUnitTest ? this.trans.hide_unit_test : this.trans.show_unit_test ≫  Plus sur Stackoverflow   Lire la doc  Snippet aléatoire

<?php declare(strict_types=1);

namespace App\Tests\Controller\Snippets;

use App\Form\EventCreateType;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Form\FormFactoryInterface;

/**
 * @covers Snippet20Trait
 */
final class Snippet20Test extends KernelTestCase
{
    private FormFactoryInterface $formFactory;

    protected function setUp(): void
    {
        self::bootKernel();
        $this->formFactory = self::$container->get('form.factory');
    }

    public function provide(): array
    {
        return [
            ['2019-03-26', null, true],
            ['2019-03-27',  '2019-03-20', false],
            ['2019-03-28', '2019-03-29', true],
        ];
    }

    /**
     * @covers Snippet20Trait::snippet20
     *
     * @dataProvider provide
     */
    public function testSnippet20(string $startDate, ?string $endDate, bool $isValid): void
    {
        $form = $this->formFactory->create(EventCreateType::class);
        $form->submit([
            'start_date' => $startDate,
            'end_date' => $endDate,
        ]);
        self::assertSame($form->isValid(), $isValid);
    }
}