[UTIL][Form] Fix bug with repeated_password

This commit is contained in:
Diogo Peralta Cordeiro 2021-11-16 23:27:29 +00:00
parent 89d36a68e5
commit f07dce4604
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
1 changed files with 32 additions and 32 deletions

View File

@ -1,10 +1,9 @@
<?php <?php
declare(strict_types = 1); declare(strict_types=1);
namespace App\Util\Form; namespace App\Util\Form;
use function App\Core\I18n\_m;
use App\Entity\Actor; use App\Entity\Actor;
use App\Entity\Language; use App\Entity\Language;
use App\Util\Common; use App\Util\Common;
@ -13,39 +12,40 @@ use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType; use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotBlank;
use function App\Core\I18n\_m;
abstract class FormFields abstract class FormFields
{ {
public static function repeated_password(array $options = []): array public static function repeated_password(array $options = []): array
{ {
$constraints = $options['required'] $constraints = (array_key_exists('required', $options) && $options['required'] === false) ? []
? [ : [
new NotBlank(['message' => _m('Please enter a password')]), new NotBlank(['message' => _m('Please enter a password')]),
new Length(['min' => Common::config('password', 'min_length'), 'minMessage' => _m(['Your password should be at least # characters'], ['count' => Common::config('password', 'min_length')]), new Length(['min' => Common::config('password', 'min_length'), 'minMessage' => _m(['Your password should be at least # characters'], ['count' => Common::config('password', 'min_length')]),
'max' => Common::config('password', 'max_length'), 'maxMessage' => _m(['Your password should be at most # characters'], ['count' => Common::config('password', 'max_length')]), ]), 'max' => Common::config('password', 'max_length'), 'maxMessage' => _m(['Your password should be at most # characters'], ['count' => Common::config('password', 'max_length')]),]),
] : []; ];
return [ return [
'password', RepeatedType::class, 'password', RepeatedType::class,
[ [
'type' => PasswordType::class, 'type' => PasswordType::class,
'first_options' => [ 'first_options' => [
'label' => _m('Password'), 'label' => _m('Password'),
'label_attr' => ['class' => 'section-form-label'], 'label_attr' => ['class' => 'section-form-label'],
'attr' => ['placeholder' => _m('********'), 'required' => $options['required'] ?? true], 'attr' => ['placeholder' => _m('********'), 'required' => $options['required'] ?? true],
'constraints' => $constraints, 'constraints' => $constraints,
'help' => _m('Write a password with at least {min_length} characters, and a maximum of {max_length}.', ['min_length' => Common::config('password', 'min_length'), 'max_length' => Common::config('password', 'max_length')]), 'help' => _m('Write a password with at least {min_length} characters, and a maximum of {max_length}.', ['min_length' => Common::config('password', 'min_length'), 'max_length' => Common::config('password', 'max_length')]),
], ],
'second_options' => [ 'second_options' => [
'label' => _m('Repeat Password'), 'label' => _m('Repeat Password'),
'label_attr' => ['class' => 'section-form-label'], 'label_attr' => ['class' => 'section-form-label'],
'attr' => ['placeholder' => _m('********')], 'attr' => ['placeholder' => _m('********')],
'help' => _m('Confirm your password.'), 'help' => _m('Confirm your password.'),
'required' => $options['required'] ?? true, 'required' => $options['required'] ?? true,
'constraints' => $constraints, 'constraints' => $constraints,
], ],
'mapped' => false, 'mapped' => false,
'required' => $options['required'] ?? true, 'required' => $options['required'] ?? true,
'invalid_message' => _m('The password fields must match'), 'invalid_message' => _m('The password fields must match'),
], ],
]; ];
@ -58,16 +58,16 @@ abstract class FormFields
{ {
return [ return [
'password', PasswordType::class, [ 'password', PasswordType::class, [
'label' => _m('Password'), 'label' => _m('Password'),
'label_attr' => ['class' => 'section-form-label'], 'label_attr' => ['class' => 'section-form-label'],
'attr' => ['placeholder' => '********'], 'attr' => ['placeholder' => '********'],
'required' => $options['required'] ?? true, 'required' => $options['required'] ?? true,
'mapped' => false, 'mapped' => false,
'constraints' => [ 'constraints' => [
new NotBlank(['message' => _m('Please enter a password')]), new NotBlank(['message' => _m('Please enter a password')]),
new Length(['min' => Common::config('password', 'min_length'), 'minMessage' => _m(['Your password should be at least # characters'], ['count' => Common::config('password', 'min_length')]), new Length(['min' => Common::config('password', 'min_length'), 'minMessage' => _m(['Your password should be at least # characters'], ['count' => Common::config('password', 'min_length')]),
'max' => Common::config('password', 'max_length'), 'maxMessage' => _m(['Your password should be at most # characters'], ['count' => Common::config('password', 'max_length')]), ]), 'max' => Common::config('password', 'max_length'), 'maxMessage' => _m(['Your password should be at most # characters'], ['count' => Common::config('password', 'max_length')]),]),
], ], ],],
]; ];
} }
@ -78,12 +78,12 @@ abstract class FormFields
'language' . ($multiple ? 's' : ''), 'language' . ($multiple ? 's' : ''),
ChoiceType::class, ChoiceType::class,
[ [
'label' => _m($label), 'label' => _m($label),
'preferred_choices' => $preferred_language_choices, 'preferred_choices' => $preferred_language_choices,
'choices' => $language_choices, 'choices' => $language_choices,
'required' => $required, 'required' => $required,
'multiple' => $multiple, 'multiple' => $multiple,
'help' => _m($help), 'help' => _m($help),
], ],
]; ];
} }