A
A
Alexander Pantyukhov2019-10-18 14:44:19
symfony
Alexander Pantyukhov, 2019-10-18 14:44:19

Why do some values ​​go to null after $form->handleRequest($request)?

Hello. I'm recently working with Symfony 4 for this reason and there are a lot of things I don't understand. Faced such a situation that when creating a new record (I work not through Entity but through DTO), some parameters were set to null.

dump($request->get('hotel_form_builder')):
"countryId" => "189"
  "cityId" => "425754"
dump($form->getData()):
  +countryId: null
  +cityId: null

To be more specific, these are 2 parameters (`countryId` and `cityId`). For these two fields, their own type Select2Type was "written":
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;

class Select2Type extends AbstractType {
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        $attr = $view->vars['attr'];
        $class = isset($attr['class']) ? $attr['class'].' ' : '';
        $class .= 'select2';
        $attr['class'] = $class;
        $attr['data-autocomplete-url'] = $attr['data-autocomplete-url'];
        $view->vars['attr'] = $attr;
    }
    public function getParent() {
        return ChoiceType::class;
    }
    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults([
            'attr' => [
                'class' => 'form-control select2',
                'data-autocomplete-url' => ''
            ]
        ]);
    }
}

I pass the data via url in the form {id: id, text: 'text'}. There is a high probability that I incorrectly described or wrote something.
This is part of the code from buildForm:
...
            ->add('countryId', Select2Type::class, [
                'label'              => 'hotel.field.countryId',
                'translation_domain' => 'hotel_inventory',
                'required'           => true,
                'attr'               => [
                    'data-autocomplete-url' => $router->generate('hotel_country'),
                    'class'                 => 'form-control select2SearchCountry',
                ]
            ])
            ->add('cityId', Select2Type::class, [
                'label'              => 'hotel.field.cityId',
                'translation_domain' => 'hotel_inventory',
                'required'           => true,
                'attr'               => [
                    'data-autocomplete-url' => $router->generate('hotel_city'),
                    'class'                 => 'form-control select2SearchCity',
                ]
            ])
...

I would be grateful for help. If you have any ideas or questions please ask. Suddenly together we will understand where the problem is.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
postgresdev, 2019-10-23
@postgresdev

Specify default values ​​in the entity, for example:

/**
     * @var bool
     *
     * @ORM\Column(name="active", type="boolean")
     */
    private $active = true;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question