Closed Thibibi closed 1 year ago
About my post above:
I encounter the same Problem. This commit introduces the error on the exception: https://github.com/a2lix/AutoFormBundle/commit/d1299090c547f429c642b543de717b34f20ba8e3
I am facing the same issue.
I am trying to use a file field (vichuploader) in a translation and I cannot make it work
Update in order to explain my issue:
In order to perform some crud operations I am using easyadmin.
I created a new easyadmin field:
<?php
declare(strict_types=1);
namespace App\Field;
use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
final class TranslationField implements FieldInterface
{
use FieldTrait;
public static function new(string $propertyName, ?string $label = NULL, array $fieldsConfig = []): self
{
return (new self())
->setProperty($propertyName)
->setLabel($label)
->setFormType(TranslationsType::class)
->setFormTypeOptions([
'default_locale' => 'de',
'excluded_fields' => [
'createdAt', 'updatedAt'
],
'fields' => $fieldsConfig,
]);
}
}
In my crud controller I am using something like:
$translate = TranslationField::new('translations', 'BLBLA', [
'title' => [
'row_attr' => ['class' => 'full-width'],
'label' => 'Title',
'label_attr' => ['style' => 'font-weight:bold;padding-bottom:0.5rem;'],
'required' => TRUE,
'help' => 'Aussagekräftiger Titel des Assets',
],
'localResourceFile' => [
'row_attr' => ['class' => 'full-width'],
'label' => 'Datei Upload',
'label_attr' => ['style' => 'font-weight:bold;padding-bottom:0.5rem;'],
'help' => 'Optional: Wahlweise kann eine Datei hinterlegt werden',
'field_type' => 'Vich\UploaderBundle\Form\Type\VichFileType',
'required' => FALSE,
'allow_delete' => TRUE,
'delete_label' => 'Löschen?',
],
]);
I do get the error from above if localResourceFile
is used. If I localResourceFile
to mapped=FALSE
the form etc. looks good but no data will be submitted...
Update 2:
If I uncomment the line with the $unknowsFields
in the file vendor\a2lix\auto-form-bundle\src\Form\Manipulator\DoctrineORMManipulator.php
my example from above works just fine...
Update 2:
If I uncomment the line with the
$unknowsFields
in the filevendor\a2lix\auto-form-bundle\src\Form\Manipulator\DoctrineORMManipulator.php
my example from above works just fine...
I confirm the solution of the problem
Update 2:
If I uncomment the line with the
$unknowsFields
in the filevendor\a2lix\auto-form-bundle\src\Form\Manipulator\DoctrineORMManipulator.php
my example from above works just fine...
Work for me to when adding translations in easyAdmin
The error disappears when the field has mapped set to false. However this prevents Vich of uploading the file and setting the filename.
To get this to work on my end I had to override some parts of the bundle.
First I extend the FormType to add valid_extra_fields
<?php
namespace App\Form\Type;
use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TranslationsTypeExtension extends AbstractTypeExtension
{
public static function getExtendedTypes(): iterable
{
return [TranslationsType::class];
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'excluded_fields' => ['id', 'locale', 'updatedAt', 'createdAt'],
'valid_extra_fields' => [],
]);
}
}
Then I add the values of my extra VichUploader fields in the TranslationField
<?php
declare(strict_types=1);
namespace App\Form\Field;
use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
final class TranslationField implements FieldInterface
{
use FieldTrait;
public static function new(string $propertyName, ?string $label = null, array $fieldsConfig = []): self
{
return (new self())
->setProperty($propertyName)
->setLabel($label)
->setFormType(TranslationsType::class)
->setFormTypeOptions([
'default_locale' => 'nl',
'fields' => $fieldsConfig,
'excluded_fields' => ['createdAt', 'updatedAt', 'document', 'id', 'locale'],
'valid_extra_fields' => ['documentFile' => []]
]);
}
}
I then replace the DoctrineORMManipulator
to take the new option into account.
<?php
namespace App\Form\Manipulator;
use A2lix\AutoFormBundle\Form\Manipulator\FormManipulatorInterface;
use A2lix\AutoFormBundle\ObjectInfo\DoctrineORMInfo;
use Symfony\Component\Form\FormInterface;
class DoctrineORMManipulator implements FormManipulatorInterface
{
/** @var DoctrineORMInfo */
private $doctrineORMInfo;
/** @var array */
private $globalExcludedFields;
public function __construct(DoctrineORMInfo $doctrineORMInfo, array $globalExcludedFields = [])
{
$this->doctrineORMInfo = $doctrineORMInfo;
$this->globalExcludedFields = $globalExcludedFields;
}
public function getFieldsConfig(FormInterface $form): array
{
$class = $this->getDataClass($form);
$formOptions = $form->getConfig()->getOptions();
//Get the parent options for the extra fields
$parentOptions = $form->getParent()?->getConfig()->getOptions();
//Get the values for the extra fields if set on this form or the parent
$extraFields = $formOptions['valid_extra_fields'] ?? $parentOptions['valid_extra_fields'] ?? [];
// Filtering to remove excludedFields
$objectFieldsConfig = $this->doctrineORMInfo->getFieldsConfig($class);
//merge the extra fields with the form fields
$validObjectFieldsConfig = $this->filteringValidObjectFields(array_merge($objectFieldsConfig, $extraFields), $formOptions['excluded_fields']);
if (empty($formOptions['fields'])) {
return $validObjectFieldsConfig;
}
$fields = [];
foreach ($formOptions['fields'] as $formFieldName => $formFieldConfig) {
$this->checkFieldIsValid($formFieldName, $formFieldConfig, $validObjectFieldsConfig);
if (null === $formFieldConfig) {
continue;
}
// If display undesired, remove
if (false === ($formFieldConfig['display'] ?? true)) {
continue;
}
// Override with formFieldsConfig priority
$fields[$formFieldName] = $formFieldConfig;
if (isset($validObjectFieldsConfig[$formFieldName])) {
$fields[$formFieldName] += $validObjectFieldsConfig[$formFieldName];
}
}
return $fields + $validObjectFieldsConfig;
}
private function getDataClass(FormInterface $form): string
{
...
}
private function filteringValidObjectFields(array $objectFieldsConfig, array $formExcludedFields): array
{
...
}
private function checkFieldIsValid($formFieldName, $formFieldConfig, $validObjectFieldsConfig): void
{
if (isset($validObjectFieldsConfig[$formFieldName])) {
return;
}
if (false === ($formFieldConfig['mapped'] ?? true)) {
return;
}
//Here set a valid error
throw new \RuntimeException(sprintf("Field '%s' doesn't exist in %s", $formFieldName, $formFieldConfig['field_type']));
}
}
I then register this class as a service :
services:
a2lix_auto_form.manipulator.default:
class: App\Form\Manipulator\DoctrineORMManipulator
arguments:
$doctrineORMInfo: '@a2lix_auto_form.object_info.doctrine_orm_info'
I can now add the field in my EasyAdmin controller, it renders correctly and works even when uploading multiple files.
$translations = TranslationField::new('translations', 'translations', [
'documentFile' => [
'field_type' => VichFileType::class
],
]);
I will have a closer look to see if it is worth making a pull request and adding this to the bundle.
https://github.com/a2lix/AutoFormBundle/releases/tag/0.4.4 should fix the main issue
Thanks @webda2l
@DennisdeBest @webda2l Thank you for your inputs. The udpate "fixes" the original error indeed. But my main issue persists:
No (with 0.4.4) no exception will be thrown but my "override" does not work anymore ;-) hence I had to downgrade to keep vich working...
so a PR with the solution from @DennisdeBest is very much appreciated ;-)
Composer packages
PHP version
Subject
I get the runtime Exception "Warning: Undefined variable $unknowsFields" when I try to add options to my translated fields. In fact I have 2 problems:
My class:
My translation class:
My formtype:
Steps to reproduce
If you remove the part between the two ////// lines, it works. If you let it, an error occurs saying "Warning: Undefined variable $unknowsFields", and as a matter of fact, there is a piece of code in the bundle refering to this non-existing variable:
Expected results