简体   繁体   中英

Symfony subfom can't acces unmapped data

I'm trying to make a form with possible subforms ( which can be added/removed on click of add/remove ), it works, but I can't get the unmapped variable from the form

MainForm

    $builder
    ...
        ->add('courses', 'collection', array(
            'type' => new CoursesType(),
            'allow_add'    => true,
            'allow_delete'    => true,
            'prototype' => true,
        ))
    ...   

CoursesType form

    $builder
    ...
        ->add('map', 'file', array(
            'attr' => array(
                'maxsize'     =>'4M',
                'accept'      =>'image/*'
            ),
            'required' => false,
            'data_class' => null,
            'mapped' => false,
        ));
    ...    

The CoursesType form is mapped to an entity so I get the other form data, while I can't access the "map" field

Tried to dump the form, the data I get from "courses" , can't find "map" anywhere

EDIT 1: if I get rid of the mapping of courses to the entity "new CoursesType()" and add a mapping false, I get the data as I want(but in array), but this way I have to manually check all the data and add to a entity, can this be avoided ?

It is possible to string together $form->get() statements before the ->getData()

$courses = $form->get('courses')
foreach ($courses as $course) {
    $map = $course->get('map')->getData();
}

I don't know how this will work with adding and removing items from the collection but works ok for fixed collections

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