From the following commit 6bffdbc I got a regression because in a specific FileType component I can enable the support for single or multiple upload. But when using a single upload I always post an array that contains metadata of a single element (file). So the field is set as multiple=false, and compound=false but I submit an array that I transform to a file entity.
Now, I got a TransformationFailedException with message: Submitted data was expected to be text or number, array given.
For me it's important to provide at least the id of file (if it exists) and the file name to support the update of file. I tried to set the field as compound but it leads to other issues, as I always receive an empty File entity as data in my listener (on submit).
I don't understand why the the submitted data corresponding to a field (not multiple or compound) should not be an array.
BlueimpFileType:
<?phpnamespacePTC\CoreBundle\Form\Type;
...classBlueimpFileTypeextendsAbstractType
{
publicfunction__construct(
private readonly string$uploadDir,
private readonly EntityManagerInterface$em,
private readonly LoggerInterface$logger
) {
}
/** * {@inheritdoc} */publicfunctionbuildForm(FormBuilderInterface$builder, array$options)
{
$uploadDir = $this->uploadDir;
if ($options['module'] !== null) {
$uploadDir .= '/'.$options['module'];
}
if ($options['userId'] !== null) {
$uploadDir .= '/'.$options['userId'];
}
if (!$options['multiple']) {
$options['max_number_of_files'] = 1;
}
$builder
->addEventSubscriber(newBlueimpFileListener($uploadDir, $options['type'], $this->em, $this->logger))
//->addViewTransformer(new CollectionToArrayTransformer())
->setAttribute('btnlabel', $options['btnlabel'])
->setAttribute('edit_title', $options['edit_title'])
->setAttribute('attr_title', $options['attr_title'])
->setAttribute('multiple', $options['multiple'])
->setAttribute('max_number_of_files', $options['max_number_of_files'])
->setAttribute('module', $options['module'])
->setAttribute('userId', $options['userId'])
->setAttribute('type', $options['type'])
->setAttribute('download_route', $options['download_route'])
->setAttribute('download_route_params', $options['download_route_params']);
if ($options['multiple']) {
$builder->addViewTransformer(newCollectionToArrayTransformer());
}
}
/** * {@inheritdoc} */publicfunctionbuildView(FormView$view, FormInterface$form, array$options)
{
$data = $form->getViewData();
$config = $form->getConfig();
$files = array();
$this->logger->info('buildView');
//$this->logger->info(print_r($data, true));if (false === empty($data)) {
if ($data instanceof EntityFile) {
$this->logger->info('data is an EntityFile');
$files[] = $data;
} elseif (\is_array($data)) {
$this->logger->info('data is a array');
//} elseif ($data instanceof PersistentCollection) {//$this->logger->info('data is a PersistentCollection');foreach($dataas$dataFile) {
if ($dataFile instanceof EntityFile) {
$this->logger->info('EntityFile');
$files[] = $dataFile;
}
}
}
}
$view->vars = \array_replace($view->vars, array(
'files' => $files,
'btnlabel' => $config->getAttribute('btnlabel'),
'edit_title' => $config->getAttribute('edit_title'),
'attr_title' => $config->getAttribute('attr_title'),
'max_number_of_files' => $config->getAttribute('max_number_of_files'),
'module' => $config->getAttribute('module'),
'userId' => $config->getAttribute('userId'),
'download_route' => $config->getAttribute('download_route'),
'download_route_params' => $config->getAttribute('download_route_params'),
));
}
/** * {@inheritdoc} */publicfunctionconfigureOptions(OptionsResolver$resolver)
{
// data_class must be mapped as :// - PTC\CoreBundle\Entity\File if the field is not multiple// - null if the field is not multiple because the collection can be:// - Doctrine\ORM\PersistentCollection: if the entity containing the files exists// - Doctrine\Common\Collections\ArrayCollection: if the entity containing the files doesn't exist$dataClass = function (Options$options) {
return (!$options['multiple'])
? File::class
//: 'Doctrine\ORM\PersistentCollection';
: null;
};
$resolver->setDefaults(array(
'data_class' => $dataClass,
'compound' => false,
'files' => array(),
'btnlabel' => 'Add File',
'edit_title' => false,
'attr_title' => array(),
'multiple' => false,
'max_number_of_files' => 5,
'module' => 'FILE',
'userId' => null,
'type' => 'FILE',
'download_route' => 'get_file',
'download_route_params' => array(),
));
}
}
@xabbuh Not sure if it's the best way to fix the issue but I could resolve it by setting compound => true and adding 'allow_extra_fields' => true and then replacing in BlueimpFileListener::onSubmit
While we can reduce objects to ID in PRE_SUBMIT it will require change to tens if not hundreds of forms.
Is there a better way to accept such an objects in symfony forms ?
For me this is still relevant.
As of now I have implemented really hacky workaround, by forcing ''multiple" to true and using another property to actually specify whether field accepts multiple selection while using custom transformer.
It would be really helpful if there was a way to use transformers before this check is made.
Symfony version(s) affected
6.1.0
Description
Hi,
From the following commit 6bffdbc I got a regression because in a specific FileType component I can enable the support for single or multiple upload. But when using a single upload I always post an array that contains metadata of a single element (file). So the field is set as
multiple=false
, andcompound=false
but I submit an array that I transform to a file entity.Now, I got a
TransformationFailedException
with message: Submitted data was expected to be text or number, array given.For me it's important to provide at least the id of file (if it exists) and the file name to support the update of file. I tried to set the field as compound but it leads to other issues, as I always receive an empty File entity as data in my listener (on submit).
I don't understand why the the submitted data corresponding to a field (not multiple or compound) should not be an array.
BlueimpFileType:
BlueimpFileListener:
view:
How to reproduce
Send data as array (metadata of single filed) on non multiple / compound field.
Possible Solution
Revert the previous commit?
Additional Context
No response
The text was updated successfully, but these errors were encountered: