简体   繁体   中英

Symfony2 Form Builder -> access entity property/form value for suffix

Working on a Symfony 2 project form.

I would like to be able to insert the value of one field in the suffix for another. Is there any good way to do this? Would be even nicer if the value could be updated through javascript when the first field is changed, but I'll settle for having the database value.

The code:

public function buildForm(FormBuilderInterface $builder, array $options)
{
   $builder
    ->add('lessons',null, array('label' => _('Lessen')))           
    ->add('contribution', 'money', array('label' => _('Bijdrage'),
            'widget_suffix' => 'per xx lessen'
     ))                              
....
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'MyBundle\Entity\Course'
    ));
}

Where xx in the suffix should be the value of the lessons field .

Any help would be appreciated!

As I haven't found a good way to do this and also would have liked the value to change the suffix, I have decided to use jquery to get the field and set it in the suffix, where xx is now inside a span with id 'lessonNumber'.

var lessonsField = $('#mybundle_coursetype_lessons');

        function setLessonsSuffix(lessonsField) {
            var lessons = lessonsField.val();
            if(lessons) {
            $("#lessonNumber").html(lessons);
            }
        }

        lessonsField.change(setLessonsSuffix(lessonsField));

        $(document).ready(function() {
            setLessonsSuffix(lessonsField);

        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM