Symfony2 form field not updated when validation error occurs

后端 未结 2 423
不知归路
不知归路 2021-01-05 12:56

Here is my form type:

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

        $         


        
2条回答
  •  伪装坚强ぢ
    2021-01-05 13:49

    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

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

    In your controller you have something like this:

    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.

提交回复
热议问题