2021-07-29 18:27:01 +01:00
< ? php
2021-11-16 23:27:29 +00:00
declare ( strict_types = 1 );
2021-10-10 09:26:18 +01:00
2021-08-06 12:12:10 +01:00
namespace App\Util\Form ;
2021-07-29 18:27:01 +01:00
2021-11-15 17:08:22 +00:00
use App\Entity\Actor ;
use App\Entity\Language ;
2021-08-06 12:12:10 +01:00
use App\Util\Common ;
2021-11-15 17:08:22 +00:00
use Symfony\Component\Form\Extension\Core\Type\ChoiceType ;
2021-07-29 18:27:01 +01:00
use Symfony\Component\Form\Extension\Core\Type\PasswordType ;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType ;
2021-08-03 11:24:45 +01:00
use Symfony\Component\Validator\Constraints\Length ;
use Symfony\Component\Validator\Constraints\NotBlank ;
2021-11-16 23:27:29 +00:00
use function App\Core\I18n\_m ;
2021-07-29 18:27:01 +01:00
abstract class FormFields
{
2021-08-04 21:08:28 +01:00
public static function repeated_password ( array $options = []) : array
2021-07-29 18:27:01 +01:00
{
2021-11-15 19:32:14 +00:00
$constraints = ( $options [ 'required' ] ? ? true )
? [
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' )]),
'max' => Common :: config ( 'password' , 'max_length' ), 'maxMessage' => _m ([ 'Your password should be at most # characters' ], [ 'count' => Common :: config ( 'password' , 'max_length' )]), ]),
] : [];
2021-11-15 17:08:22 +00:00
2021-10-21 13:44:30 +01:00
return [
'password' , RepeatedType :: class ,
[
2021-11-16 23:27:29 +00:00
'type' => PasswordType :: class ,
2021-07-29 18:27:01 +01:00
'first_options' => [
2021-11-16 23:27:29 +00:00
'label' => _m ( 'Password' ),
'label_attr' => [ 'class' => 'section-form-label' ],
'attr' => [ 'placeholder' => _m ( '********' ), 'required' => $options [ 'required' ] ? ? true ],
2021-11-15 17:08:22 +00:00
'constraints' => $constraints ,
2021-11-16 23:27:29 +00:00
'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' )]),
2021-07-29 18:27:01 +01:00
],
'second_options' => [
2021-11-16 23:27:29 +00:00
'label' => _m ( 'Repeat Password' ),
'label_attr' => [ 'class' => 'section-form-label' ],
'attr' => [ 'placeholder' => _m ( '********' )],
'help' => _m ( 'Confirm your password.' ),
'required' => $options [ 'required' ] ? ? true ,
2021-11-15 17:08:22 +00:00
'constraints' => $constraints ,
2021-07-29 18:27:01 +01:00
],
2021-11-16 23:27:29 +00:00
'mapped' => false ,
'required' => $options [ 'required' ] ? ? true ,
2021-07-29 18:27:01 +01:00
'invalid_message' => _m ( 'The password fields must match' ),
],
];
}
2021-08-03 11:24:45 +01:00
2021-08-08 01:37:02 +01:00
/**
* @ codeCoverageIgnore
*/
2021-08-04 21:08:28 +01:00
public static function password ( array $options = []) : array
2021-08-03 11:24:45 +01:00
{
2021-09-06 19:49:03 +01:00
return [
'password' , PasswordType :: class , [
2021-11-16 23:27:29 +00:00
'label' => _m ( 'Password' ),
'label_attr' => [ 'class' => 'section-form-label' ],
'attr' => [ 'placeholder' => '********' ],
'required' => $options [ 'required' ] ? ? true ,
'mapped' => false ,
2021-09-06 19:49:03 +01:00
'constraints' => [
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' )]),
2021-11-16 23:27:29 +00:00
'max' => Common :: config ( 'password' , 'max_length' ), 'maxMessage' => _m ([ 'Your password should be at most # characters' ], [ 'count' => Common :: config ( 'password' , 'max_length' )]),]),
],],
2021-08-03 11:24:45 +01:00
];
}
2021-11-15 17:08:22 +00:00
public static function language ( Actor $actor , ? Actor $context_actor , string $label , string $help , bool $multiple = false , bool $required = true , ? bool $use_short_display = null ) : array
{
[ $language_choices , $preferred_language_choices ] = Language :: getSortedLanguageChoices ( $actor , $context_actor , use_short_display : $use_short_display );
return [
'language' . ( $multiple ? 's' : '' ),
ChoiceType :: class ,
[
2021-11-16 23:27:29 +00:00
'label' => _m ( $label ),
2021-11-15 17:08:22 +00:00
'preferred_choices' => $preferred_language_choices ,
2021-11-16 23:27:29 +00:00
'choices' => $language_choices ,
'required' => $required ,
'multiple' => $multiple ,
'help' => _m ( $help ),
2021-11-15 17:08:22 +00:00
],
];
}
2021-07-29 18:27:01 +01:00
}