Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 form field not updated when validation error occurs

Tags:

php

symfony

Here is my form type:

class TestFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder->add('thumbnail', 'hidden', array(
            'label' => 'Thumbnail',
            'label_attr' => array(
                'class' => 'col-xs-2 control-label'
            ),
            'required' => false,
            'error_bubbling' => true,
            'required' => false
        ));

        $builder->add('thumbnail_data', 'file', array(
            'error_bubbling' => true,
            'required' => false
        ));
    }

    public function setDefaultOptions(\Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'X\LibraryBundle\Entity\Test',
            'cascade_validation' => true,
            'error_bubbling' => true,
        ));

    }

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

Here is the entity, important part is the setThumbnailData($file) method, which stores the thumbnail file and sets the thumbnail path via the setThumbnail(string) method.

<?php

namespace X\LibraryBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Test
 *
 * @ORM\Table(name="test")
 * @ORM\Entity(repositoryClass="X\LibraryBundle\Repository\TestRepository")
 */
class Test
{
    /**
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    protected $thumbnail;

    /**
     * Set thumbnail
     *
     * @param string $thumbnail
     * @return Test
     */
    public function setThumbnail($thumbnail)
    {
        $this->thumbnail = $thumbnail;

        return $this;
    }

    /**
     * Get thumbnail
     *
     * @return string 
     */
    public function getThumbnail()
    {
        return $this->thumbnail;
    }

    /**
     * This will save file to disk
     * @param $file
     */
    public function setThumbnailData($file) {
        if($file !== null && $file !== false)
        {
            $fileName = $file->getClientOriginalName();
            $baseDir = __DIR__ . '/../../../../../../../web/uploads/promotional_material/';
            $dir =  sha1(microtime());
            while (is_dir($baseDir . $dir)) {
                $dir = sha1(microtime());
            }
            mkdir($baseDir . $dir);
            $this->setThumbnail('/uploads/promotional_material/' . $dir . '/' . $fileName);
            $file->move($baseDir . $dir, $fileName);
        }
    }

    public function getThumbnailData() {
        return '';
    }
}

Now the issue is, if the form runs into a validation error, the following twig lines produce different output, the correct value outputted from the second line, the other produces the original thumbnail path. So if I output the thumbnail input using {{ form_widget(form.thumbnail) }}, I get the original thumbnail path, not the one that should have changed via the setThumbnailData() method.

{{ dump(form.thumbnail.vars.data) }}
{{ dump(form.vars.data.thumbnail) }}

Is the issue caused by setting the thumbnail using the setThumbnailData() method? Not sure how to fix this other than hard coding the input in twig like so:

<input type="hidden" name="test[thumbnail]" value="{{ form.vars.value.thumbnail }}"/>
like image 982
Trololololol Avatar asked Feb 10 '15 18:02

Trololololol


1 Answers

I cannot exactly solve your problem, because in my opinion you're doing it the wrong way. A data class is actually only responsibly for keeping your data, so your method set/getThumbnailData should look like this

<?php
// ...
private $thumbnailData;

public function setThumbnailData(UploadedFile $thumbnailData) {
    $this->thumbnailData = $thumbnailData;
}

public function getThumbnailData() {
    return $this->thumbnailData;
}

In your controller you have something like this:

<?php
// ...
public function formAction() {

    $form = // ... 
    $form->handleRequest($request);
    if($form->isValid()) {
        $test = $form->getData();
        /* @var $test Test */
        $thumbnailUploader = $this->get('thumbnail_uploader');
        /* @var $thumbnailUploader ThumbnailUploadService */

        $file = $test->getThumbnailData();
        $filename = $file->getClientOriginalName();
        $thumbnailUploader->upload($filename, $file);

        // ...
    }
}

I recommend to move all logic which has nothing to do with forms or requests into services. Your service in this case can be more generic handle any symfony files with a given filename.

<?php
class ThumbnailUploadService {
    /**
     * @param $file Symfony\Component\HttpFoundation\File\File
     *
     */
    public function upload($filename, File $file) {
        // ...
    }
}
like image 107
Aitch Avatar answered Nov 12 '22 14:11

Aitch