-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Regex constraint to Pattern rule mapping
- Loading branch information
Showing
4 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
namespace Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping; | ||
|
||
use Boekkooi\Bundle\JqueryValidationBundle\Exception\LogicException; | ||
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\ConstraintRule; | ||
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\ConstraintMapperInterface; | ||
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCollection; | ||
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleMessage; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
|
||
/** | ||
* @author Warnar Boekkooi <warnar@boekkooi.net> | ||
*/ | ||
class PatternRule implements ConstraintMapperInterface | ||
{ | ||
const RULE_NAME = 'pattern'; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $enabled; | ||
|
||
public function __construct($enabled) | ||
{ | ||
$this->enabled = $enabled; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection) | ||
{ | ||
if (!$this->supports($constraint, $form)) { | ||
throw new LogicException(); | ||
} | ||
|
||
/** @var \Symfony\Component\Validator\Constraints\Regex $constraint */ | ||
$collection->set( | ||
self::RULE_NAME, | ||
new ConstraintRule( | ||
self::RULE_NAME, | ||
$constraint->getHtmlPattern(), | ||
new RuleMessage($constraint->message), | ||
$constraint->groups | ||
) | ||
); | ||
} | ||
|
||
public function supports(Constraint $constraint, FormInterface $form) | ||
{ | ||
return $this->enabled && get_class($constraint) === 'Symfony\Component\Validator\Constraints\Regex'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters