|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/** |
|
5
|
|
|
* Created by PhpStorm. |
|
6
|
|
|
* User: Valery Maslov |
|
7
|
|
|
* Date: 24.08.2018 |
|
8
|
|
|
* Time: 10:07. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace App\Form\Type; |
|
12
|
|
|
|
|
13
|
|
|
use App\Entity\User; |
|
14
|
|
|
use Symfony\Component\Form\AbstractType; |
|
15
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
|
16
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
|
17
|
|
|
use Symfony\Component\Form\Extension\Core\Type\PasswordType; |
|
|
|
|
|
|
18
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
19
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
20
|
|
|
|
|
21
|
|
|
final class UserType extends AbstractType |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options): void |
|
27
|
|
|
{ |
|
28
|
|
|
$builder |
|
29
|
|
|
->add( |
|
30
|
|
|
'roles', ChoiceType::class, [ |
|
31
|
|
|
'choices' => [ |
|
32
|
|
|
'label.roles.admin' => 'ROLE_ADMIN', |
|
33
|
|
|
], |
|
34
|
|
|
'expanded' => true, |
|
35
|
|
|
'multiple' => true, |
|
36
|
|
|
'label' => false, |
|
37
|
|
|
'label_attr' => ['class' => 'switch-custom'], |
|
38
|
|
|
] |
|
39
|
|
|
) |
|
40
|
|
|
->add('username', null, [ |
|
41
|
|
|
'label' => 'label.username', |
|
42
|
|
|
]) |
|
43
|
|
|
->add('profile', ProfileType::class) |
|
44
|
|
|
->add('email', null, [ |
|
45
|
|
|
'label' => 'label.email', |
|
46
|
|
|
]) |
|
47
|
|
|
->add( |
|
48
|
|
|
'email_verified', CheckboxType::class, [ |
|
49
|
|
|
'label' => 'label.email_verified', |
|
50
|
|
|
'label_attr' => ['class' => 'switch-custom'], |
|
51
|
|
|
'mapped' => false, |
|
52
|
|
|
'required' => false, |
|
53
|
|
|
'data' => null !== $options['data']->getEmailVerifiedAt(), |
|
54
|
|
|
] |
|
55
|
|
|
) |
|
56
|
|
|
->add('password', PasswordType::class, [ |
|
57
|
|
|
'label' => 'label.password', |
|
58
|
|
|
]); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function configureOptions(OptionsResolver $resolver): void |
|
65
|
|
|
{ |
|
66
|
|
|
$resolver->setDefaults([ |
|
67
|
|
|
'data_class' => User::class, |
|
68
|
|
|
]); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: