src/Kunstmaan/NodeBundle/Form/EventListener/URLChooserLinkTypeSubscriber.php
public function postSubmit(FormEvent $event)
    {
        // Suppress validation
        $event->stopPropagation();

        $constraints = [];
        $attributes['class'] = 'js-change-urlchooser';

        $form = $event->getForm()->getParent();
        $linkType = $event->getData();

        if ($linkType) {
            $form->remove('link_url');

            switch ($linkType) {
                case URLChooserType::INTERNAL:
                    $attributes['choose_url'] = true;

                    break;
                case URLChooserType::EXTERNAL:
                    $attributes['placeholder'] = 'https://';
                    $constraints[] = new Url();

                    break;
                case URLChooserType::EMAIL:
                    $constraints[] = new Email();

                    break;
            }

            $form->add('link_url', TextType::class, array(
                'label' => 'URL',
                'required' => true,
                'attr' => $attributes,
                'constraints' => $constraints,
                'error_bubbling' => true,
            ));
        }
    }
public function postSubmit(FormEvent $event)
    {
        // Suppress validation
        $event->stopPropagation();

        $constraints = [];
        $attributes['class'] = 'js-change-urlchooser';

        $form = $event->getForm()->getParent();
        $linkType = $event->getData();

        if ($linkType) {
            $form->remove('link_url');

            switch ($linkType) {
                case URLChooserType::INTERNAL:
                    $attributes['choose_url'] = true;

                    break;
                case URLChooserType::EXTERNAL:
                    $attributes['placeholder'] = 'https://';
                    $constraints[] = new Url();

                    break;
                case URLChooserType::EMAIL:
                    $constraints[] = new Email();

                    break;
            }

            $form->add('link_url', TextType::class, array(
                'label' => 'URL',
                'required' => true,
                'attr' => $attributes,
                'constraints' => $constraints,
                'error_bubbling' => true,
            ));
        }
    }
public function postSubmit ( FormEvent $ event ) { // Suppress validation $ event -> stopPropagation ( ) ; $ constraints = [ ] ; $ attributes [ 'class' ] = 'js-change-urlchooser' ; $ form = $ event -> getForm ( ) -> getParent ( ) ; $ linkType = $ event -> getData ( ) ; if ( $ linkType ) { $ form -> remove ( 'link_url' ) ; switch ( $ linkType ) { case URLChooserType :: INTERNAL : $ attributes [ 'choose_url' ] = true ; break ; case URLChooserType :: EXTERNAL : $ attributes [ 'placeholder' ] = 'https://' ; $ constraints [ ] = new Url ( ) ; break ; case URLChooserType :: EMAIL : $ constraints [ ] = new Email ( ) ; break ; } $ form -> add ( 'link_url' , TextType :: class , array ( 'label' => 'URL' , 'required' => true , 'attr' => $ attributes , 'constraints' => $ constraints , 'error_bubbling' => true , ) ) ; } }
When changing the link type, the form get's submitted with an ajax callback in the url_chooser.js;
We add the URL field only as an URL Chooser if it's an external link.

@param FormEvent $event
When changing the link type the form get s submitted with an ajax callback in the url_chooser . js ; We add the URL field only as an URL Chooser if it s an external link .