-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Closed
Labels
Description
The trivial (as I thinked before yesterday) problem: there is have some data in array $data = array('exist_field' => '')
wich should be validated. If something wrong - return report like: array('field1' => 'error1', 'field2' => 'error2');
Variant one:
<?php
$constraintsCollection = new Collection(array(
'fields' => array(
'exist_field' => new NotBlank(),
'not_exist_field' => new NotBlank(),
),
));
$result = $validator->validateValue($data, $constraintsCollection);
What I get
Array.[exist_field]:
This value should not be blank
Array.:
The fields "not_exist_field" are missing
so now I should by hand extract "not_exist_field" using $violation->getMessageParameters()['{{ field }}']
. Not nice.
Variant two:
<?php
$constraintsCollection = new Collection(array(
'fields' => array(
'exist_field' => new NotBlank(),
'not_exist_field' => new NotBlank(),
),
'allowMissingFields' => true
));
$validator->validateValue($data, $constraintsCollection);
What I get
Array.[exist_field]: This value should not be blank
Not enought.
Variant three:
<?php
$form = $this->createFormBuilder(null, array(
'validation_constraint' => $constraintsCollection, // $constraintsCollection from variant one or two - no difference
'error_bubbling' => true,
))
->add('exist_field', 'text')
->add('not_exist_field', 'text')
->getForm();
$form->bind($data);
vd($form->isValid(), $form->getData(), $form->getErrors()); // false, array('exist_field' => null, 'not_exist_field' => null), array()
So form is not valid, but no any message about incorrects fields.
Variant four:
<?php
$form = $this->createFormBuilder(null, array(
'error_bubbling' => true,
))
->add('exist_field', 'text', array('validation_constraint' => new NotBlank()))
->add('not_exist_field', 'text', array('validation_constraint' => new NotBlank()))
->getForm();
$form->bind($data);
vd($form->isValid(), $form->getData(), $form->getErrors()); // true, array('exist_field' => null, 'not_exist_field' => null), array()
Form is valid? o_O
IMHO the easiest way to fix it - add to Collection
constraint option replaceMissingFieldsWithNull
.