У меня есть эта форма, отчет с прикрепленным документом 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\Uploadableon 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 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;
} }