Skip to content

Commit

Permalink
feat: add ButtonTypeExtension to lower default priority
Browse files Browse the repository at this point in the history
  • Loading branch information
siganushka committed Nov 29, 2024
1 parent 1aed3ee commit be42bf8
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
4 changes: 4 additions & 0 deletions config/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Siganushka\GenericBundle\Form\Extension\ButtonTypeExtension;
use Siganushka\GenericBundle\Form\Extension\CollectionTypeExtension;
use Siganushka\GenericBundle\Form\Extension\Html5ValidationTypeExtension;

Expand All @@ -12,6 +13,9 @@
->set('siganushka_generic.form.type_extension.collection', CollectionTypeExtension::class)
->tag('form.type_extension')

->set('siganushka_generic.form.type_extension.button', ButtonTypeExtension::class)
->tag('form.type_extension')

->set('siganushka_generic.form.type_extension.html5_validation', Html5ValidationTypeExtension::class)
->tag('form.type_extension')
;
Expand Down
24 changes: 24 additions & 0 deletions src/Form/Extension/ButtonTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Siganushka\GenericBundle\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ButtonTypeExtension extends AbstractTypeExtension
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('priority', -16);
}

public static function getExtendedTypes(): iterable
{
return [
ButtonType::class,
];
}
}
31 changes: 31 additions & 0 deletions tests/Form/Extension/ButtonTypeExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Siganushka\GenericBundle\Tests\Form\Extension;

use Siganushka\GenericBundle\Form\Extension\ButtonTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Test\TypeTestCase;

class ButtonTypeExtensionTest extends TypeTestCase
{
public function testAll(): void
{
$button = $this->factory->create(ButtonType::class);
$submit = $this->factory->create(SubmitType::class);

$buttonView = $button->createView();
$submitView = $submit->createView();
static::assertSame(-16, $buttonView->vars['priority']);
static::assertSame(-16, $submitView->vars['priority']);
}

protected function getTypeExtensions(): array
{
return [
new ButtonTypeExtension(),
];
}
}
31 changes: 9 additions & 22 deletions tests/Form/Extension/Html5ValidationTypeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,24 @@

namespace Siganushka\GenericBundle\Tests\Form\Extension;

use PHPUnit\Framework\TestCase;
use Siganushka\GenericBundle\Form\Extension\Html5ValidationTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormFactoryBuilder;
use Symfony\Component\Form\Test\TypeTestCase;

class Html5ValidationTypeExtensionTest extends TestCase
class Html5ValidationTypeExtensionTest extends TypeTestCase
{
public function testDefault(): void
public function testAll(): void
{
$formFactoryBuilder = new FormFactoryBuilder();

$form = $formFactoryBuilder->getFormFactory()
->createBuilder(TextType::class)
->getForm()
;
$form = $this->factory->create(TextType::class);

$view = $form->createView();
static::assertTrue($view->vars['required']);
static::assertFalse($view->vars['required']);
}

public function testHtml5Validation(): void
protected function getTypeExtensions(): array
{
$formFactoryBuilder = new FormFactoryBuilder();
$formFactoryBuilder->addTypeExtension(new Html5ValidationTypeExtension());

$form = $formFactoryBuilder->getFormFactory()
->createBuilder(TextType::class)
->getForm()
;

$view = $form->createView();
static::assertFalse($view->vars['required']);
return [
new Html5ValidationTypeExtension(),
];
}
}

0 comments on commit be42bf8

Please sign in to comment.