Skip to content

Commit 2e4d9e3

Browse files
Merge pull request #15 from LinioIT/feature/validator-builder
Added validator builder.
2 parents 5ca5687 + 21ce3cf commit 2e4d9e3

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

Form/FormFactory.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Symfony\Component\Form\FormBuilderInterface;
1111
use Symfony\Component\Form\FormFactory as SymfonyFormFactory;
1212
use Symfony\Component\Form\FormInterface;
13+
use Symfony\Component\Validator\Validation;
1314

1415
class FormFactory
1516
{
@@ -170,6 +171,42 @@ public function createBuilder($key, $data = [], $options = [], $name = null)
170171
return $formBuilder;
171172
}
172173

174+
/**
175+
* This method generates a validator based on the configuration file.
176+
*
177+
* @param string $key The key of the Form in the form configuration
178+
* @param mixed $object Object which is the target of the validator
179+
*
180+
* @return ValidatorInterface
181+
*
182+
* @throws NonExistentFormException
183+
*/
184+
public function createValidator($key, $object)
185+
{
186+
if (!isset($this->configuration[$key])) {
187+
throw new NonExistentFormException(sprintf('The form "%s" was not found.', $key));
188+
}
189+
190+
$validator = Validation::createValidatorBuilder()->getValidator();
191+
$metadata = $validator->getMetadataFor(get_class($object));
192+
193+
foreach ($this->configuration[$key] as $key => $fieldConfiguration) {
194+
if (!$fieldConfiguration['enabled']) {
195+
continue;
196+
}
197+
198+
if (!isset($fieldConfiguration['validation'])) {
199+
continue;
200+
}
201+
202+
foreach ($fieldConfiguration['validation'] as $validatorName => $options) {
203+
$metadata->addPropertyConstraint($key, new $validatorName($options));
204+
}
205+
}
206+
207+
return $validator;
208+
}
209+
173210
/**
174211
* @param string $alias
175212
*

Tests/Form/FormFactoryTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,33 @@ public function testIsHandlingNotExistenFormException()
274274
$this->formFactory->getConfiguration('bar');
275275
}
276276

277+
public function testIsCreatingValidator()
278+
{
279+
$formConfiguration = [
280+
'foo' => [
281+
'field1' => [
282+
'enabled' => true,
283+
'type' => 'field1_type',
284+
'validation' => [
285+
'Symfony\Component\Validator\Constraints\IsTrue' => [
286+
'message' => 'The token is invalid.',
287+
],
288+
],
289+
],
290+
],
291+
];
292+
293+
$expectedFieldOptions = [
294+
'constraints' => [
295+
new IsTrue(['message' => 'The token is invalid.']),
296+
],
297+
];
298+
299+
$this->formFactory->setConfiguration($formConfiguration);
300+
$validator = $this->formFactory->createValidator('foo', new MockValidatedClass());
301+
$this->assertEmpty($validator->validate(new MockValidatedClass()));
302+
}
303+
277304
public function setup()
278305
{
279306
$this->formBuilderMock = $this->prophesize('Symfony\Component\Form\FormBuilder');
@@ -301,3 +328,8 @@ public function reverseTransform($value)
301328
{
302329
}
303330
}
331+
332+
class MockValidatedClass
333+
{
334+
public $field1 = true;
335+
}

0 commit comments

Comments
 (0)