就不说表单上传的方式了,在Entity里完成即可。
在sonata中表单上传成功后,编辑或者显示时,你会让你的图片路径显示出来,但是这样很丑陋,如果能够显示图片会友好很多。
只需要很简单的方式即可实现,在下面表单方法中这样操作即可:
1 |
protected function configureFormFields(FormMapper $formMapper) |
完整代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
protected function configureFormFields(FormMapper $formMapper) { $subject = $this->getSubject(); //获取对象 // use $fileFieldOptions so we can add other options to the field $fileFieldOptions = ['label' => '图片', 'required' => false]; if ($subject && $subject->getImagepath()) { //判断图片路径是否存在 //$container = $this->getConfigurationPool()->getContainer(); $fullPath = $subject->getImagepath(); $fileFieldOptions['help'] = '<img src="/' . $fullPath . '" style="width:200px;" />'; } $formMapper ->add('imagefile', 'file', $fileFieldOptions) ; } |
ok了,刷新就可以看到效果