Skip to content

Commit

Permalink
Merge pull request #223 from maximehuran/feature/update-forms
Browse files Browse the repository at this point in the history
Update link validation and split fields and events in builder
  • Loading branch information
maximehuran authored Apr 12, 2024
2 parents a289a8f + 73a4066 commit bbade9a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/Form/Type/UiElement/ButtonLinkType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion src/Form/Type/UiElement/ImageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand All @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions src/Form/Type/UiElement/VideoType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand All @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/translations/validators.en.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
monsieurbiz_richeditor_plugin:
not_valid_url: 'This value is not a valid URL (absolute or relative) or a valid hashtag.'
2 changes: 2 additions & 0 deletions src/Resources/translations/validators.fr.yaml
Original file line number Diff line number Diff line change
@@ -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.'

0 comments on commit bbade9a

Please sign in to comment.