src/Form/CadastroCandidatoType.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Usuario;
  4. use App\Service\CpfValidator;
  5. use App\Service\TelefoneValidator;
  6. use Symfony\Component\Form\AbstractType;
  7. use Symfony\Component\Form\Extension\Core\Type\CountryType;
  8. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  9. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  10. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  11. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  12. use Symfony\Component\Form\Extension\Core\Type\TextType;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\OptionsResolver\OptionsResolver;
  15. use Symfony\Component\Validator\Constraints\Callback;
  16. use Symfony\Component\Validator\Constraints\Email;
  17. use Symfony\Component\Validator\Constraints\EmailValidator;
  18. use Symfony\Component\Validator\Constraints\Length;
  19. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  20. class CadastroCandidatoType extends AbstractType
  21. {
  22.     public function buildForm(FormBuilderInterface $builder, array $options)
  23.     {
  24.         $repository $options['repository'];
  25.         $builder
  26.             ->add('nome'TextType::class, [
  27.                 'label' => 'Nome completo',
  28.             ])
  29.             ->add('nacionalidade'CountryType::class)
  30.             ->add('cpf'TextType::class, [
  31.                 'label' => 'CPF',
  32.                 'constraints' => [
  33.                     new Callback([
  34.                         'callback' => function($valueExecutionContextInterface $context) use ($repository){
  35.                             if (empty($value)) return;
  36.                             $cpf = new CpfValidator($valuetrue);
  37.                             if (!$cpf->isValid()) {
  38.                                 $context->buildViolation('Este não é um CPF válido!')
  39.                                     ->atPath('cpf')
  40.                                     ->addViolation();
  41.                             }
  42.                             $user $repository->findOneBy(['cpf' => $cpf]);
  43.                             if ($user) {
  44.                                 $context->buildViolation("O CPF {$cpf} já está em uso por outro candidato. Se é seu CPF, faça o login.")
  45.                                     ->atPath('cpf')
  46.                                     ->addViolation();
  47.                             }
  48.                             $context->getRoot()->getData()->setCpf($cpf->getString());
  49.                         }
  50.                     ]),
  51.                 ],
  52.             ])
  53.             ->add('passaporte'TextType::class, [
  54.                 'label' => 'Passaporte',
  55.                 'required' => false,
  56.                 'constraints' => [
  57.                     new Callback([
  58.                         'callback' => function($valueExecutionContextInterface $context) use ($repository){
  59.                             if (empty($value)) return;
  60.                             $nacionalidade $context->getRoot()->getData()->getNacionalidade();
  61.                             $passaporte trim(strtoupper($value));
  62.                             if (!preg_match('/^[A-Z0-9]{6,40}$/'$value)) {
  63.                                 $context->buildViolation("O Passaporte {$passaporte} ($nacionalidade) não é válido.")
  64.                                     ->atPath('passaporte')
  65.                                     ->addViolation();
  66.                             }
  67.                             $user $repository->findOneBy(['passaporte' => $passaporte'nacionalidade' => $nacionalidade]);
  68.                             if ($user) {
  69.                                 $context->buildViolation("O Passaporte {$passaporte} ($nacionalidade) já está em uso por outro candidato. Se é o seu passaporte, faça o login.")
  70.                                     ->atPath('passaporte')
  71.                                     ->addViolation();
  72.                             }
  73.                             $context->getRoot()->getData()->setPassaporte($passaporte);
  74.                         }
  75.                     ]),
  76.                 ],
  77.                 'help' => 'Estrangeiro: o seu Passaporte irá garantir o seu cadastro, mas você precisará apresentar um CPF válido para matrícula, no caso de aprovação.',
  78.             ])
  79.             ->add('telefone'TextType::class, [
  80.                 'constraints' => [
  81.                     new Callback([
  82.                         'callback' => function($valueExecutionContextInterface $context){
  83.                             //if (empty($value)) return;
  84.                             $nacionalidade $context->getRoot()->getData()->getNacionalidade();
  85.                             if ($nacionalidade == 'BR') {
  86.                                 $tel = new TelefoneValidator($valuenulltrue);
  87.                                 if (!$tel->isValid()) {
  88.                                     $context->buildViolation('Este não é um telefone válido!')
  89.                                         ->atPath('telefone')
  90.                                         ->addViolation();
  91.                                 }
  92.                                 $context->getRoot()->getData()->setTelefone($tel->getString());
  93.                             }
  94.                             else {
  95.                                 $tel preg_replace('/[^\d]/'''$value);
  96.                                 if (strlen($tel) < 8) {
  97.                                     $context->buildViolation('Este não parece ser telefone válido!')
  98.                                         ->atPath('telefone')
  99.                                         ->addViolation();
  100.                                 }
  101.                             }
  102.                         }
  103.                     ]),
  104.                 ],
  105.             ])
  106.             ->add('email'RepeatedType::class, [
  107.                 'type' => EmailType::class,
  108.                 'invalid_message' => 'Os emails devem ser iguais.',
  109.                 'required' => true,
  110.                 'first_options' => ['label' => 'E-mail''constraints' => [
  111.                     new Email(['mode' => Email::VALIDATION_MODE_LOOSE]),
  112.                     new Email(['mode' => Email::VALIDATION_MODE_STRICT])
  113.                 ]],
  114.                 'second_options' => ['label' => 'Repita o e-mail'],
  115.             ])
  116.             ->add('plainPassword'RepeatedType::class, [
  117.                 'type' => PasswordType::class,
  118.                 'invalid_message' => 'As senhas devem ser iguais.',
  119.                 'required' => true,
  120.                 'first_options' => ['label' => 'Senha''constraints' => [
  121.                     new Length([
  122.                         'min' => 8,
  123.                         'minMessage' => "Senha muito curta. Use pelo menos 8 caracteres entre minúsculas, maiúsculas, números e sinais. Por exemplo: ".Usuario::generatePassword(),
  124.                         'allowEmptyString' => false
  125.                     ])
  126.                 ]],
  127.                 'second_options' => ['label' => 'Repita a Senha']
  128.             ])
  129.             ->add('save'SubmitType::class, [
  130.                 'label' => 'Cadastrar',
  131.             ]);
  132.     }
  133.     public function configureOptions(OptionsResolver $resolver)
  134.     {
  135.         $resolver->setDefaults([
  136.             'data_class' => Usuario::class,
  137.             'repository' => null,
  138.         ]);
  139.     }
  140. }