Answer the question
In order to leave comments, you need to log in
How to display the original value in a Symfony 2 form?
There is a large form of editing with a complex hierarchy.
It is necessary to write field values from the database under input.
But so that after submitting the form, if validation fails, this value does not change.
The input widget has been fixed.
In the variant
$builder->addEventListener(
FormEvents::POST_SET_DATA,
function (FormEvent $event){
$form = $event->getForm();
$form->add(
'max_count',
'integer',
[
'label' => 'deal.coupon_count.name',
'initial_value' =>$form->get('max_count')->getData(),
'attr' => [
'placeholder' => 'count.max',
'class' => 'form-control small'
],
'required' => false
]);
});
Answer the question
In order to leave comments, you need to log in
$max_count = 10;
$form->add(
'max_count', 'integer', array(
'label' => 'deal.coupon_count.name',
'data' => $max_count
))
Create extension , profit. You can take this article as a basis . The essence will be the same - you need to extend the types you need (in theory, in general, field, from which everything is inherited), and check settings in it, manipulate data, etc. Also, instead of event listeners, you can use data transformers, in theory it will be more reliable because you will always have these models ... Although here you need to think and try.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question