FixUrlProtocolListener example

class FixUrlProtocolListenerTest extends TestCase
{
    /** * @dataProvider provideUrlToFix */
    public function testFixUrl($data)
    {
        $form = new Form(new FormConfigBuilder('name', null, new EventDispatcher()));
        $event = new FormEvent($form$data);

        $filter = new FixUrlProtocolListener('http');
        $filter->onSubmit($event);

        $this->assertSame('http://'.$data$event->getData());
    }

    public static function provideUrlToFix()
    {
        return [
            ['www.symfony.com'],
            ['symfony.com/doc'],
            ['twitter.com/@symfony'],
            [
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class UrlType extends AbstractType
{
    /** * @return void */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        if (null !== $options['default_protocol']) {
            $builder->addEventSubscriber(new FixUrlProtocolListener($options['default_protocol']));
        }
    }

    /** * @return void */
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        if ($options['default_protocol']) {
            $view->vars['attr']['inputmode'] = 'url';
            $view->vars['type'] = 'text';
        }
Home | Imprint | This part of the site doesn't use cookies.