Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3fac090

Browse files
ChoiceField.rst - Enum with human-readable values
1 parent b5c785b commit 3fac090

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

doc/fields/ChoiceField.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,29 @@ add them explicitly::
182182
// values via Doctrine; it's equivalent to calling: ->setChoices(BlogPostStatus::cases())
183183
yield ChoiceField::new('status');
184184

185+
Also, if you want to have human-readable values from your enum in your admin, add a dedicated method to your enum (here ``getLabel()``)::
186+
187+
namespace App\Config;
188+
189+
enum BlogPostStatus: string {
190+
case Draft = 'draft';
191+
case Published = 'published';
192+
case Deleted = 'deleted';
193+
194+
public function getLabel(): string
195+
{
196+
return match($this) {
197+
self::Draft => 'Draft 📝',
198+
self::Published => 'Published ✅',
199+
self::Deleted => 'Deleted ❌',
200+
};
201+
}
202+
}
203+
204+
Then, configure your field to use it with ``setFormTypeOption()``::
205+
206+
yield ChoiceField::new('status')->setFormTypeOption('choice_label', fn ($value) => $value->getLabel());
207+
185208
setTranslatableChoices
186209
~~~~~~~~~~~~~~~~~~~~~~
187210

0 commit comments

Comments
 (0)