Output of the "Add a custom conditional validation on a Symfony form" %tag% snippet

Environment:

  • Strangebuzz Snippet VM: 1.8.3
  • Symfony: 5.0.4

Code execution output:

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

 Back to the code  ≪ showUnitTestButtonLabel ≫  More on Stackoverflow   Read the doc  Random snippet

<?php declare(strict_types=1);

namespace App\Tests\Controller\Snippets;

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

/**
 * @covers Snippet20Trait
 */
final class Snippet20Test extends KernelTestCase
{
    /**
     * @var FormFactoryInterface
     */
    private $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,
        ]);
        $this->assertSame($form->isValid(), $isValid);
    }
}