11EasyAdmin File Field
22====================
33
4- This field is used to manage the uploading of files (PDFs, documents, etc.) to
5- the backend. The entity property only stores the path to the file and not its
6- binary contents, which are stored in a file.
4+ This field is used to manage the uploading of files (PDFs, documents, etc.) to the
5+ backend. The entity property only stores the path to the file (relative to the
6+ upload directory). The actual file contents are stored on the server filesystem
7+ or on any remote system configured via the `league/flysystem-bundle `_.
78
89In :ref: `form pages (edit and new) <crud-pages >` it looks like this:
910
@@ -45,9 +46,11 @@ setUploadDir
4546stored. The argument is the directory relative to your project root::
4647
4748 yield FileField::new('...')->setUploadDir('public/uploads/files/');
49+ // the property will only store the file path relative to this dir
50+ // (e.g. 'catalog.pdf', 'venue/contract.docx')
4851
49- Unlike `` ImageField ``, `` FileField `` does not define a default upload directory.
50- If you don't call this method, an exception will be thrown.
52+ `` FileField `` does not define a default upload directory. If you don't call this
53+ method, an exception will be thrown.
5154
5255setFileConstraints
5356~~~~~~~~~~~~~~~~~~
@@ -57,7 +60,7 @@ option to define the constraints applied to the uploaded file::
5760
5861 use Symfony\Component\Validator\Constraints\File;
5962
60- yield FileField::new('...')->setFileConstraints(new File(maxSize : '2M '));
63+ yield FileField::new('...')->setFileConstraints(new File(filenameCharset : 'ASCII '));
6164
6265setUploadedFileNamePattern
6366~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -94,7 +97,8 @@ The string pattern passed as argument can include the following special values:
9497
9598You can combine them in any way::
9699
97- yield FileField::new('...')->setUploadedFileNamePattern('[YYYY]/[MM]/[DD]/[slug]-[contenthash].[extension]');
100+ yield FileField::new('...')
101+ ->setUploadedFileNamePattern('[YYYY]/[MM]/[DD]/[slug]-[contenthash].[extension]');
98102
99103The argument of this method also accepts a closure that receives the Symfony's
100104``UploadedFile `` instance and the **current entity instance ** as arguments::
@@ -103,36 +107,38 @@ The argument of this method also accepts a closure that receives the Symfony's
103107 fn (UploadedFile $file): string => sprintf('upload_%d_%s.%s', random_int(1, 999), $file->getFilename(), $file->guessExtension()))
104108 );
105109
106- Unlike ``ImageField ``, the `` FileField `` closure also receives the entity as a
107- second argument. This allows naming files based on entity data. On the ``new ``
108- page, the entity is a fresh instance (possibly without an ID); on the ``edit ``
109- page, it has its current database values::
110+ The ``FileField `` closure also receives the entity as a second argument. This
111+ allows naming files based on entity data. On the ``new `` page, the entity is a
112+ fresh instance (possibly without an ID); on the ``edit `` page, it has its
113+ current database values::
110114
111115 yield FileField::new('...')->setUploadedFileNamePattern(
112116 static fn (UploadedFile $file, MyEntity $entity): string => sprintf('%s/[name].[extension]', $entity->getSlug()))
113117 );
114118
115- mimeTypes
116- ~~~~~~~~~
119+ isDeletable
120+ ~~~~~~~~~~~
117121
118- By default, all file types are accepted. Use this option to restrict the allowed
119- MIME types. The value is a string with a comma-separated list of file extensions
120- or MIME types. You can use any value valid in the `HTML "accept" attribute `_::
122+ By default, the file upload widget shows a "delete" checkbox that allows users
123+ to remove the uploaded file. Use this option to hide that checkbox::
121124
122- yield FileField::new('...')->mimeTypes('.pdf,.doc,.docx');
123- yield FileField::new('...')->mimeTypes('video/*');
124- yield FileField::new('...')->mimeTypes('image/*');
125- yield FileField::new('...')->mimeTypes('.doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document');
125+ yield FileField::new('...')->isDeletable(false);
126126
127- When this option is set, the corresponding MIME types are also added
128- automatically as validation constraints. You can customize the error message
129- shown when the validation fails by passing a second argument::
127+ isDownloadable
128+ ~~~~~~~~~~~~~~
130129
131- yield FileField::new('...')->mimeTypes('.pdf', 'The file "{{ name }}" has MIME type "{{ type }}" but only "{{ types }}" are allowed.');
130+ By default, a link to download the uploaded file is displayed next to the form
131+ field. Use this option to hide that link::
132132
133- The available placeholders for the error message are: ``{{ file }} `` (the absolute
134- file path), ``{{ name }} `` (the base file name), ``{{ type }} `` (the MIME type of
135- the uploaded file) and ``{{ types }} `` (the list of allowed MIME types).
133+ yield FileField::new('...')->isDownloadable(false);
134+
135+ isViewable
136+ ~~~~~~~~~~
137+
138+ By default, a link to view the uploaded file is displayed next to the form field.
139+ Use this option to hide that link::
140+
141+ yield FileField::new('...')->isViewable(false);
136142
137143maxSize
138144~~~~~~~
@@ -153,6 +159,28 @@ file path), ``{{ name }}`` (the base file name), ``{{ size }}`` (the file size),
153159``{{ limit }} `` (the maximum allowed size) and ``{{ suffix }} `` (the size unit,
154160e.g. ``kB ``, ``MB ``).
155161
162+ mimeTypes
163+ ~~~~~~~~~
164+
165+ By default, all file types are accepted. Use this option to restrict the allowed
166+ MIME types. The value is a string with a comma-separated list of file extensions
167+ or MIME types. You can use any value valid in the `HTML "accept" attribute `_::
168+
169+ yield FileField::new('...')->mimeTypes('.pdf,.doc,.docx');
170+ yield FileField::new('...')->mimeTypes('video/*');
171+ yield FileField::new('...')->mimeTypes('image/*');
172+ yield FileField::new('...')->mimeTypes('.doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document');
173+
174+ When this option is set, the corresponding MIME types are also added
175+ automatically as validation constraints. You can customize the error message
176+ shown when the validation fails by passing a second argument::
177+
178+ yield FileField::new('...')->mimeTypes('.pdf', 'The file "{{ name }}" has MIME type "{{ type }}" but only "{{ types }}" are allowed.');
179+
180+ The available placeholders for the error message are: ``{{ file }} `` (the absolute
181+ file path), ``{{ name }} `` (the base file name), ``{{ type }} `` (the MIME type of
182+ the uploaded file) and ``{{ types }} `` (the list of allowed MIME types).
183+
156184Replaced File Behavior
157185~~~~~~~~~~~~~~~~~~~~~~
158186
@@ -178,30 +206,6 @@ controls what happens to the old file on disk. There are three behaviors:
178206
179207 yield FileField::new('...')->keepReplacedFileOrFail();
180208
181- isViewable
182- ~~~~~~~~~~
183-
184- By default, a link to view the uploaded file is displayed next to the form field.
185- Use this option to hide that link::
186-
187- yield FileField::new('...')->isViewable(false);
188-
189- isDownloadable
190- ~~~~~~~~~~~~~~
191-
192- By default, a link to download the uploaded file is displayed next to the form
193- field. Use this option to hide that link::
194-
195- yield FileField::new('...')->isDownloadable(false);
196-
197- isDeletable
198- ~~~~~~~~~~~
199-
200- By default, the file upload widget shows a "delete" checkbox that allows users
201- to remove the uploaded file. Use this option to hide that checkbox::
202-
203- yield FileField::new('...')->isDeletable(false);
204-
205209Flysystem Integration (Remote Storage)
206210--------------------------------------
207211
0 commit comments