0

I learned Symfony 3 and I want create form class to upload File, so i created ImageType, cutom form type to handle image uploaded in NewsType (form with some description and this field):

class ImageType extends AbstractType
{
    private $path;

    public function __construct($path)
    {
        $this->path = $path;
    }

    public function getParent()
    {
        return FileType::class;
    }

    public function getName()
    {
        return 'image';
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'image_name' => ''
        ));
    }

    /**
     * @param FormView $view
     * @param FormInterface $form
     * @param array $options
     */
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        $view->vars['image_name'] = $options['image_name'];
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
            ->setAttribute('image_name', $options['image_name'])
            ->addModelTransformer(new ImageTransformer($this->path))

    }
}

I use ImageTransformer to transform file name like 124324235342.jpg to instance File class. Form work fine when i created and saved date to database, but how manage entity in edit mode ?

public function editAction(Request $request, News $news)
{
    $path = $this->getParameter('upload_directory') . $news->getImage();
    $image = $news->getImage();
    $form = $this->createForm(NewsType::class, $news, ['image_name' => $image]);
    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid())
    {
        $this->get('app.image_uploader')->uploadNews($news, $image);

        $em = $this->getDoctrine()->getManager();
        $em->persist($news);
        $em->flush();

        return $this->redirectToRoute('admin_news_index');
    }

    return $this->render('admin/news/form.html.twig', [
        'form' => $form->createView(),
        'news' => $news
    ]);
}

I want handle case to use same form to edit database entity. I populated form, but when user not upload image I don't want change this field and live old value. How accomplish this ?

Heroes84
  • 934
  • 1
  • 9
  • 18

1 Answers1

0

The simplest method for your code:

In setter of your News type:

function setImage($image) {
    if(strlen($image)) {
          $this->image = $image;
    }
}

This allow you to do not worry about case of lacking image, when user edits other fields. In this case $this->fileName in News will be not overwritten.

Then in service app.image_uploader you should check if file with given name exist. If not then you should not overwrite this file.

But there are some other poroblems:

1) You should validate extension of file.

2) You should use unique names for your files in your server different than names from users. Yo can use both names, your unique for string files in hard drive, and name form user in user interface, but you should not use name of file form user to store file in your hard drive.

I recommend to read about these problems in docs:

https://symfony.com/doc/current/controller/upload_file.html

Or on stack overflow:

Symfony2 file upload step by step

Daniel
  • 7,684
  • 7
  • 52
  • 76
  • Thanks for reply but how about use empty_data ? Is some tip to modyfy form after set data from entity and and empty_data to field ? – Heroes84 Sep 13 '17 at 08:45