mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
Refactor address vue app and create a PickAddressType
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\MainBundle\Form\Type\DataTransformer;
|
||||
|
||||
use Chill\MainBundle\Repository\AddressRepository;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
|
||||
final class AddressToIdDataTransformer implements DataTransformerInterface
|
||||
{
|
||||
private AddressRepository $addressRepository;
|
||||
|
||||
public function __construct(AddressRepository $addressRepository)
|
||||
{
|
||||
$this->addressRepository = $addressRepository;
|
||||
}
|
||||
|
||||
public function reverseTransform($value)
|
||||
{
|
||||
if (NULL === $value || '' === $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$address = $this->addressRepository->find($value);
|
||||
|
||||
if (NULL === $address) {
|
||||
$failure = new TransformationFailedException(sprintf("Address with id %s does not exists", $value));
|
||||
$failure
|
||||
->setInvalidMessage("The given {{ value }} is not a valid address id", [ '{{ value }}' => $value]);
|
||||
|
||||
throw $failure;
|
||||
}
|
||||
|
||||
return $address;
|
||||
}
|
||||
|
||||
public function transform($value)
|
||||
{
|
||||
if (NULL === $value) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $value->getId();
|
||||
}
|
||||
}
|
52
src/Bundle/ChillMainBundle/Form/Type/PickAddressType.php
Normal file
52
src/Bundle/ChillMainBundle/Form/Type/PickAddressType.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\MainBundle\Form\Type;
|
||||
|
||||
use Chill\MainBundle\Entity\Address;
|
||||
use Chill\MainBundle\Form\Type\DataTransformer\AddressToIdDataTransformer;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
final class PickAddressType extends AbstractType
|
||||
{
|
||||
private AddressToIdDataTransformer $addressToIdDataTransformer;
|
||||
|
||||
public function __construct(AddressToIdDataTransformer $addressToIdDataTransformer)
|
||||
{
|
||||
$this->addressToIdDataTransformer = $addressToIdDataTransformer;
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder->addModelTransformer($this->addressToIdDataTransformer);
|
||||
}
|
||||
|
||||
public function buildView(FormView $view, FormInterface $form, array $options)
|
||||
{
|
||||
$view->vars['uniqid'] = $view->vars['attr']['data-input-address'] =\uniqid('input_address_');
|
||||
$view->vars['attr']['data-use-valid-from'] = $options['useValidFrom'];
|
||||
$view->vars['attr']['data-use-valid-to'] = $options['useValidTo'];
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'class' => Address::class,
|
||||
'useValidFrom' => false,
|
||||
'useValidTo' => false,
|
||||
|
||||
// reset default from hidden type
|
||||
'required' => true,
|
||||
'error_bubbling' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getParent()
|
||||
{
|
||||
return HiddenType::class;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user