|
15 | 15 | use Symfony\Component\Form\Form;
|
16 | 16 | use Symfony\Component\Form\Forms;
|
17 | 17 | use Symfony\Component\Form\Test\Traits\ValidatorExtensionTrait;
|
| 18 | +use Symfony\Component\Form\Tests\Extension\Core\Type\CollectionTypeTest; |
18 | 19 | use Symfony\Component\Form\Tests\Extension\Core\Type\FormTypeTest;
|
19 | 20 | use Symfony\Component\Form\Tests\Extension\Core\Type\TextTypeTest;
|
20 | 21 | use Symfony\Component\Form\Tests\Fixtures\Author;
|
| 22 | +use Symfony\Component\Form\Tests\Fixtures\AuthorType; |
| 23 | +use Symfony\Component\Form\Tests\Fixtures\Organization; |
21 | 24 | use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
|
22 | 25 | use Symfony\Component\Validator\Constraints\GroupSequence;
|
23 | 26 | use Symfony\Component\Validator\Constraints\Length;
|
@@ -158,4 +161,185 @@ protected function createForm(array $options = [])
|
158 | 161 | {
|
159 | 162 | return $this->factory->create(FormTypeTest::TESTED_TYPE, null, $options);
|
160 | 163 | }
|
| 164 | + |
| 165 | + public function testCollectionTypeKeepAsListOptionFalse() |
| 166 | + { |
| 167 | + $formMetadata = new ClassMetadata(Form::class); |
| 168 | + $authorMetadata = (new ClassMetadata(Author::class)) |
| 169 | + ->addPropertyConstraint('firstName', new NotBlank()); |
| 170 | + $organizationMetadata = (new ClassMetadata(Organization::class)) |
| 171 | + ->addPropertyConstraint('authors', new Valid()); |
| 172 | + $metadataFactory = $this->createMock(MetadataFactoryInterface::class); |
| 173 | + $metadataFactory->expects($this->any()) |
| 174 | + ->method('getMetadataFor') |
| 175 | + ->willReturnCallback(static function ($classOrObject) use ($formMetadata, $authorMetadata, $organizationMetadata) { |
| 176 | + if (Author::class === $classOrObject || $classOrObject instanceof Author) { |
| 177 | + return $authorMetadata; |
| 178 | + } |
| 179 | + |
| 180 | + if (Organization::class === $classOrObject || $classOrObject instanceof Organization) { |
| 181 | + return $organizationMetadata; |
| 182 | + } |
| 183 | + |
| 184 | + if (Form::class === $classOrObject || $classOrObject instanceof Form) { |
| 185 | + return $formMetadata; |
| 186 | + } |
| 187 | + |
| 188 | + return new ClassMetadata(\is_string($classOrObject) ? $classOrObject : $classOrObject::class); |
| 189 | + }); |
| 190 | + |
| 191 | + $validator = Validation::createValidatorBuilder() |
| 192 | + ->setMetadataFactory($metadataFactory) |
| 193 | + ->getValidator(); |
| 194 | + |
| 195 | + $form = Forms::createFormFactoryBuilder() |
| 196 | + ->addExtension(new ValidatorExtension($validator)) |
| 197 | + ->getFormFactory() |
| 198 | + ->create(FormTypeTest::TESTED_TYPE, new Organization([]), [ |
| 199 | + 'data_class' => Organization::class, |
| 200 | + 'by_reference' => false, |
| 201 | + ]) |
| 202 | + ->add('authors', CollectionTypeTest::TESTED_TYPE, [ |
| 203 | + 'entry_type' => AuthorType::class, |
| 204 | + 'allow_add' => true, |
| 205 | + 'allow_delete' => true, |
| 206 | + 'keep_as_list' => false, |
| 207 | + ]) |
| 208 | + ; |
| 209 | + |
| 210 | + $form->submit([ |
| 211 | + 'authors' => [ |
| 212 | + 0 => [ |
| 213 | + 'firstName' => '', // Fires a Not Blank Error |
| 214 | + 'lastName' => 'lastName1', |
| 215 | + ], |
| 216 | + // key "1" could be missing if we add 4 blank form entries and then remove it. |
| 217 | + 2 => [ |
| 218 | + 'firstName' => '', // Fires a Not Blank Error |
| 219 | + 'lastName' => 'lastName3', |
| 220 | + ], |
| 221 | + 3 => [ |
| 222 | + 'firstName' => '', // Fires a Not Blank Error |
| 223 | + 'lastName' => 'lastName3', |
| 224 | + ], |
| 225 | + ], |
| 226 | + ]); |
| 227 | + |
| 228 | + // Form does have 3 not blank errors |
| 229 | + $errors = $form->getErrors(true); |
| 230 | + $this->assertCount(3, $errors); |
| 231 | + |
| 232 | + // Form behaves as expected. It has index 0, 2 and 3 (1 has been removed) |
| 233 | + // But errors property paths mismatch happening with "keep_as_list" option set to false |
| 234 | + $errorPaths = [ |
| 235 | + $errors[0]->getCause()->getPropertyPath(), |
| 236 | + $errors[1]->getCause()->getPropertyPath(), |
| 237 | + $errors[2]->getCause()->getPropertyPath(), |
| 238 | + ]; |
| 239 | + |
| 240 | + $this->assertTrue($form->get('authors')->has('0')); |
| 241 | + $this->assertContains('data.authors[0].firstName', $errorPaths); |
| 242 | + |
| 243 | + $this->assertFalse($form->get('authors')->has('1')); |
| 244 | + $this->assertContains('data.authors[1].firstName', $errorPaths); |
| 245 | + |
| 246 | + $this->assertTrue($form->get('authors')->has('2')); |
| 247 | + $this->assertContains('data.authors[2].firstName', $errorPaths); |
| 248 | + |
| 249 | + $this->assertTrue($form->get('authors')->has('3')); |
| 250 | + $this->assertNotContains('data.authors[3].firstName', $errorPaths); |
| 251 | + |
| 252 | + // As result, root form contain errors |
| 253 | + $this->assertCount(1, $form->getErrors(false)); |
| 254 | + } |
| 255 | + |
| 256 | + public function testCollectionTypeKeepAsListOptionTrue() |
| 257 | + { |
| 258 | + $formMetadata = new ClassMetadata(Form::class); |
| 259 | + $authorMetadata = (new ClassMetadata(Author::class)) |
| 260 | + ->addPropertyConstraint('firstName', new NotBlank()); |
| 261 | + $organizationMetadata = (new ClassMetadata(Organization::class)) |
| 262 | + ->addPropertyConstraint('authors', new Valid()); |
| 263 | + $metadataFactory = $this->createMock(MetadataFactoryInterface::class); |
| 264 | + $metadataFactory->expects($this->any()) |
| 265 | + ->method('getMetadataFor') |
| 266 | + ->willReturnCallback(static function ($classOrObject) use ($formMetadata, $authorMetadata, $organizationMetadata) { |
| 267 | + if (Author::class === $classOrObject || $classOrObject instanceof Author) { |
| 268 | + return $authorMetadata; |
| 269 | + } |
| 270 | + |
| 271 | + if (Organization::class === $classOrObject || $classOrObject instanceof Organization) { |
| 272 | + return $organizationMetadata; |
| 273 | + } |
| 274 | + |
| 275 | + if (Form::class === $classOrObject || $classOrObject instanceof Form) { |
| 276 | + return $formMetadata; |
| 277 | + } |
| 278 | + |
| 279 | + return new ClassMetadata(\is_string($classOrObject) ? $classOrObject : $classOrObject::class); |
| 280 | + }); |
| 281 | + |
| 282 | + $validator = Validation::createValidatorBuilder() |
| 283 | + ->setMetadataFactory($metadataFactory) |
| 284 | + ->getValidator(); |
| 285 | + |
| 286 | + $form = Forms::createFormFactoryBuilder() |
| 287 | + ->addExtension(new ValidatorExtension($validator)) |
| 288 | + ->getFormFactory() |
| 289 | + ->create(FormTypeTest::TESTED_TYPE, new Organization([]), [ |
| 290 | + 'data_class' => Organization::class, |
| 291 | + 'by_reference' => false, |
| 292 | + ]) |
| 293 | + ->add('authors', CollectionTypeTest::TESTED_TYPE, [ |
| 294 | + 'entry_type' => AuthorType::class, |
| 295 | + 'allow_add' => true, |
| 296 | + 'allow_delete' => true, |
| 297 | + 'keep_as_list' => true, |
| 298 | + ]) |
| 299 | + ; |
| 300 | + |
| 301 | + $form->submit([ |
| 302 | + 'authors' => [ |
| 303 | + 0 => [ |
| 304 | + 'firstName' => '', // Fires a Not Blank Error |
| 305 | + 'lastName' => 'lastName1', |
| 306 | + ], |
| 307 | + // key "1" could be missing if we add 4 blank form entries and then remove it. |
| 308 | + 2 => [ |
| 309 | + 'firstName' => '', // Fires a Not Blank Error |
| 310 | + 'lastName' => 'lastName3', |
| 311 | + ], |
| 312 | + 3 => [ |
| 313 | + 'firstName' => '', // Fires a Not Blank Error |
| 314 | + 'lastName' => 'lastName3', |
| 315 | + ], |
| 316 | + ], |
| 317 | + ]); |
| 318 | + |
| 319 | + // Form does have 3 not blank errors |
| 320 | + $errors = $form->getErrors(true); |
| 321 | + $this->assertCount(3, $errors); |
| 322 | + |
| 323 | + // No property paths mismatch happening with "keep_as_list" option set to true |
| 324 | + $errorPaths = [ |
| 325 | + $errors[0]->getCause()->getPropertyPath(), |
| 326 | + $errors[1]->getCause()->getPropertyPath(), |
| 327 | + $errors[2]->getCause()->getPropertyPath(), |
| 328 | + ]; |
| 329 | + |
| 330 | + $this->assertTrue($form->get('authors')->has('0')); |
| 331 | + $this->assertContains('data.authors[0].firstName', $errorPaths); |
| 332 | + |
| 333 | + $this->assertTrue($form->get('authors')->has('1')); |
| 334 | + $this->assertContains('data.authors[1].firstName', $errorPaths); |
| 335 | + |
| 336 | + $this->assertTrue($form->get('authors')->has('2')); |
| 337 | + $this->assertContains('data.authors[2].firstName', $errorPaths); |
| 338 | + |
| 339 | + $this->assertFalse($form->get('authors')->has('3')); |
| 340 | + $this->assertNotContains('data.authors[3].firstName', $errorPaths); |
| 341 | + |
| 342 | + // Root form does NOT contain errors |
| 343 | + $this->assertCount(0, $form->getErrors(false)); |
| 344 | + } |
161 | 345 | }
|
0 commit comments