src/Form/SetPasswordFormType.php line 23

Open in your IDE?
  1. <?php
  2. /**
  3.  * @file
  4.  *   Class for updating passwords.
  5.  */
  6. namespace App\Form;
  7. use stdClass;
  8. use Symfony\Component\Form\AbstractType;
  9. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  10. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  11. use Symfony\Component\Form\Extension\Core\Type\TextType;
  12. use Symfony\Component\Form\FormBuilderInterface;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. use Symfony\Component\Validator\Constraints\Length;
  15. use Symfony\Component\Validator\Constraints\Regex;
  16. /**
  17.  * Password update form
  18.  */
  19. class SetPasswordFormType extends AbstractType
  20. {
  21.     /**
  22.      * {@Inheritdoc}
  23.      */
  24.     public function buildForm(FormBuilderInterface $builder, array $options)
  25.     {
  26.         $builder
  27.             ->add('pass'PasswordType::class, [
  28.                 'label' => 'New Password',
  29.                 'constraints' => $this->getPasswordConstraints($options['password_constraints']),
  30.             ])
  31.             ->add('confirm_pass'PasswordType::class, [
  32.                 'label' => 'Confirm Password',
  33.                 'constraints' => $this->getPasswordConstraints($options['password_constraints']),
  34.             ])
  35.             ->add('verification_code'TextType::class)
  36.             ->add('submit'SubmitType::class, [
  37.                 'label' => "Set Password",
  38.             ]);
  39.     }
  40.     /**
  41.      * {@Inheritdoc}
  42.      */
  43.     public function configureOptions(OptionsResolver $resolver)
  44.     {
  45.         $resolver->setDefaults([
  46.             // Configure your form options here
  47.             'password_constraints' => null,
  48.         ]);
  49.     }
  50.     /**
  51.      * Create the password constraints array based on the provided object.
  52.      *
  53.      * @param object|null $constraints The constraints object.
  54.      *
  55.      * @return array
  56.      */
  57.     private function getPasswordConstraints(object $constraints null): array
  58.     {
  59.         $out = [];
  60.         if (!$constraints) {
  61.             // No constraints provided just return the empty array.
  62.             return [];
  63.         }
  64.         // Set minimum length.
  65.         $out[] = new Length([
  66.             'min' => $constraints->minLength,
  67.         ]);
  68.         if ($constraints->requireLowercase) {
  69.             $out[] = new Regex([
  70.                 'pattern' => '/[a-z]/',
  71.                 'message' => 'Passwords must contain at least one lowercase letter.',
  72.             ]);
  73.         }
  74.         if ($constraints->requireUppercase) {
  75.             $out[] = new Regex([
  76.                 'pattern' => '/[A-Z]/',
  77.                 'message' => 'Passwords must contain at least one uppercase letter.',
  78.             ]);
  79.         }
  80.         if ($constraints->requireNumber) {
  81.             $out[] = new Regex([
  82.                 'pattern' => '/[0-9]/',
  83.                 'message' => 'Passwords must contain at least one number.',
  84.             ]);
  85.         }
  86.         if ($constraints->requireSpecialChar) {
  87.             $out[] = new Regex([
  88.                 'pattern' => '/[!@#$%^&*()\.,\-_+=:;]/',
  89.                 'message' => 'Passwords must contain at least one special character.',
  90.             ]);
  91.         }
  92.         return $out;
  93.     }
  94. }