Skip to content

Commit e01afc4

Browse files
Merge pull request #4 from acseo/v1.0
Add validation for repeated fields
2 parents 428aaaf + b553f56 commit e01afc4

File tree

2 files changed

+73
-12
lines changed

2 files changed

+73
-12
lines changed

Service/AbstractFormJsValidation.php

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace ACSEO\Bundle\FormJsValidationBundle\Service;
44

5+
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
6+
57
abstract class AbstractFormJsValidation
68
{
7-
private $validator;
8-
private $translator;
9+
protected $validator;
10+
protected $translator;
911

1012
public function __construct($validator, $translator)
1113
{
@@ -20,36 +22,85 @@ public function addJsValidation($form, $validationGroup = "Default")
2022
$metadata = $this->validator->getMetadataFor($form->getConfig()->getDataClass());
2123
$constrainedProperties = $metadata->getConstrainedProperties();
2224

23-
2425
foreach ($form->all() as $field) {
2526
$name = $field->getConfig()->getName();
26-
$type = get_class($field->getConfig()->getType()->getInnerType());
27-
$options = $field->getConfig()->getOptions();
27+
$innerType = $field->getConfig()->getType()->getInnerType();
2828

2929
if (in_array($name, $constrainedProperties)) {
30+
$type = get_class($innerType);
31+
$options = $field->getConfig()->getOptions();
3032
$constraints = $metadata->getPropertyMetadata($name)[0]->getConstraints();
33+
34+
if (!isset($options["attr"])) {
35+
$options["attr"] = array();
36+
}
37+
3138
foreach ($constraints as $constraint) {
3239
if (in_array($validationGroup, $constraint->groups)) {
33-
if (!isset($options["attr"])) {
34-
$options["attr"] = array();
35-
}
3640
$constraintName = ((new \ReflectionClass($constraint))->getShortName());
3741
if (!isset($mapping[$constraintName])) {
3842
continue;
39-
//throw new \Exception("The constraint ". $constraintName." is not known");
4043
}
4144
$newAttrs = call_user_func_array($mapping[$constraintName], [$constraint, $this->translator]);
4245
$options["attr"] = array_merge($options["attr"], $newAttrs);
43-
4446
}
45-
4647
}
48+
if ($innerType instanceof RepeatedType) {
49+
$firstOptions = isset($options["first_options"]) ? $options["first_options"] : array();
50+
$options["first_options"] = $this->addRepeatedFieldJsValidation($mapping, $constraints, $field->get('first'), $firstOptions, $validationGroup);
51+
$secondOptions = isset($options["second_options"]) ? $options["second_options"] : array();
52+
$options["second_options"] = $this->addRepeatedFieldJsValidation($mapping, $constraints, $field->get('second'), $secondOptions, $validationGroup);
53+
}
54+
4755
$form->add($name, $type, $options);
4856
}
4957
}
5058

5159
return $form;
5260
}
5361

62+
private function addRepeatedFieldJsValidation($mapping, $constraints, $field, $options, $validationGroup = "Default")
63+
{
64+
$options = $field->getConfig()->getOptions();
65+
66+
if (!isset($options["attr"])) {
67+
$options["attr"] = array();
68+
}
69+
70+
foreach ($constraints as $constraint) {
71+
if (in_array($validationGroup, $constraint->groups)) {
72+
$constraintName = ((new \ReflectionClass($constraint))->getShortName());
73+
if (!isset($mapping[$constraintName])) {
74+
continue;
75+
}
76+
$newAttrs = call_user_func_array($mapping[$constraintName], [$constraint, $this->translator]);
77+
$options["attr"] = array_merge($options["attr"], $newAttrs);
78+
}
79+
}
80+
81+
// Add a constraint to the second field in order to control the equality with the first one
82+
if ($field->getConfig()->getName() == 'second') {
83+
$options["attr"] = $this->addEqualToConstraint($field, $options["attr"]);
84+
}
85+
86+
return $options;
87+
}
88+
89+
protected function getFieldId($field)
90+
{
91+
$completeNameArray = [$field->getConfig()->getName()];
92+
$parent = $field;
93+
while ($parent = $parent->getParent()) {
94+
$completeNameArray[] = $parent->getConfig()->getName();
95+
}
96+
97+
return implode('_', array_reverse($completeNameArray));
98+
}
99+
100+
protected function addEqualToConstraint($field, $attrOptions)
101+
{
102+
return $attrOptions;
103+
}
104+
54105
abstract protected function getMapping();
55106
}

Service/JqueryFormValidator.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected function getMapping()
2222
// String Constraints
2323
"Email" => function ($constraint, $translator) {
2424
return array(
25-
'data-rule-email' => $constraint->limit,
25+
'data-rule-email' => true,
2626
'data-msg-email' => $translator->trans($constraint->message),
2727
);
2828
},
@@ -79,4 +79,14 @@ protected function getMapping()
7979

8080
return $mapping;
8181
}
82+
83+
protected function addEqualToConstraint($field, $attrOptions)
84+
{
85+
$parentOptions = $field->getParent()->getConfig()->getOptions();
86+
87+
$attrOptions['data-rule-equalTo'] = '#'.$this->getFieldId($field->getParent()->get('first'));
88+
$attrOptions['data-msg-equalTo'] = $this->translator->trans(isset($parentOptions['invalid_message']) ? $parentOptions['invalid_message'] : 'Les deux champs doivent être identiques.');
89+
90+
return $attrOptions;
91+
}
8292
}

0 commit comments

Comments
 (0)