load postal codes dynamically

This commit is contained in:
2018-07-05 13:02:21 +02:00
parent 86a814cfb5
commit 26a4d80ce6
16 changed files with 368 additions and 10 deletions

View File

@@ -21,10 +21,14 @@ namespace Chill\MainBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\MainBundle\Entity\PostalCode;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Chill\MainBundle\Form\ChoiceLoader\PostalCodeChoiceLoader;
use Symfony\Component\Translation\TranslatorInterface;
/**
* A form to pick between PostalCode
@@ -39,10 +43,35 @@ class PostalCodeType extends AbstractType
* @var TranslatableStringHelper
*/
protected $translatableStringHelper;
/**
*
* @var UrlGeneratorInterface
*/
protected $urlGenerator;
/**
*
* @var PostalCodeChoiceLoader
*/
protected $choiceLoader;
/**
*
* @var TranslatorInterface
*/
protected $translator;
public function __construct(TranslatableStringHelper $helper)
{
public function __construct(
TranslatableStringHelper $helper,
UrlGeneratorInterface $urlGenerator,
PostalCodeChoiceLoader $choiceLoader,
TranslatorInterface $translator
) {
$this->translatableStringHelper = $helper;
$this->urlGenerator = $urlGenerator;
$this->choiceLoader = $choiceLoader;
$this->translator = $translator;
}
@@ -55,11 +84,26 @@ class PostalCodeType extends AbstractType
{
// create a local copy for usage in Closure
$helper = $this->translatableStringHelper;
$resolver->setDefault('class', PostalCode::class)
$resolver
->setDefault('class', PostalCode::class)
->setDefault('choice_label', function(PostalCode $code) use ($helper) {
return $code->getCode().' '.$code->getName().' ['.
$helper->localize($code->getCountry()->getName()).']';
}
);
})
->setDefault('choice_loader', $this->choiceLoader)
->setDefault('placeholder', 'Select a postal code')
;
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['attr']['data-postal-code'] = 'data-postal-code';
$view->vars['attr']['data-search-url'] = $this->urlGenerator
->generate('chill_main_postal_code_search');
$view->vars['attr']['data-placeholder'] = $this->translator->trans($options['placeholder']);
$view->vars['attr']['data-no-results-label'] = $this->translator->trans('select2.no_results');
$view->vars['attr']['data-error-load-label'] = $this->translator->trans('select2.error_loading');
$view->vars['attr']['data-searching-label'] = $this->translator->trans('select2.searching');
}
}