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 essence, the important part is the setThumbnailData ($ file) method, which stores the thumbnail file and sets the thumbnail path using the setThumbnail (string) method.
<?php namespace X\LibraryBundle\Entity; use Doctrine\ORM\Mapping as ORM; class Test { protected $id; protected $thumbnail; public function setThumbnail($thumbnail) { $this->thumbnail = $thumbnail; return $this; } public function getThumbnail() { return $this->thumbnail; } 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 problem is that if the form starts in a validation error, the following lines of the line produce different output, the correct value displayed from the second line, the other creates the original sketch path. Therefore, if I display a sketch using {{form_widget (form.thumbnail)}}, I get the original sketch path, and not the one that should have been modified using the setThumbnailData () method.
{{ dump(form.thumbnail.vars.data) }} {{ dump(form.vars.data.thumbnail) }}
Is the problem related to setting the sketch using the setThumbnailData () method? You do not know how to fix this, except for hard-coding input in a branch, for example:
<input type="hidden" name="test[thumbnail]" value="{{ form.vars.value.thumbnail }}"/>