diff --git a/src/Entity/Attachments/Attachment.php b/src/Entity/Attachments/Attachment.php
index a4807a21..98565fe9 100644
--- a/src/Entity/Attachments/Attachment.php
+++ b/src/Entity/Attachments/Attachment.php
@@ -25,6 +25,7 @@ namespace App\Entity\Attachments;
use App\Entity\Base\AbstractNamedDBElement;
use App\Validator\Constraints\Selectable;
use Doctrine\ORM\Mapping as ORM;
+use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use function in_array;
use InvalidArgumentException;
@@ -95,6 +96,14 @@ abstract class Attachment extends AbstractNamedDBElement
*/
protected string $path = '';
+ /**
+ * @var string the name of this element
+ * @ORM\Column(type="string")
+ * @Assert\NotBlank(message="validator.attachment.name_not_blank")
+ * @Groups({"simple", "extended", "full"})
+ */
+ protected string $name = '';
+
/**
* ORM mapping is done in sub classes (like PartAttachment).
*/
diff --git a/src/Form/AttachmentFormType.php b/src/Form/AttachmentFormType.php
index fbf75b97..6f33fde0 100644
--- a/src/Form/AttachmentFormType.php
+++ b/src/Form/AttachmentFormType.php
@@ -44,6 +44,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Validator\Constraints\File;
+use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Url;
use Symfony\Contracts\Translation\TranslatorInterface;
@@ -72,9 +73,12 @@ class AttachmentFormType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options): void
{
- $builder->add('name', TextType::class, [
- 'label' => 'attachment.edit.name',
- ])
+ $builder
+ ->add('name', TextType::class, [
+ 'label' => 'attachment.edit.name',
+ 'required' => false,
+ 'empty_data' => '',
+ ])
->add('attachment_type', StructuralEntityType::class, [
'label' => 'attachment.edit.attachment_type',
'class' => AttachmentType::class,
@@ -134,6 +138,7 @@ class AttachmentFormType extends AbstractType
],
]);
+
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event): void {
$form = $event->getForm();
$attachment = $form->getData();
@@ -141,13 +146,27 @@ class AttachmentFormType extends AbstractType
$file_form = $form->get('file');
$file = $file_form->getData();
- if ($attachment instanceof Attachment && $file instanceof UploadedFile && $attachment->getAttachmentType(
- ) && !$this->submitHandler->isValidFileExtension($attachment->getAttachmentType(), $file)) {
+ if (!$attachment instanceof Attachment) {
+ return;
+ }
+
+ if (!$file instanceof UploadedFile) {
+ return;
+ }
+
+ //Ensure that the file extension is allowed for the selected attachment type
+ if ($attachment->getAttachmentType()
+ && !$this->submitHandler->isValidFileExtension($attachment->getAttachmentType(), $file)) {
$event->getForm()->get('file')->addError(
new FormError($this->translator->trans('validator.file_ext_not_allowed'))
);
}
- });
+
+ //If the name is empty, use the original file name as attachment name
+ if (empty($attachment->getName())) {
+ $attachment->setName($file->getClientOriginalName());
+ }
+ }, 100000);
//Check the secure file checkbox, if file is in securefile location
$builder->get('secureFile')->addEventListener(
diff --git a/translations/validators.en.xlf b/translations/validators.en.xlf
index eba403cc..7150f094 100644
--- a/translations/validators.en.xlf
+++ b/translations/validators.en.xlf
@@ -299,5 +299,11 @@
The part name does not match the regular expression stated by the category: %regex%
+
+
+ validator.attachment.name_not_blank
+ Set a value here, or upload a file to automatically use its filename as name for the attachment.
+
+