diff --git a/src/Form/Type/UiElement/ButtonLinkType.php b/src/Form/Type/UiElement/ButtonLinkType.php index 8bd6d7e6..f8008387 100644 --- a/src/Form/Type/UiElement/ButtonLinkType.php +++ b/src/Form/Type/UiElement/ButtonLinkType.php @@ -32,12 +32,28 @@ public function buildForm(FormBuilderInterface $builder, array $options): void new Assert\NotBlank([]), ]; $constraintsLink = [ - new Assert\Url([]), + new Assert\AtLeastOneOf([ + 'includeInternalMessages' => false, + 'message' => 'monsieurbiz_richeditor_plugin.not_valid_url', + 'constraints' => [ + new Assert\Url(['protocols' => ['http', 'https'], 'relativeProtocol' => true]), + new Assert\Regex(['pattern' => '`^(#|/[^/])`']), + ], + ]), new Assert\NotBlank([]), ]; } else { $constraintsLabel = []; - $constraintsLink = []; + $constraintsLink = [ + new Assert\AtLeastOneOf([ + 'includeInternalMessages' => false, + 'message' => 'monsieurbiz_richeditor_plugin.not_valid_url', + 'constraints' => [ + new Assert\Url(['protocols' => ['http', 'https'], 'relativeProtocol' => true]), + new Assert\Regex(['pattern' => '`^(#|/[^/])`']), + ], + ]), + ]; } $builder diff --git a/src/Form/Type/UiElement/ImageType.php b/src/Form/Type/UiElement/ImageType.php index c33ead69..89b0900c 100644 --- a/src/Form/Type/UiElement/ImageType.php +++ b/src/Form/Type/UiElement/ImageType.php @@ -29,6 +29,12 @@ class ImageType extends AbstractType * @inheritdoc */ public function buildForm(FormBuilderInterface $builder, array $options): void + { + $this->addFields($builder, $options); + $this->addEvents($builder, $options); + } + + public function addFields(FormBuilderInterface $builder, array $options): void { $builder ->add('image', FileType::class, [ @@ -49,12 +55,22 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'required' => false, 'label' => 'monsieurbiz_richeditor_plugin.ui_element.monsieurbiz.image.field.link', 'constraints' => [ - new Assert\Url([]), + new Assert\AtLeastOneOf([ + 'includeInternalMessages' => false, + 'message' => 'monsieurbiz_richeditor_plugin.not_valid_url', + 'constraints' => [ + new Assert\Url(['protocols' => ['http', 'https'], 'relativeProtocol' => true]), + new Assert\Regex(['pattern' => '`^(#|/[^/])`']), + ], + ]), ], ]) ->add('align', AlignmentType::class) ; + } + public function addEvents(FormBuilderInterface $builder, array $options): void + { $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event): void { // Change image field constraints depending on submitted value $options = $event->getForm()->get('image')->getConfig()->getOptions(); diff --git a/src/Form/Type/UiElement/VideoType.php b/src/Form/Type/UiElement/VideoType.php index ffc3fbad..3d2dda7f 100644 --- a/src/Form/Type/UiElement/VideoType.php +++ b/src/Form/Type/UiElement/VideoType.php @@ -27,6 +27,12 @@ class VideoType extends AbstractType * @inheritdoc */ public function buildForm(FormBuilderInterface $builder, array $options): void + { + $this->addFields($builder, $options); + $this->addEvents($builder, $options); + } + + public function addFields(FormBuilderInterface $builder, array $options): void { $builder ->add('video', FileType::class, [ @@ -43,7 +49,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ]) ->add('align', AlignmentType::class) ; + } + public function addEvents(FormBuilderInterface $builder, array $options): void + { $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event): void { // Change video field constraints depending on submitted value $options = $event->getForm()->get('video')->getConfig()->getOptions(); diff --git a/src/Resources/translations/validators.en.yaml b/src/Resources/translations/validators.en.yaml new file mode 100644 index 00000000..eb22719e --- /dev/null +++ b/src/Resources/translations/validators.en.yaml @@ -0,0 +1,2 @@ +monsieurbiz_richeditor_plugin: + not_valid_url: 'This value is not a valid URL (absolute or relative) or a valid hashtag.' diff --git a/src/Resources/translations/validators.fr.yaml b/src/Resources/translations/validators.fr.yaml new file mode 100644 index 00000000..fd1fa81c --- /dev/null +++ b/src/Resources/translations/validators.fr.yaml @@ -0,0 +1,2 @@ +monsieurbiz_richeditor_plugin: + not_valid_url: 'Cette valeur n''est pas une URL valide (absolue ou relative) ou un hashtag valide.'