Skip to content

Commit

Permalink
Split add field and add event in form type for image and video elements
Browse files Browse the repository at this point in the history
  • Loading branch information
maximehuran committed Apr 12, 2024
1 parent 1d8e66d commit 73a4066
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Form/Type/UiElement/ImageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Validator\Constraints as Assert;
use Webmozart\Assert\Assert as AssertAssert;

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 @@ -53,7 +58,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
new Assert\AtLeastOneOf([
'includeInternalMessages' => false,
'message' => 'monsieurbiz_richeditor_plugin.not_valid_url',
'constraints'=> [
'constraints' => [
new Assert\Url(['protocols' => ['http', 'https'], 'relativeProtocol' => true]),
new Assert\Regex(['pattern' => '`^(#|/[^/])`']),
],
Expand All @@ -62,7 +67,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 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

0 comments on commit 73a4066

Please sign in to comment.