Hi.
I was wondering: how do I get the file size constraint working, if I have a one-to-many relationship between an entity and uploaded files using the form CollectionType?
User FormType:
->add('UserFiles', CollectionType::class, array(
'entry_type' => UserFilesType::class,
'required' => false,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true,
'label' => false,
'attr' => array(
'class' => 'file-collection',
),
))
UserFiles FormType:
->add('file', VichFileType::class, [
'required' => false,
'allow_delete' => true, // not mandatory, default is true
'download_link' => true, // not mandatory, default is true
])
I have tryed using annotation:
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="user_files", fileNameProperty="filename")
* @Assert\File(
* maxSize = "1024k"
* )
* @var File
*/
private $file;
AND using callback:
/**
* @Assert\Callback(methods={"validate"})
*/
public function validate(ExecutionContextInterface $context)
{
if ($this->file->getClientSize() > 1024) {
$context
->buildViolation('File too big, max 1Mb')
->atPath('fileName')
->addViolation()
;
}
}
Upload, delete and so on all works fine, but I cannot seem to get the constraint working. Do you have any ideas?
Did you add @Assert\Valid on $UserFiles?
Hi Garak!
Wow, thx for the fast answer! That was the problem!
I also noticed that in order to show the errors with {{ form_errors(form) }} you have to set error_bubbling to true on the userfiletype (collection) AND on the filetype (nested in collection). Thanks for your help 馃憤
Most helpful comment
Hi Garak!
Wow, thx for the fast answer! That was the problem!
I also noticed that in order to show the errors with {{ form_errors(form) }} you have to set error_bubbling to true on the userfiletype (collection) AND on the filetype (nested in collection). Thanks for your help 馃憤