N
N
Nikita Roschenko2016-01-12 20:16:37
symfony
Nikita Roschenko, 2016-01-12 20:16:37

How to make images checkbox in symfony2 forms?

Good afternoon, in my project on Symfony2, there is an Entity Photo
And there is a form for adding photos to the album from the user's existing ones. I would like that in the form the possible photos to be added are displayed not as dropdown, but as regular photos . And from the preview it would already be possible to choose what to add to the album. Can you suggest how this can be implemented? Google, unfortunately, did not give anything intelligible, I am writing on Symfony for the first time, so forgive me if I ask a stupid question) CreateAlbumType code:<img src=""/>

class CreateAlbumType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('date', 'datetime', [
                'widget' => 'single_text'
            ])
            ->add('text', 'textarea', [
                'attr' => [
                    'rows' => 5,
                    'style' => 'min-width: 300px;',
                ],
            ])
            ->add('photos', 'entity', array(
                'class' => 'Flugo\PhotosBundle\Entity\Photo',
                'label' => 'Photos',
                'multiple' => true,
            ))
        ;
    }
    
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Flugo\AlbumBundle\Entity\CreateAlbum'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'flugo_albumbundle_post';
    }
}

Form display:
{% block body -%}
    <h1>Album creation</h1>

    {{ form(form) }}
{% endblock %}

And the photos are displayed in selecte:
xAeNK4RsYggJKr.jpg
And I would like something like this:
a2Xa9wJsyDD0nA.jpg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita Roschenko, 2016-01-12
@Avillions

I found a solution to the problem myself. Everything turned out to be simple, we need the EntityType to be displayed as a CheckboxList . To do this, sets the multiple and expanded parameters to true . Next, in the checkbox label, display the html img tag with the scr attribute specified. We hide the checkbox itself, and we hang event handlers on the pictures using js.
Here's what happened:
Here's the code (I moved it from the form to the controller to limit the choices to the user's photos):

$form->add('photos', 'entity', array(
            'class'    => 'Flugo\PhotosBundle\Entity\Photo',
            'multiple' => true,
            'expanded' => true,
            'choices'  => $user->getPhotos(),
            'attr'     => [
                'class' => 'photos-checkbox '
            ],
            'choice_label' => function($choice, $currentChoiceKey) {
                return sprintf('<img src="%s">',  $choice->getPhoto130());
            },
            'choice_attr' => ['class' => 'hidden'],
        ));

B
BoShurik, 2016-01-12
@BoShurik

StackOverflow
You need to override the widget for the fieldphotos

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question