Answer the question
In order to leave comments, you need to log in
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
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' => ''
]
]);
}
}
...
->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',
]
])
...
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question