-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRequiredFieldFormExtension.php
More file actions
56 lines (46 loc) · 1.97 KB
/
Copy pathRequiredFieldFormExtension.php
File metadata and controls
56 lines (46 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
declare(strict_types=1);
namespace Netgen\Bundle\SiteBundle\InfoCollection\Form\Extension;
use Ibexa\Contracts\ContentForms\Data\Content\FieldData;
use Netgen\Bundle\InformationCollectionBundle\Ibexa\ContentForms\InformationCollectionFieldType;
use Netgen\Bundle\InformationCollectionBundle\Ibexa\ContentForms\InformationCollectionType;
use Netgen\Bundle\SiteBundle\InfoCollection\Validator\Constraints\RequiredField;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
final class RequiredFieldFormExtension extends AbstractTypeExtension
{
public static function getExtendedTypes(): iterable
{
return [InformationCollectionType::class];
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'translation_domain' => 'validators',
'attr' => [
'class' => 'embed-form js-form-embed',
],
]);
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
foreach ($builder->getData()->getCollectedFields() as $identifier => $fieldData) {
/** @var FieldData $fieldData */
if ($fieldData->fieldDefinition->isRequired === true) {
$fieldOptions = $builder->get($identifier)->getOptions();
$hasRequiredFieldConstraint = false;
foreach ($fieldOptions['constraints'] as $constraint) {
if ($constraint instanceof RequiredField) {
$hasRequiredFieldConstraint = true;
break;
}
}
if (!$hasRequiredFieldConstraint) {
$fieldOptions['constraints'][] = new RequiredField();
$builder->add($identifier, InformationCollectionFieldType::class, $fieldOptions);
}
}
}
}
}