M
M
Michael2014-07-11 13:23:57
Ruby on Rails
Michael, 2014-07-11 13:23:57

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
                ]);
});

Everything works fine.
But I can not figure out how to make automatic substitution of this value. For example, if instead of the initial_value option (with the value of the object) pass the option 'set_initial_value' => true

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
N. Bekseitov, 2016-03-25
@Mirkom63

How do thumbnails work in paperclip gem?

M
maksmaers, 2014-07-11
@maksmaers

$max_count = 10;

$form->add(
'max_count', 'integer', array(
    'label' => 'deal.coupon_count.name',
    'data' => $max_count
))

data - the value that will be substituted into the form

S
Sergey, 2014-07-11
Protko @Fesor

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 question

Ask a Question

731 491 924 answers to any question