Skip to content

Commit 550ae0d

Browse files
committed
Refactored rule::depends into rule::conditions
1 parent 5eb19f3 commit 550ae0d

File tree

13 files changed

+559
-221
lines changed

13 files changed

+559
-221
lines changed

.php_cs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,10 @@
11
<?php
22
$finder = Symfony\CS\Finder\DefaultFinder::create()
33
->in(__DIR__ . '/src')
4+
->in(__DIR__ . '/tests')
45
;
56

67
return Symfony\CS\Config\Config::create()
7-
->fixers(array(
8-
'encoding',
9-
'linefeed',
10-
'indentation',
11-
'trailing_spaces',
12-
'unused_use',
13-
'visibility',
14-
'short_tag',
15-
'php_closing_tag',
16-
'return',
17-
'braces',
18-
'lowercase_constants',
19-
'lowercase_keywords',
20-
'include',
21-
'function_declaration',
22-
'controls_spaces',
23-
'spaces_cast',
24-
'elseif',
25-
'eof_ending',
26-
'standardize_not_equal',
27-
'new_with_braces'
28-
))
8+
->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
299
->finder($finder)
3010
;
31-

src/DependencyInjection/Compiler/ExtensionPass.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ protected function registerRuleCompilers(ContainerBuilder $container)
6868
$container->getDefinition('boekkooi.jquery_validation.rule_compiler')->replaceArgument(0, $references);
6969
}
7070

71-
7271
/**
7372
* @param ContainerBuilder $container
7473
* @return array|null

src/Form/Rule.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ abstract class Rule
2424
public $message;
2525

2626
/**
27-
* A list of field names that require to be valid before the rule is used
28-
* @var array
27+
* A list rule conditions.
28+
* @var RuleCondition[]
2929
*/
30-
public $depends;
30+
public $conditions;
3131

32-
public function __construct($name, $options = null, RuleMessage $message = null, array $depends = array())
32+
public function __construct($name, $options = null, RuleMessage $message = null, array $conditions = array())
3333
{
3434
$this->name = $name;
3535
$this->options = $options;
3636
$this->message = $message;
37-
$this->depends = $depends;
37+
$this->conditions = $conditions;
3838
}
3939
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
namespace Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Condition;
3+
4+
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCondition;
5+
use Boekkooi\Bundle\JqueryValidationBundle\Form\Util\FormHelper;
6+
7+
/**
8+
* @author Warnar Boekkooi <warnar@boekkooi.net>
9+
*/
10+
class FieldDependency implements RuleCondition
11+
{
12+
const FIELD_VALID = '=';
13+
const FIELD_INVALID = '!';
14+
15+
/**
16+
* Dependent field
17+
* @var string
18+
*/
19+
public $field;
20+
21+
/**
22+
* @var string
23+
*/
24+
public $condition;
25+
26+
public function __construct($field, $condition = self::FIELD_VALID)
27+
{
28+
$this->field = FormHelper::getFormName($field);
29+
$this->condition = $condition;
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function macro()
36+
{
37+
return 'field_dependency';
38+
}
39+
}

src/Form/Rule/ConstraintRule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class ConstraintRule extends Rule
1616
*/
1717
public $groups;
1818

19-
public function __construct($name, $options = null, RuleMessage $message = null, array $groups = array(Constraint::DEFAULT_GROUP), array $depends = array())
19+
public function __construct($name, $options = null, RuleMessage $message = null, array $groups = array(Constraint::DEFAULT_GROUP), array $conditions = array())
2020
{
21-
parent::__construct($name, $options, $message, $depends);
21+
parent::__construct($name, $options, $message, $conditions);
2222

2323
$this->groups = $groups;
2424
}

src/Form/Rule/Processor/CompoundCopyToChildPass.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleContextBuilder;
55
use Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleProcessorContext;
66
use Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleProcessorInterface;
7+
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Condition\FieldDependency;
78
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\ConstraintRule;
89
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\RequiredRule;
910
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCollection;
@@ -102,7 +103,7 @@ private function registerRulesForChildren(FormRuleContextBuilder $formRuleContex
102103
if ($collection->containsKey($name)) {
103104
$childRule = $collection[$name];
104105
$childRule->message = $rule->message;
105-
$childRule->depends = $rule->depends;
106+
$childRule->conditions = $rule->conditions;
106107
if ($childRule instanceof ConstraintRule && $rule instanceof ConstraintRule) {
107108
$childRule->groups = array_unique(
108109
array_merge($childRule->groups, $rule->groups)
@@ -114,7 +115,7 @@ private function registerRulesForChildren(FormRuleContextBuilder $formRuleContex
114115

115116
$rule = clone $rule;
116117
$rule->message = $message;
117-
$rule->depends[] = $childView->vars['full_name'];
118+
$rule->conditions[] = new FieldDependency($childView->vars['full_name']);
118119
}
119120
}
120121

src/Form/Rule/Processor/DateTimeToArrayTransformerPass.php

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33

44
use Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleContextBuilder;
55
use Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleProcessorContext;
6+
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Condition\FieldDependency;
67
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\MaxRule;
78
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\MinRule;
89
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\NumberRule;
910
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\RequiredRule;
1011
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCollection;
1112
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleMessage;
1213
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\TransformerRule;
13-
use Boekkooi\Bundle\JqueryValidationBundle\Form\Util\FormViewRecursiveIterator;
14+
use Boekkooi\Bundle\JqueryValidationBundle\Form\Util\FormHelper;
15+
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer;
1416
use Symfony\Component\Form\FormView;
1517

1618
/**
@@ -36,55 +38,58 @@ public function process(FormRuleProcessorContext $context, FormRuleContextBuilde
3638
return;
3739
}
3840

41+
/** @var \Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer $transformer */
3942
$transformer = $this->findTransformer($formConfig, 'Symfony\\Component\\Form\\Extension\\Core\\DataTransformer\\DateTimeToArrayTransformer');
4043
if ($transformer === null) {
4144
return;
4245
}
4346
$view = $context->getView();
44-
45-
/** @var FormView[] $it */
46-
$it = new \RecursiveIteratorIterator(
47-
new FormViewRecursiveIterator($view->getIterator()),
48-
\RecursiveIteratorIterator::LEAVES_ONLY
49-
);
47+
$fields = $this->getTransformerFields($transformer);
5048
$invalidMessage = $this->getFormRuleMessage($formConfig);
5149

52-
$depends = array();
53-
foreach ($it as $childView) {
50+
$views = array();
51+
$conditions = array();
52+
foreach ($fields as $fieldName) {
53+
$childView = $view->children[$fieldName];
54+
55+
// Get child rules collection
5456
$childRules = $formRuleContext->get($childView);
5557
if ($childRules === null) {
5658
$formRuleContext->add($childView, new RuleCollection());
5759
$childRules = $formRuleContext->get($childView);
5860
}
5961

62+
// Register rules
6063
$this->addNumberCheck(
6164
$childView,
6265
$childRules,
6366
$invalidMessage,
64-
$depends
67+
$conditions
6568
);
66-
$depends[] = $childView->vars['full_name'];
69+
70+
$views[] = FormHelper::getFormName($childView);
71+
$conditions[] = new FieldDependency($childView);
6772
}
6873

69-
if ($this->useGroupRule && count($depends) > 1) {
70-
$rules = $formRuleContext->get(array_shift($depends));
74+
if ($this->useGroupRule && count($views) > 1) {
75+
$rules = $formRuleContext->get(array_shift($views));
7176
$rules->set(
7277
CompoundCopyToChildPass::RULE_NAME_GROUP_REQUIRED,
73-
new TransformerRule(CompoundCopyToChildPass::RULE_NAME_GROUP_REQUIRED, $depends, $invalidMessage)
78+
new TransformerRule(CompoundCopyToChildPass::RULE_NAME_GROUP_REQUIRED, $views, $invalidMessage)
7479
);
7580
}
7681
}
7782

78-
private function addNumberCheck(FormView $view, RuleCollection $rules, RuleMessage $message = null, array $depends = array())
83+
private function addNumberCheck(FormView $view, RuleCollection $rules, RuleMessage $message = null, array $conditions = array())
7984
{
80-
if (!$this->useGroupRule && count($depends) > 0) {
85+
if (!$this->useGroupRule && count($conditions) > 0) {
8186
$rules->set(
8287
RequiredRule::RULE_NAME,
8388
new TransformerRule(
8489
RequiredRule::RULE_NAME,
8590
true,
8691
$message,
87-
$depends
92+
$conditions
8893
)
8994
);
9095
}
@@ -98,7 +103,7 @@ private function addNumberCheck(FormView $view, RuleCollection $rules, RuleMessa
98103
NumberRule::RULE_NAME,
99104
true,
100105
$message,
101-
$depends
106+
$conditions
102107
)
103108
);
104109

@@ -125,11 +130,22 @@ private function addNumberCheck(FormView $view, RuleCollection $rules, RuleMessa
125130
}
126131
$rules->set(
127132
MinRule::RULE_NAME,
128-
new TransformerRule(MinRule::RULE_NAME, $min, $message, $depends)
133+
new TransformerRule(MinRule::RULE_NAME, $min, $message, $conditions)
129134
);
130135
$rules->set(
131136
MaxRule::RULE_NAME,
132-
new TransformerRule(MaxRule::RULE_NAME, $max, $message, $depends)
137+
new TransformerRule(MaxRule::RULE_NAME, $max, $message, $conditions)
133138
);
134139
}
140+
141+
private function getTransformerFields(DateTimeToArrayTransformer $transformer)
142+
{
143+
$property = new \ReflectionProperty(
144+
'Symfony\\Component\\Form\\Extension\\Core\\DataTransformer\\DateTimeToArrayTransformer',
145+
'fields'
146+
);
147+
$property->setAccessible(true);
148+
149+
return $property->getValue($transformer);
150+
}
135151
}

src/Form/RuleCondition.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
namespace Boekkooi\Bundle\JqueryValidationBundle\Form;
3+
4+
/**
5+
* @author Warnar Boekkooi <warnar@boekkooi.net>
6+
*/
7+
interface RuleCondition
8+
{
9+
/**
10+
* Get the twig macro name to call.
11+
* @return string
12+
*/
13+
public function macro();
14+
}

src/Form/RuleMessage.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@
66
*/
77
class RuleMessage
88
{
9+
/**
10+
* A message text.
11+
* @var string
12+
*/
913
public $message;
1014

15+
/**
16+
* A list of message parameters.
17+
* @var array
18+
*/
1119
public $parameters;
1220

21+
/**
22+
* @var null
23+
*/
1324
public $plural;
1425

1526
public function __construct($message, array $parameters = array(), $plural = null)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{% macro field_dependency(condition, rule) %}
2+
{%- set isRequired = rule.name is sameas('required') -%}
3+
4+
{%- if isRequired -%}
5+
var dep = form.find("[name=\"{{ condition.field|e('js') }}\"]")[0];
6+
{%- endif -%}
7+
8+
if (
9+
{%- if condition.condition is sameas('!') -%}!{%- endif -%}
10+
(
11+
{%- if isRequired -%}
12+
!$.validator.methods.required.call(validator, validator.elementValue(dep), dep, true) || {% endif -%}
13+
"{{ condition.field|e('js') }}" in validator.errorMap || "{{ condition.field|e('js') }}" in validator.invalid)) {
14+
return false;
15+
}
16+
{% endmacro %}

src/Resources/views/Form/macros.js.twig

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,46 +44,42 @@
4444

4545
{# Generate a rule #}
4646
{% macro rule(rule, includeGroupDeps) %}
47-
{%- import _self as gen -%}
47+
{% import "BoekkooiJqueryValidationBundle:Form:conditions.js.twig" as macro_conditions %}
4848

4949
{%- set hasGroups = includeGroupDeps|default(false) and rule.groups|length > 0 -%}
50-
{%- set hasDepends = (rule.depends is not empty) -%}
50+
{%- set hasConditions = (rule.conditions is not empty) -%}
5151

52-
{%- if hasGroups or hasDepends -%}
52+
{%- if hasGroups or hasConditions -%}
5353
{%- set hasParams = rule.options is not sameas(true) -%}
5454

5555
{
5656
{%- if hasParams -%}
5757
param: {{- rule.options|json_encode()|raw -}},
5858
{%- endif -%}
5959
depends: function() {
60-
{%- if hasDepends and rule.name is sameas('required') %}
61-
var dep = form.find("[name=\"{{ rule.depends|e('js')|last }}\"]")[0];
62-
{% endif %}
63-
64-
return
65-
{%- if hasGroups %} (
66-
{%- for group in rule.groups -%}
67-
validator.settings.validation_groups["{{ group|e('js') }}"] {%- if not loop.last %} || {% endif -%}
60+
{%- if hasConditions %}
61+
{%- if hasGroups -%}
62+
if (!(
63+
{%- for group in rule.groups -%}
64+
validator.settings.validation_groups["{{ group|e('js') }}"] {%- if not loop.last %} || {% endif -%}
65+
{%- endfor -%}
66+
)) {
67+
return false;
68+
}
69+
{%- endif -%}
70+
71+
{%- for condition in rule.conditions -%}
72+
{{ attribute(macro_conditions, condition.macro, [condition, rule]) }}
6873
{%- endfor -%}
69-
)
70-
{%- endif -%}
71-
72-
{%- if hasGroups and hasDepends %} && {% endif -%}
7374

74-
{%- if hasDepends %}
75-
{% if rule.name is sameas('required') %}
76-
$.validator.methods.required.call(validator, validator.elementValue(dep), dep, true) &&
77-
{% endif %}
78-
79-
!(
80-
{%- for selector in rule.depends -%}
81-
"{{ selector|e('js') }}" in validator.errorMap ||
82-
"{{ selector|e('js') }}" in validator.invalid {%- if not loop.last %} || {% endif -%}
83-
{%- endfor -%}
84-
)
75+
return true;
76+
{% else %}
77+
return (
78+
{%- for group in rule.groups -%}
79+
validator.settings.validation_groups["{{ group|e('js') }}"] {%- if not loop.last %} || {% endif -%}
80+
{%- endfor -%}
81+
);
8582
{%- endif -%}
86-
;
8783
}
8884
}
8985
{%- else -%}

0 commit comments

Comments
 (0)