我有这张表格,一份附有一个toToy文档的报告
class ReportType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('documentDatas', CollectionType::class, array(
'entry_type' => DocumentType::class,
'allow_add' => true,
'allow_delete' => true,
'label' => false
))
->add('comment', TextType::class, array(
'label' => 'vat',
'required' => false,
))
->add('save', SubmitType::class);
}
}
这是文档类型
class DocumentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('examDocument', VichImageType::class, array(
'label' => 'examDocument',
'data_class' => null,
'attr' => array('class' => 'upload-image'),
))
->add('note', TextType::class, array(
'label' => 'notes',
'required' => false,
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Model\DocumentData',
));
}
}
我使用VichImageType上传的位置。表单验证成功后,所有步骤均正确,文件被上传,文档实体添加到我的数据库中。 但是,当违反了某些验证(在文档或注释上)时,我会收到此奇怪的错误:
“ AppBundle \ Model \ DocumentData”类不可上传。如果您使用 注释来配置VichUploaderBundle,您可能只是忘记了 在实体顶部添加
@Vich\Uploadable
。如果你不使用 注释,请检查配置文件是否在右侧 地点。在这两种情况下,清除缓存也可以解决问题。
当表单无效时,我的操作尝试对视图进行响应时,我会收到此消息:
public function commentAction(Request $request) {
$reportData = new ReportData();
$form = $this->createForm(ReportType::class, $reportData);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
//some logic
}
return $this->render('report/form.html.twig', [
'form' => $form->createView(), //symfony evidence this row in the exception
]);
}
答案 0 :(得分:0)
我决定也在数据模型中添加Vich Uploadable注释映射,如下所示:
/** * @Vich\Uploadable */ class DocumentData {
/**
* @Assert\NotNull()
* @Assert\File(
* maxSize = "5M",
* mimeTypes = {"image/*", "application/pdf"}
* )
* @Vich\UploadableField(mapping="document_file", fileNameProperty="examDocument")
*/
public $examDocument;
/**
* @Assert\Length(
* min = 2,
* max = 30,
* )
*/
public $note;
public function getExamDocument()
{
return $this->examDocument;
}
public function setExamDocument($examDocument)
{
$this->examDocument = $examDocument;
}
public static function create(Document $document)
{
$edm = new static();
$edm->examDocument = $document->getExamDocument();
$edm->note = $document->getNote();
return $edm;
} }