In my entity I defined a field color with a callback. The colors can only be selected in the COLORS list (const in this class)
/**
* @ORM\Entity(repositoryClass="App\Repository\EventTagRepository")
*/
class EventTag
{
const COLORS = [
"primary"=>"primary",
"secondary"=>"secondary",
"success"=> "success",
"danger"=>"danger",
"warning"=>"warning",
"info"=>"info",
"light"=>"light",
"dark"=>"dark"
];
/**
* @ORM\Column(type="string", length=255)
* @Assert\Choice(callback="getColors")
*/
private $color;
public function getColors()
{
return $this::COLORS;
}
When I'm creating the form in easy-admin, I'd like to access this callback in the choice type options to prevent the user to choose a wrong color.
EventTag:
class: App\Entity\EventTag
list:
actions: ['-delete']
form:
fields:
- { type: 'group', label: 'Content', icon: 'pencil-alt', columns: 8 }
- 'name'
- { property: 'color', type: 'choice', type_options: { expanded: false, multiple: false, choices: 'colors'} }
Unfortunately in the type_options I didn't find a way to access the entity properties, instead of searching for getColors(), IsColors(), hasColors() methods, it only read the string.
Is it already possible and if not are you planning to add this feature ?
Yaml configuration have some limitations, but I think we should have an option to use callbacks somehow.
The only way I found to add choices dynamically is extending the AdminController.
Working example:
public function createCategoryEntityFormBuilder($entity, $view)
{
$formBuilder = parent::createEntityFormBuilder($entity, $view);
$field = $formBuilder->get('type');
$options = $field->getOptions();
$attr = $field->getAttributes();
$options['choices'] = $formBuilder->getData()->getTypeLabels();
$formBuilder->add($field->getName(), ChoiceType::class, $options);
$formBuilder->get($field->getName())
->setAttribute('easyadmin_form_tab', $attr['easyadmin_form_tab'])
->setAttribute('easyadmin_form_group', $attr['easyadmin_form_group']);
return $formBuilder;
}
As $formBuilder->add erases attributes we need to set them again manually. Probably it can be skipped if you are not using Groups/Tabs, otherwise it will throw Exception saying that field was already rendered.
Doesn't solve the general issue but as a workaround for your specific case: you can use
@Assert\Choice(choices=EventTag::COLORS) in the PHP docblock and
choices: !php/const App\Entity\EventTag::COLORS in the YAML config
with those two solution I have a workaround for lots of configuration. thx
I let the issue open because I think it doesn't answer the general issue
I'm sorry you had some troubles with this. As others mentioned, this is one of the most clear drawbacks of using YAML to configure things.
The good news is that in EasyAdmin 3 this is not only possible to do, but really easy 馃槃 because EasyAdmin 3 no longer uses or requires YAML. We've just released the first alpha version of EasyAdmin 3 and we intend to release the stable version soon. So, let's close this one as "fixed". Thanks.
Most helpful comment
with those two solution I have a workaround for lots of configuration. thx
I let the issue open because I think it doesn't answer the general issue