Skip to content

Commit

Permalink
Merge pull request #2590 from acrobat/external-url-validation-break
Browse files Browse the repository at this point in the history
[NodeBundle] Fix url chooser to allow # and #! urls
  • Loading branch information
acrobat committed Nov 26, 2019
2 parents 4a9921c + 126207d commit 9c7f720
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Kunstmaan\NodeBundle\Form\Type\URLChooserType;
use Kunstmaan\NodeBundle\Validation\URLValidator;
use Kunstmaan\NodeBundle\Validator\Constraint\ValidExternalUrl;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormEvent;
Expand Down Expand Up @@ -58,7 +59,7 @@ private function formModifier(FormEvent $event)
} // Else, it's an external link
else {
$form->get('link_type')->setData(URLChooserType::EXTERNAL);
$constraints[] = new Url();
$constraints[] = new ValidExternalUrl();
}
} else {
$choices = $form->get('link_type')->getConfig()->getOption('choices');
Expand All @@ -71,7 +72,7 @@ private function formModifier(FormEvent $event)
break;
case URLChooserType::EXTERNAL:
$attributes['placeholder'] = 'https://';
$constraints[] = new Url();
$constraints[] = new ValidExternalUrl();

break;
case URLChooserType::EMAIL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kunstmaan\NodeBundle\Form\EventListener;

use Kunstmaan\NodeBundle\Form\Type\URLChooserType;
use Kunstmaan\NodeBundle\Validator\Constraint\ValidExternalUrl;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormEvent;
Expand Down Expand Up @@ -52,7 +53,7 @@ public function postSubmit(FormEvent $event)
break;
case URLChooserType::EXTERNAL:
$attributes['placeholder'] = 'https://';
$constraints[] = new Url();
$constraints[] = new ValidExternalUrl();

break;
case URLChooserType::EMAIL:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Kunstmaan\NodeBundle\Tests\unit\Validator\Constraint;

use Kunstmaan\NodeBundle\Validator\Constraint\ValidExternalUrl;
use Kunstmaan\NodeBundle\Validator\Constraint\ValidExternalUrlValidator;
use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

class ValidExternalUrlValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator()
{
return new ValidExternalUrlValidator();
}

/**
* @dataProvider getValidUrls
*/
public function testValidUrls($url)
{
$this->validator->validate($url, new ValidExternalUrl());

$this->assertNoViolation();
}

/**
* @dataProvider getInvalidUrls
*/
public function testInvalidUrls($url)
{
$this->validator->validate($url, new ValidExternalUrl());

$this->buildViolation('This value is not a valid URL.')
->setParameter('{{ value }}', '"'.$url.'"')
->setCode(Url::INVALID_URL_ERROR)
->assertRaised();
}

public function getValidUrls()
{
return [
['http://www.example.com'],
['https://example.com'],
['#'],
['#anchor-name'],
['#!'],
];
}

public function getInvalidUrls()
{
return [
['example.com'],
['www.example.com'],
['!#'],
['abc#anchor-name'],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Kunstmaan\NodeBundle\Validator\Constraint;

use Symfony\Component\Validator\Constraint;

final class ValidExternalUrl extends Constraint
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Kunstmaan\NodeBundle\Validator\Constraint;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\Constraints\UrlValidator;
use Symfony\Component\Validator\ConstraintValidator;

final class ValidExternalUrlValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
if (strpos($value, '#') === 0) {
return;
}

$urlValidator = new UrlValidator();
$urlValidator->initialize($this->context);
$urlValidator->validate($value, new Url());
}
}

0 comments on commit 9c7f720

Please sign in to comment.