/
GroupForm.php
149 lines (131 loc) · 4.42 KB
/
GroupForm.php
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/
namespace Spryker\Zed\Acl\Communication\Form;
use Spryker\Zed\Gui\Communication\Form\Type\Select2ComboBoxType;
use Spryker\Zed\Kernel\Communication\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* @method \Spryker\Zed\Acl\Business\AclFacadeInterface getFacade()
* @method \Spryker\Zed\Acl\Communication\AclCommunicationFactory getFactory()
* @method \Spryker\Zed\Acl\Persistence\AclQueryContainerInterface getQueryContainer()
* @method \Spryker\Zed\Acl\AclConfig getConfig()
*/
class GroupForm extends AbstractType
{
public const FIELD_TITLE = 'title';
public const FIELD_ROLES = 'roles';
public const OPTION_ROLE_CHOICES = 'role_choices';
public const GROUP_UNIQUE_GROUP_CHECK = 'unique_group_check';
/**
* @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
*
* @return void
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired(self::OPTION_ROLE_CHOICES);
$resolver->setDefaults([
'validation_groups' => function (FormInterface $form) {
$defaultData = $form->getConfig()->getData();
$submittedData = $form->getData();
if (array_key_exists(self::FIELD_TITLE, $defaultData) === false ||
$defaultData[self::FIELD_TITLE] !== $submittedData[self::FIELD_TITLE]
) {
return [Constraint::DEFAULT_GROUP, self::GROUP_UNIQUE_GROUP_CHECK];
}
return [Constraint::DEFAULT_GROUP];
},
]);
}
/**
* @deprecated Use `configureOptions()` instead.
*
* @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
*
* @return void
*/
public function setDefaultOptions(OptionsResolver $resolver)
{
$this->configureOptions($resolver);
}
/**
* @param \Symfony\Component\Form\FormBuilderInterface $builder
* @param array $options
*
* @return void
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$this
->addTitleField($builder)
->addRolesField($builder, $options[self::OPTION_ROLE_CHOICES]);
}
/**
* @param \Symfony\Component\Form\FormBuilderInterface $builder
*
* @return $this
*/
protected function addTitleField(FormBuilderInterface $builder)
{
$builder->add(self::FIELD_TITLE, TextType::class, [
'label' => 'Title',
'constraints' => [
new NotBlank(),
new Callback([
'callback' => function ($name, ExecutionContextInterface $contextInterface) {
if ($this->getQueryContainer()->queryGroupByName($name)->count() > 0) {
$contextInterface->addViolation('Group name already in use');
}
},
'groups' => [self::GROUP_UNIQUE_GROUP_CHECK],
]),
],
]);
return $this;
}
/**
* @param \Symfony\Component\Form\FormBuilderInterface $builder
* @param array $choices
*
* @return $this
*/
protected function addRolesField(FormBuilderInterface $builder, array $choices)
{
$builder->add(self::FIELD_ROLES, Select2ComboBoxType::class, [
'label' => 'Assigned Roles',
'placeholder' => false,
'multiple' => true,
'choices' => $choices,
'constraints' => [
new NotBlank(),
],
]);
return $this;
}
/**
* @deprecated Use `getBlockPrefix()` instead.
*
* @return string
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* @return string
*/
public function getBlockPrefix()
{
return 'group';
}
}