Skip to content

Commit

Permalink
Added Regex constraint to Pattern rule mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
boekkooi committed Apr 28, 2015
1 parent b04380e commit 6d5c718
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
54 changes: 54 additions & 0 deletions src/Form/Rule/Mapping/PatternRule.php
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';
}
}
7 changes: 7 additions & 0 deletions src/Resources/config/form_rule_additional_mappers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ services:
public: false
tags:
- { name: form_rule_constraint_mapper }

boekkooi.jquery_validation.mapper.pattern:
class: Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\PatternRule
arguments: [ %boekkooi.jquery_validation.additional.pattern% ]
public: false
tags:
- { name: form_rule_constraint_mapper }
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public function buildForm(FormBuilderInterface $builder, array $options)
)),
),
))

->add('pattern', 'text', array(
'constraints' => array(
new Constraints\Regex('/^[a-zA-Z]+$/'),
),
))
;
}

Expand Down
6 changes: 4 additions & 2 deletions tests/Functional/TotalFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,15 +539,17 @@ public function it_should_additional_rules()
"additional_rules\x5Bipv4_ipv6\x5D": {"one_or_other": {"ipv4": true, "ipv6": true}},
"additional_rules\x5Biban\x5D": {"iban": true},
"additional_rules\x5Bluhn\x5D": {"luhn": true},
"additional_rules\x5Bfile\x5D": {"accept": "text\/plain,application\/pdf"}
"additional_rules\x5Bfile\x5D": {"accept": "text\/plain,application\/pdf"},
"additional_rules\x5Bpattern\x5D": {"pattern": "[a-zA-Z]+"}
},
messages: {
"additional_rules\x5Bipv4\x5D": {"ipv4": "This\x20is\x20not\x20a\x20valid\x20IP\x20address."},
"additional_rules\x5Bipv6\x5D": {"ipv6": "This\x20is\x20not\x20a\x20valid\x20IP\x20address."},
"additional_rules\x5Bipv4_ipv6\x5D": {"one_or_other": "This\x20is\x20not\x20a\x20valid\x20IP\x20address."},
"additional_rules\x5Biban\x5D": {"iban": "This\x20is\x20not\x20a\x20valid\x20International\x20Bank\x20Account\x20Number\x20\x28IBAN\x29."},
"additional_rules\x5Bluhn\x5D": {"luhn": "Invalid\x20card\x20number."},
"additional_rules\x5Bfile\x5D": {"accept": "The\x20mime\x20type\x20of\x20the\x20file\x20is\x20invalid\x20\x28\x7B\x7B\x20type\x20\x7D\x7D\x29.\x20Allowed\x20mime\x20types\x20are\x20text\x2Fplain,\x20application\x2Fpdf."}
"additional_rules\x5Bfile\x5D": {"accept": "The\x20mime\x20type\x20of\x20the\x20file\x20is\x20invalid\x20\x28\x7B\x7B\x20type\x20\x7D\x7D\x29.\x20Allowed\x20mime\x20types\x20are\x20text\x2Fplain,\x20application\x2Fpdf."},
"additional_rules\x5Bpattern\x5D": {"pattern": "This\x20value\x20is\x20not\x20valid."}
}
});
})(jQuery);',
Expand Down

0 comments on commit 6d5c718

Please sign in to comment.