跳到内容

EasyAdmin Choice Field

编辑此页

此字段显示属性的内容,该属性的值只能是给定集合中包含的值之一。

表单页面(编辑和新建)中,此字段以多种不同的方式呈现,具体取决于其配置(请参阅此页面稍后的详细信息)。

基本信息

  • PHP 类: EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField
  • Doctrine DBAL 类型 用于存储此值:string 或您用于可能值的任何其他类型(例如 integer
  • Symfony 表单类型 用于呈现字段: ChoiceType
  • 呈现为:

    1
    2
    3
    <!-- this field is rendered either as a group of checkboxes, a set of
         radiobuttons or an advanced widget created with JavaScript.
         It all depends on the field configuration, as explained below -->

选项

allowMultipleChoices

默认情况下,在表单页面(editnew)中,您只能选择字段的可能值之一。使用此选项可以允许选择不限数量的项目

1
yield ChoiceField::new('...')->allowMultipleChoices();

autocomplete

默认情况下,所有可能的项目都会一次性加载并显示在项目选择器中。如果项目很多,请考虑使用此选项根据用户输入动态过滤项目

1
yield ChoiceField::new('...')->autocomplete();

这是 autocomplete 字段展开后的外观

Default style of EasyAdmin choice field with autocomplete

escapeHtml

默认情况下,在可能值的列表中,所有 HTML 内容都会被转义。如果您的值包含 HTML 内容并希望呈现它们,请使用此选项

1
yield ChoiceField::new('...')->escapeHtml(false);

renderAsBadges

当使用这种字段时,列表通常将选定的值表示为徽章(例如,发票的已支付/未支付状态)。此选项提供了多种不同的方式,可以将您的选择转换为只读页面(indexdetail)中的徽章

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// all values are rendered with the same badge style (Bootstrap's ' secondary' style)
yield ChoiceField::new('...')->renderAsBadges();

// you can assign different badge styles per value using an array
// there's no need to assign styles for all values; unassigned values will
// be rendered with the 'secondary' style
yield ChoiceField::new('...')->renderAsBadges([
    // $value => $badgeStyleName
    'paid' => 'success',
    'pending' => 'warning',
    'refunded' => 'danger',
]);

// in addition to an array, you can also use a callback; this callback
// receives a FieldDto object as its first and only argument
// (unlike when using arrays, when using callables you must return a badge
// style for all possible values)
yield ChoiceField::new('...')->renderAsBadges(
    static fn (FieldDto $field): string => $field->getValue() < 10 ? 'warning' : 'primary'
);

// no badges are displayed for any value (this is the default behavior)
yield ChoiceField::new('...')->renderAsBadges(false);

内置的徽章样式与 Bootstrap 相同:'success''warning''danger''info''primary''secondary''light''dark'

renderAsNativeWidget

默认情况下,当字段值未展开呈现时(请参阅 renderExpanded 选项),它使用基于 TomSelect 库的高级 JavaScript 小部件。如果您更喜欢使用默认的 <select> HTML 元素,请使用此选项

1
yield ChoiceField::new('...')->renderAsNativeWidget();

renderExpanded

默认情况下,可能值的列表使用高级 JavaScript 小部件(或 <select> 元素;请参阅 renderAsNativeWidget 选项)显示。如果元素列表很短,则一次显示所有可能的值可能更方便

1
yield ChoiceField::new('...')->renderExpanded();

如果 allowMultipleChoices 为 false,则展开的字段将显示单选按钮

Default style of EasyAdmin choice field with radiobuttons

如果 allowMultipleChoices 为 true,则展开的字段将显示复选框

Default style of EasyAdmin choice field with checkboxes

setChoices

这是最重要的选项,因为它设置了字段的可能有效选项。这些选项的定义方式与 Symfony 表单相同:['用户可见的标签' => '提交的值', ...]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
yield ChoiceField::new('...')->setChoices([
    'Paid Invoice' => 'paid',
    'Invoice Sent but Unpaid' => 'pending',
    'Refunded Invoice' => 'refunded',
]);

// in addition to arrays, you can use PHP closures to return the list of values
// (the closure is given as arguments the current entity instance and the FieldDto)
yield ChoiceField::new('...')->setChoices(
    static fn (?MyEntity $foo): array => $foo->someField()->getChoices()
);

yield ChoiceField::new('...')->setChoices(
    static fn (?MyEntity $foo, FieldDto $field): array => $field->getValue() < 10 ? $foo->getLowStockOptions() : $foo->getNormalStockOptions()
);

此选项也支持 PHP 枚举,包括 UnitEnum 和 BackedEnum。假设您在项目的某个位置定义了以下 backed enum

1
2
3
4
5
6
7
namespace App\Config;

enum BlogPostStatus: string {
    case Draft = 'draft';
    case Published = 'published';
    case Deleted = 'deleted';
}

此枚举在 setChoices() 中以不同的方式支持

1
2
3
4
5
// you can set the options as all the possible cases of the Enum explicitly
yield ChoiceField::new('status')->setChoices(BlogPostStatus::cases());

// you can select only some of the possible enum values:
yield ChoiceField::new('status')->setChoices([BlogPostStatus::Draft, BlogPostStatus::Published]);

此外,EasyAdmin 还为与 PHP 枚举关联的 Doctrine 属性提供自动支持。考虑以下 Doctrine 实体

1
2
3
4
5
6
7
8
#[Entity]
class BlogPost
{
    // ...

    #[Column(type: 'string', enumType: BlogPostStatus::class)]
    public $status;
}

如果要显示该枚举的所有可能值,则无需显式添加它们

1
2
3
// there's no need to call ->setChoices(); EasyAdmin will get all possible
// values via Doctrine; it's equivalent to calling: ->setChoices(BlogPostStatus::cases())
yield ChoiceField::new('status');

setTranslatableChoices

PHP 不允许使用对象作为数组键。这就是为什么在使用 TranslatableMessage 对象来定义选项标签时,不能使用 setChoices() 方法的原因。

请改用此方法,该方法的工作方式与 setChoices() 相同,但会翻转顺序以使用 value => label 而不是 label => value

1
2
3
4
5
6
7
8
9
use function Symfony\Component\Translation\t;
// ...

yield ChoiceField::new('...')->setTranslatableChoices([
    'paid' => t('Paid Invoice'),
    'pending' => t('Invoice Sent but Unpaid'),
    // if you want, some choices can use strings instead of objects
    'refunded' => 'Refunded Invoice',
]);
这项工作,包括代码示例,根据 Creative Commons BY-SA 3.0 许可协议获得许可。
目录
    版本