简体   繁体   中英

sonata_type_collection clear input file when adding a row

in advance, sorry for my poor english.

I have a problem with the "sonata_type_collection" form type.

I got two entity, the first, "proposition" has a one-to-many relationship with "Image". "Image" has a many-to-one relationship with "proposition". Everything seems to work well, the ImageAdmin is nested in PropositionAdmin.

But when i add a row in the PropositionAdmin, without persisting object, it clear the input="file" field. I read that its the correct behavior since Sonata reload the form when adding a row. So i was wondering if there was a way to avoid that behavior.

Thanks in advance.

Here is my code :

protected function configureFormFields(FormMapper $formMapper)
{

    $formMapper
        ->add('title')
        ->add('axe')
        ->add('username')
        ->add('password','repeated', array('type' => 'text','options' => array('translation_domain' => 'FOSUserBundle'),
            'first_options' => array('label' => 'form.password'),
            'second_options' => array('label' => 'form.password_confirmation'),
            'invalid_message' => 'fos_user.password.mismatch'))
        ->add('imgs', 'sonata_type_collection',  array(
            'by_reference' => false
        ), array(
            'edit' => 'inline',
            'allow_delete' => true
        )) 
    ;
}

I endend up by using a Symfony2 form.

I generated the formtype with Command here

Then, in the parent entity's bundle:

>add('imgs', 'collection',  array(
            'label' => 'Créations',
            'by_reference' => false ,
            'type' => new \propalBundle\Form\ImageType(),
            'allow_delete' => true,
            'allow_add' => true
            ), array(
            'edit' => 'standard',
            'inline' => 'table',
        )) 

It can require some tweaks but seems to work.

When the proposition has relation oneToMany with image you should add multiple attribute in the configuration of imgs filed in PropositionAdmin class

->add('imgs', 'sonata_type_collection',  array(
'by_reference' => false
'multiple' => true
), array(
'edit' => 'inline',
'allow_delete' => true
));

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