I am having a hard time making a form to edit a already existing subset of the Entity “Setting”.
The idea is to render only a determined collection of “Settings” in one template, each with his custom input type, at least on template level changing the widget.
The Setting Entity looks like this:
AppEntitySetting.php
<?php
namespace AppEntity;
use AppRepositorySettingRepository;
use DoctrineDBALTypesTypes;
use DoctrineORMMapping as ORM;
use SymfonyComponentUidUlid;
#[ORMEntity(repositoryClass: SettingRepository::class)]
class Setting
{
#[ORMId]
#[ORMColumn(type: 'ulid', unique: true)]
#[ORMGeneratedValue(strategy: 'CUSTOM')]
#[ORMCustomIdGenerator(class: 'doctrine.ulid_generator')]
private ?Ulid $id = null;
#[ORMColumn(length: 255)]
private ?string $name = null;
#[ORMColumn(length: 255)]
private ?string $slug = null;
#[ORMColumn(type: Types::TEXT, nullable: true)]
private ?string $value = null;
public function getId(): ?Ulid
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getValue(): ?string
{
return $this->value;
}
public function setValue(?string $value): self
{
$this->value = $value;
return $this;
}
}
The Form Type:
AppFormSettingGeneralType.php
<?php
namespace AppForm;
use AppEntitySetting;
use AppFormSettingType;
use DoctrineORMEntityRepository;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeCollectionType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
class SettingGeneralType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name')
->add('value')
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Setting::class,
]);
}
}
For the “Set” that needs to be in the form i could give following data:
id | name | value | slug |
---|---|---|---|
App name | AppName | app_name | |
App logo | NULL | app_logo | |
App description | NULL | app_description | |
Maintenance mode | false | app_maintenance | |
Default date and time format | d.m.Y H:i | app_datetime_format | |
PHP bin path | pathToPhp.bin | app_php_bin_path | |
Default color mode | auto | app_theme_default |
The value types vary between string,choice (Dropdowns),boolean (checkboxes).
I would have no problem in formatting the different form widgets manually in the template to fit the required value, but i can’t find any solution to how to render only a little collection each with his own “input” from this entity.
My only idea was using the “CollectionType” since it can render a set of simple forms i can render in the template and handle on submit, but i could not get it to filter entites that i wanted be rendered in those forms since i dont want every setting to be rendered…
This is my first question, so no hate if i missed something please 🙂