从 type_options 访问实体 属性
access entity property from type_options
在我的实体中,我定义了一个带有回调的字段颜色。颜色只能在 COLORS 列表中选择(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;
}
当我在 easy-admin 中创建表单时,我想在 choice
类型选项中访问此回调,以防止用户选择错误的颜色。
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'} }
不幸的是,在 type_options
中我没有找到访问实体属性的方法,而是搜索 getColors()
、IsColors()
、hasColors()
方法,它只读取字符串。
是否可以换一种方式?
回调引用实体const:
你可以使用
@Assert\Choice(choices=EventTag::COLORS)
在 PHP 实体中和
choices: App\Entity\EventTag::COLORS
在 YAML 配置中
回调指的是更具体的值:
您需要手动扩展 AdminController
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.
在我的实体中,我定义了一个带有回调的字段颜色。颜色只能在 COLORS 列表中选择(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;
}
当我在 easy-admin 中创建表单时,我想在 choice
类型选项中访问此回调,以防止用户选择错误的颜色。
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'} }
不幸的是,在 type_options
中我没有找到访问实体属性的方法,而是搜索 getColors()
、IsColors()
、hasColors()
方法,它只读取字符串。
是否可以换一种方式?
回调引用实体const:
你可以使用
@Assert\Choice(choices=EventTag::COLORS)
在 PHP 实体中和
choices: App\Entity\EventTag::COLORS
在 YAML 配置中
回调指的是更具体的值:
您需要手动扩展 AdminController
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.