generated from headsnet/symfony-bundle-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TextTypeDefaultStringExtension with associated configuration setting
- Loading branch information
Showing
7 changed files
with
130 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Headsnet\SymfonyToolsBundle\Form\Extension; | ||
|
||
use Symfony\Component\Form\AbstractTypeExtension; | ||
use Symfony\Component\Form\Extension\Core\Type\EmailType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextareaType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\Form\Extension\Core\Type\UrlType; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* Override the default behaviour of TextType, which returns null when the field is empty. | ||
* | ||
* To keep our entities free of nulls, we want to return an empty string instead. This | ||
* extension simply sets the "empty_data" option to "" for all TextType fields. | ||
*/ | ||
final class TextTypeDefaultStringExtension extends AbstractTypeExtension | ||
{ | ||
#[\Override] | ||
public static function getExtendedTypes(): iterable | ||
{ | ||
return [TextType::class, EmailType::class, UrlType::class, TextareaType::class]; | ||
} | ||
|
||
#[\Override] | ||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
parent::configureOptions($resolver); | ||
|
||
$resolver->setDefault('empty_data', ''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
headsnet_symfony_tools: | ||
root_namespace: App | ||
rate_limiting: | ||
use_headers: true | ||
root_namespace: App | ||
forms: | ||
default_empty_string: | ||
enabled: true | ||
rate_limiting: | ||
use_headers: true | ||
|
43 changes: 43 additions & 0 deletions
43
tests/Form/Extension/TextTypeDefaultStringExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Headsnet\SymfonyToolsBundle\Tests\Form\Extension; | ||
|
||
use Headsnet\SymfonyToolsBundle\Form\Extension\TextTypeDefaultStringExtension; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Form\Extension\Core\Type\EmailType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextareaType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\Form\Extension\Core\Type\UrlType; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
#[CoversClass(TextTypeDefaultStringExtension::class)] | ||
final class TextTypeDefaultStringExtensionTest extends TestCase | ||
{ | ||
#[Test] | ||
public function applies_to_all_textual_form_types(): void | ||
{ | ||
$result = TextTypeDefaultStringExtension::getExtendedTypes(); | ||
|
||
$this->assertEquals([TextType::class, EmailType::class, UrlType::class, TextareaType::class], $result); | ||
} | ||
|
||
#[Test] | ||
public function validation_is_disabled(): void | ||
{ | ||
$optionsResolver = new OptionsResolver(); | ||
$sut = new TextTypeDefaultStringExtension(); | ||
|
||
$sut->configureOptions($optionsResolver); | ||
|
||
$this->assertTrue($optionsResolver->isDefined('empty_data')); | ||
$this->assertEquals( | ||
[ | ||
'empty_data' => '', | ||
], | ||
$optionsResolver->resolve() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters