Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"doctrine/doctrine-bundle": "^2.13",
"doctrine/orm": "^3.3",
"symfony/doctrine-messenger": "^7.3",
"symfony/intl": "^7.3",
"symfony/ux-toggle-password": "^2.22"
},
"require-dev": {
Expand Down
5 changes: 4 additions & 1 deletion config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

use SumoCoders\FrameworkCoreBundle\Command\SecretsGetCommand;
use SumoCoders\FrameworkCoreBundle\Command\TranslateCommand;
use SumoCoders\FrameworkCoreBundle\DoctrineListener\DoctrineAuditListener;
use SumoCoders\FrameworkCoreBundle\EventListener\TitleListener;
use SumoCoders\FrameworkCoreBundle\Form\Type\BelgiumPostCodeType;
use SumoCoders\FrameworkCoreBundle\Logger\AuditLogger;
use SumoCoders\FrameworkCoreBundle\Service\PageTitle;
use SumoCoders\FrameworkCoreBundle\Twig\ContentExtension;
Expand Down Expand Up @@ -84,6 +84,9 @@
->set('framework.file_type', FileType::class)
->tag('form.type', ['alias' => 'sumoFile'])

->set('framework.file_type', BelgiumPostCodeType::class)
->tag('form.type', ['alias' => 'sumoBelgiumPostCode'])

/*
* Secure headers
*/
Expand Down
15 changes: 15 additions & 0 deletions docs/development/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,18 @@ Where `Username` is a translation. So if you want to translate the label, you ca
```yaml
Username: 'Enter your username'
```

## Belgium postcode

Form type that allows only valid belgian postcodes to be selected.

Use in your own form:
```php
$builder->add('postcode', BelgiumPostCodeType::class);
```

Add a property in the DTO:
```php
#[Assert\NotBlank]
public ?BelgiumPostCode $postcode = null;
```
72 changes: 72 additions & 0 deletions src/Form/Type/BelgiumPostCodeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace SumoCoders\FrameworkCoreBundle\Form\Type;

use SumoCoders\FrameworkCoreBundle\Intl\BelgiumPostCodes;
use SumoCoders\FrameworkCoreBundle\ValueObject\BelgiumPostCode;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\ChoiceList\ChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Intl\Intl;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class BelgiumPostCodeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addModelTransformer(
new CallbackTransformer(
function (?BelgiumPostCode $object): ?string {
return $object ? ($object->postcode . '|' . $object->municipality) : null;
},
function (?string $postcodeKey): ?BelgiumPostCode {
if ($postcodeKey === null || $postcodeKey === '') {
return null;
}

[$postcode, $municipality] = explode('|', $postcodeKey, 2);
return new BelgiumPostCode(
$postcode,
$municipality
);
}
)
);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'choice_loader' => function (Options $options) {
if (!class_exists(Intl::class)) {
throw new LogicException(sprintf('The "symfony/intl" component is required to use "%s". Try running "composer require symfony/intl".', static::class)); // phpcs:ignore Generic.Files.LineLength
}

return ChoiceList::loader(
$this,
new IntlCallbackChoiceLoader(static fn() => array_flip(BelgiumPostCodes::getNames()))
);
},
'choice_translation_domain' => false,
'invalid_message' => 'Please select a valid postcode.',
'placeholder' => '',
'autocomplete' => true,
'label' => 'Postcode and municipality',
]);
}

public function getParent(): ?string
{
return ChoiceType::class;
}

public function getBlockPrefix(): string
{
return 'sumoBelgiumPostCode';
}
}
21 changes: 21 additions & 0 deletions src/Intl/BelgiumPostCodes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace SumoCoders\FrameworkCoreBundle\Intl;

use Symfony\Component\Intl\ResourceBundle;

class BelgiumPostCodes extends ResourceBundle
{
/**
* @return array<string, string>
*/
public static function getNames(): array
{
return self::readEntry(['Names'], 'nl', false);
}

protected static function getPath(): string
{
return __DIR__ . '/Resources/data/belgiumpostcodes';
}
}
Loading