1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys UserBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\UserBundle\Form
;
14 use Rapsys\PackBundle\Form\CaptchaType
;
16 use Rapsys\UserBundle\Entity\Civility
;
18 use Symfony\Bridge\Doctrine\Form\Type\EntityType
;
19 use Symfony\Component\Form\Extension\Core\Type\CheckboxType
;
20 use Symfony\Component\Form\Extension\Core\Type\EmailType
;
21 use Symfony\Component\Form\Extension\Core\Type\PasswordType
;
22 use Symfony\Component\Form\Extension\Core\Type\RepeatedType
;
23 use Symfony\Component\Form\Extension\Core\Type\SubmitType
;
24 use Symfony\Component\Form\Extension\Core\Type\TextType
;
25 use Symfony\Component\Form\FormBuilderInterface
;
26 use Symfony\Component\OptionsResolver\OptionsResolver
;
27 use Symfony\Component\Validator\Constraints\Email
;
28 use Symfony\Component\Validator\Constraints\NotBlank
;
33 class RegisterType
extends CaptchaType
{
37 public function buildForm(FormBuilderInterface
$builder, array $options): void {
38 //Add extra mail field
39 if (!empty($options['mail'])) {
40 $builder->add('mail', EmailType
::class, ['attr' => ['placeholder' => 'Your mail'], 'constraints' => [new NotBlank(['message' => 'Please provide your mail']), new Email(['message' => 'Your mail doesn\'t seems to be valid'])], 'required' => true]);
43 //Add extra password field
44 if (!empty($options['password'])) {
45 //Add password repeated field
46 if (!empty($options['password_repeated'])) {
47 $builder->add('password', RepeatedType
::class, ['type' => PasswordType
::class, 'invalid_message' => 'The password and confirmation must match', 'first_options' => ['attr' => ['placeholder' => 'Your password'], 'label' => 'Password'], 'second_options' => ['attr' => ['placeholder' => 'Your password confirmation'], 'label' => 'Confirm password'], 'options' => ['constraints' => [new NotBlank(['message' => 'Please provide your password'])]], 'required' => true]);
50 $builder->add('password', PasswordType
::class, ['attr' => ['placeholder' => 'Your password'], 'constraints' => [new NotBlank(['message' => 'Please provide your password'])], 'required' => true]);
54 //Add extra civility field
55 if (!empty($options['civility'])) {
56 $builder->add('civility', EntityType
::class, ['class' => $options['civility_class'], 'attr' => ['placeholder' => 'Your civility'], 'choice_translation_domain' => true, 'empty_data' => $options['civility_default'], 'required' => true]);
59 //Add extra forename field
60 if (!empty($options['forename'])) {
61 $builder->add('forename', TextType
::class, ['attr' => ['placeholder' => 'Your forename']]);
64 //Add extra surname field
65 if (!empty($options['surname'])) {
66 $builder->add('surname', TextType
::class, ['attr' => ['placeholder' => 'Your surname']]);
69 //Add extra active field
70 if (!empty($options['active'])) {
71 $builder->add('active', CheckboxType
::class, ['attr' => ['placeholder' => 'Your active']]);
74 //Add extra enable field
75 if (!empty($options['enable'])) {
76 $builder->add('enable', CheckboxType
::class, ['attr' => ['placeholder' => 'Your enable']]);
80 $builder->add('submit', SubmitType
::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
83 parent
::buildForm($builder, $options);
89 public function configureOptions(OptionsResolver
$resolver): void {
90 //Call parent configure options
91 parent
::configureOptions($resolver);
94 $resolver->setDefaults(['error_bubbling' => true, 'civility' => true, 'civility_class' => 'Rapsys\UserBundle\Entity\Civility', 'civility_default' => null, 'mail' => true, 'password' => true, 'password_repeated' => true, 'forename' => true, 'surname' => true, 'active' => false, 'enable' => false]);
96 //Add extra civility option
97 $resolver->setAllowedTypes('civility', 'boolean');
100 $resolver->setAllowedTypes('civility_class', 'string');
102 //Add civility default
103 //XXX: trigger strange error about table not existing is not specified in form create
104 $resolver->setAllowedTypes('civility_default', [Civility
::class, 'null']);
106 //Add extra mail option
107 $resolver->setAllowedTypes('mail', 'boolean');
109 //Add extra password option
110 $resolver->setAllowedTypes('password', 'boolean');
112 //Add extra password repeated option
113 $resolver->setAllowedTypes('password_repeated', 'boolean');
115 //Add extra forename option
116 $resolver->setAllowedTypes('forename', 'boolean');
118 //Add extra surname option
119 $resolver->setAllowedTypes('surname', 'boolean');
121 //Add extra active option
122 $resolver->setAllowedTypes('active', 'boolean');
124 //Add extra enable option
125 $resolver->setAllowedTypes('enable', 'boolean');
131 public function getName(): string {
132 return 'rapsysuser_register';