src/Form/UserType.php line 90

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\User;
  4. use App\Entity\UserCategory;
  5. use App\String\Constant;
  6. use App\Utility\DropdownValues;
  7. use Doctrine\ORM\EntityRepository;
  8. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  9. use Symfony\Component\Form\AbstractType;
  10. use Symfony\Component\Form\ChoiceList\ChoiceList;
  11. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  12. use Symfony\Component\Form\Extension\Core\Type\CountryType;
  13. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  14. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  15. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  16. use Symfony\Component\Form\Extension\Core\Type\TelType;
  17. use Symfony\Component\Form\Extension\Core\Type\TextType;
  18. use Symfony\Component\Form\FormBuilderInterface;
  19. use Symfony\Component\OptionsResolver\OptionsResolver;
  20. class UserType extends AbstractType
  21. {
  22.     public const HONEYPOT_EMAIL_NAME 'emailAddress';
  23.     public const HONEYPOT_FULLNAME_NAME 'fullName';
  24.     public function buildForm(FormBuilderInterface $builder, array $options)
  25.     {
  26.         $builder 
  27.             ->add(self::HONEYPOT_EMAIL_NAMETextType::class, [
  28.                 'required' => false,
  29.                 'attr' => [
  30.                     'autocomplete' => 'nope',
  31.                     'autofill' => false,
  32.                 ]
  33.             ]) // honeypot email
  34.             ->add(self::HONEYPOT_FULLNAME_NAMETextType::class, [
  35.                 'required' => false,
  36.                 'attr' => [
  37.                     'autocomplete' => 'nope',
  38.                     'autofill' => false,
  39.                 ]
  40.             ]) // honeypot full name
  41.             ->add('roles'ChoiceType::class, [
  42.                 'label_html' => true,
  43.                 'choices' => [
  44.                     'Donor/Funder' => Constant::ROLE_DONOR,
  45.                     'Filmmaker' => Constant::ROLE_FILMMAKER,
  46.                     'Impact Partner' => Constant::ROLE_SPONSOR,
  47.                     'Documentary Lover' => Constant::ROLE_DOCUMENTARY_LOVER,
  48.                 ],
  49.                 'expanded' => true,
  50.                 'multiple' => true,
  51.                 'attr' => [
  52.                     'class' => 'col-3'
  53.                 ],
  54.                 'label_attr' => [
  55.                     'class' => 'user_roles-tile',
  56.                 ],
  57.                 'choice_label' => function ($choice$key$value) {
  58.                     switch($choice) {
  59.                         case Constant::ROLE_DONOR:  return '<i class="icons donor"></i> Donor/Funder';
  60.                         case Constant::ROLE_FILMMAKER:  return '<i class="icons filmmaker"></i> Filmmaker';
  61.                         case Constant::ROLE_SPONSOR:  return '<i class="icons sponsor"></i> Impact Partner';
  62.                         case Constant::ROLE_DOCUMENTARY_LOVER:  return '<i class="icons sponsor"></i> Documentary Lover';
  63.                     }
  64.                     return strtoupper($key);  
  65.                 },
  66.             ])
  67.             ->add('email'EmailType::class, [
  68.                 'required'   => true
  69.             ])
  70.             ->add('firstName'TextType::class, [
  71.                 'required'   => true
  72.             ])
  73.             ->add('lastName'TextType::class, [
  74.                 'required'   => true
  75.             ])
  76.             ->add('phone'TelType::class, [
  77.                 'required'   => true
  78.             ])
  79.             ->add('organisation'TextType::class, [
  80.                 'required'   => false
  81.             ])
  82.             ->add('categories',  EntityType::class, [
  83.                 'label_html' => true,
  84.                 'multiple' => true,
  85.                 'class' => UserCategory::class,
  86.                 'query_builder' => function (EntityRepository $er) {
  87.                     return $er->createQueryBuilder('u')
  88.                         ->orderBy('u.name''ASC');
  89.                 },
  90.                 'choice_attr' => ChoiceList::attr($this, function (?UserCategory $category) {
  91.                     return $category ? ['data-roles' => implode(','$category->getRoles())] : [];
  92.                 }),
  93.             ])
  94.             ->add('userRoles'ChoiceType::class, [
  95.                 'label_html' => true,
  96.                 'choices' => DropdownValues::USER_ROLE_VALUES,
  97.                 'expanded' => false,
  98.                 'multiple' => true,
  99.                 'property_path'=>'userProfile.userRoles'
  100.             ])
  101.             ->add('areaInterest'ChoiceType::class, [
  102.                 'label_html' => true,
  103.                 'choices' => DropdownValues::IMPACT_VALUES,
  104.                 'expanded' => true,
  105.                 'multiple' => true,
  106.             ])
  107.             ->add('street'TextType::class, [
  108.                 'property_path'=>'userAddress.street'
  109.             ])
  110.             ->add('city'TextType::class, [
  111.                 'property_path'=>'userAddress.city'
  112.             ])
  113.             ->add('state'ChoiceType::class, [
  114.                 'property_path'=>'userAddress.state',
  115.                 'choices' => [
  116.                     'ACT' => 'ACT',
  117.                     'NSW' => 'NSW',
  118.                     'NT' => 'NT',
  119.                     'QLD' => 'QLD',
  120.                     'SA' => 'SA',
  121.                     'TAS' => 'TAS',
  122.                     'VIC' => 'VIC',
  123.                     'WA' => 'WA',
  124.                     'Other State' => 'Other State',
  125.                 ],
  126.                 'placeholder' => 'Select your state',
  127.                 'empty_data' => null,
  128.                 'required'   => true,
  129.             ])
  130.             ->add('postcode'TextType::class, [
  131.                 'property_path'=>'userAddress.postcode'
  132.             ])
  133.             ->add('country'CountryType::class, [
  134.                 'property_path'=>'userAddress.country',
  135.                 'empty_data' => null,
  136.                 'required'   => true,
  137.                 'placeholder' => 'Select your country'
  138.             ])
  139.             ->add('gender'ChoiceType::class, [
  140.                 'empty_data' => null,
  141.                 'choices' => [
  142.                     'Select your gender' => null,
  143.                     'Male' => 'Male',
  144.                     'Female' => 'Female',
  145.                     'Non-binary' => 'Non-binary',
  146.                     'Prefer not to say' => 'Prefer not to say',
  147.                 ]
  148.             ])
  149.             ->add('isAborigin'ChoiceType::class, [
  150.                 'choices' => [
  151.                     'No' =>  'No',
  152.                     'Yes' => 'Yes',
  153.                     'Prefer not to say' =>  'Prefer not to say',
  154.                 ],
  155.                 'empty_data' => '',
  156.                 'required' => false
  157.             ])
  158.             ->add('isDiverse'ChoiceType::class, [
  159.                 'choices' => [
  160.                     'No' =>  'No',
  161.                     'Yes' => 'Yes',
  162.                     'Prefer not to say' =>  'Prefer not to say',
  163.                 ],
  164.                 'empty_data' => '',
  165.                 'required' => false
  166.             ])
  167.             ->add('password'RepeatedType::class, [
  168.                 'type' => PasswordType::class,
  169.                 'first_options' => ['label' => 'Password'],
  170.                 'second_options' => ['label' => 'Confirm Password']
  171.             ])
  172.         ;
  173.     }
  174.     public function configureOptions(OptionsResolver $resolver)
  175.     {
  176.         $resolver->setDefaults([
  177.             'csrf_protection' => false,
  178.             'data_class' => User::class,
  179.         ]);
  180.     }
  181. }