1
|
|
|
<?php |
2
|
|
|
namespace Boekkooi\Bundle\JqueryValidationBundle\Form; |
3
|
|
|
|
4
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Exception\InvalidArgumentException; |
5
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Util\FormHelper; |
6
|
|
|
use Symfony\Component\Form\FormView; |
7
|
|
|
use Symfony\Component\Validator\Constraint; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Warnar Boekkooi <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class FormRuleContextBuilder extends FormRuleContext |
13
|
|
|
{ |
14
|
|
|
public function __construct() |
15
|
|
|
{ |
16
|
|
|
parent::__construct(array(), array(), array()); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function get($view) |
20
|
|
|
{ |
21
|
|
|
$name = FormHelper::getFormName($view); |
22
|
|
|
|
23
|
|
|
return parent::get($name); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function getGroup($view) |
27
|
|
|
{ |
28
|
|
|
$name = FormHelper::getFormName($view); |
29
|
|
|
|
30
|
|
|
return parent::getGroup($name); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return FormRuleContext |
35
|
|
|
*/ |
36
|
|
|
public function getRuleContext() |
37
|
|
|
{ |
38
|
|
|
return new FormRuleContext($this->rules, $this->groups, $this->buttons); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Adds a rule collection. |
43
|
|
|
* |
44
|
|
|
* @param string|FormView $view The form full_name or view instance |
45
|
|
|
* @param RuleCollection $collection A RuleCollection instance |
46
|
|
|
*/ |
47
|
|
|
public function add($view, RuleCollection $collection) |
48
|
|
|
{ |
49
|
|
|
$name = FormHelper::getFormName($view); |
50
|
|
|
if (isset($this->rules[$name])) { |
51
|
|
|
$this->rules[$name]->addCollection($collection); |
52
|
|
|
} else { |
53
|
|
|
$this->rules[$name] = $collection; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Removes a rule or an array of rules by name from the collection |
59
|
|
|
* |
60
|
|
|
* @param string|FormView $form The form full_name or view instance |
61
|
|
|
*/ |
62
|
|
|
public function remove($form) |
63
|
|
|
{ |
64
|
|
|
$name = FormHelper::getFormName($form); |
65
|
|
|
unset($this->rules[$name]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function addButton(FormView $view, $groups = null) |
69
|
|
|
{ |
70
|
|
|
$name = FormHelper::getFormName($view); |
71
|
|
|
|
72
|
|
|
$this->addGroup($name, $groups); |
73
|
|
|
$this->buttons[] = $name; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function addGroup($view, $groups) |
77
|
|
|
{ |
78
|
|
|
$groups = $this->normalizeGroups($groups); |
79
|
|
|
|
80
|
|
|
$name = FormHelper::getFormName($view); |
81
|
|
|
$this->groups[$name] = $groups; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function normalizeGroups($groups) |
85
|
|
|
{ |
86
|
|
|
if ($groups === null) { |
87
|
|
|
return array(Constraint::DEFAULT_GROUP); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($this->isValidGroup($groups)) { |
91
|
|
|
return array($groups); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (!is_array($groups) && !$groups instanceof \Traversable) { |
95
|
|
|
throw new InvalidArgumentException('A group must be a string, int, callable or FALSE.'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
foreach ($groups as $group) { |
99
|
|
|
if ($this->isValidGroup($group)) { |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
throw new InvalidArgumentException('A group must be a string, int, callable or FALSE.'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $groups; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|