Vichuploader 和 Symfony 验证的问题
Problem with Vichuploader and Symfony Validion
我有这张表格,一份附有 oneToMany 文件的报告
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上传的地方。当表单验证正常时,一切正常,文件被上传,文档实体添加到我的数据库中。
但是当某些验证被违反时(在文档或评论中)我收到这个奇怪的错误:
The class "AppBundle\Model\DocumentData" is not uploadable. If you use
annotations to configure VichUploaderBundle, you probably just forgot
to add @Vich\Uploadable
on top of your entity. If you don't use
annotations, check that the configuration files are in the right
place. In both cases, clearing the cache can also solve the issue.
当我的操作尝试响应视图时收到此消息,当表单无效时:
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
]);
}
我决定在我的数据模型中也添加 Vich 可上传注释映射,如下所示:
/** * @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;
} }
我有这张表格,一份附有 oneToMany 文件的报告
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上传的地方。当表单验证正常时,一切正常,文件被上传,文档实体添加到我的数据库中。 但是当某些验证被违反时(在文档或评论中)我收到这个奇怪的错误:
The class "AppBundle\Model\DocumentData" is not uploadable. If you use annotations to configure VichUploaderBundle, you probably just forgot to add
@Vich\Uploadable
on top of your entity. If you don't use annotations, check that the configuration files are in the right place. In both cases, clearing the cache can also solve the issue.
当我的操作尝试响应视图时收到此消息,当表单无效时:
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
]);
}
我决定在我的数据模型中也添加 Vich 可上传注释映射,如下所示:
/** * @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;
} }