2021-07-29 18:27:01 +01:00
< ? php
2021-11-25 23:08:30 +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-25 23:08:30 +00:00
use function App\Core\I18n\_m ;
2021-11-15 17:08:22 +00:00
use App\Entity\Actor ;
2021-08-06 12:12:10 +01:00
use App\Util\Common ;
2021-12-26 09:48:16 +00:00
use Component\Language\Entity\Language ;
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-07-29 18:27:01 +01:00
abstract class FormFields
{
2021-11-25 23:16:04 +00:00
/**
* Create a form field for asking for a new password twice ( ensuring they ' re the same )
*/
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-25 23:08:30 +00:00
'type' => PasswordType :: class ,
2021-07-29 18:27:01 +01:00
'first_options' => [
2021-11-20 21:27:00 +00:00
'label' => _m ( 'Password' ),
'label_attr' => [ 'class' => 'section-form-label' ],
'attr' => array_merge ([ 'placeholder' => _m ( '********' ), 'required' => $options [ 'required' ] ? ? true ], $options [ 'attr' ] ? ? []),
2021-11-15 17:08:22 +00:00
'constraints' => $constraints ,
2021-11-25 23:08:30 +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-20 21:27:00 +00:00
'label' => _m ( 'Repeat Password' ),
'label_attr' => [ 'class' => 'section-form-label' ],
'attr' => array_merge ([ 'placeholder' => _m ( '********' ), 'required' => $options [ 'required' ] ? ? true ], $options [ 'attr' ] ? ? []),
'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-25 23:08:30 +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
/**
2021-11-25 23:16:04 +00:00
* Create a form field for asking for an existing password
*
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-20 21:27:00 +00:00
'label' => _m ( 'Password' ),
'label_attr' => [ 'class' => 'section-form-label' ],
'attr' => [ 'placeholder' => '********' , 'autocomplete' => 'current-password' ],
'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-25 23:08:30 +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
2021-11-25 23:16:04 +00:00
/**
* Create a from field for `select` ing a language for $actor , in reply or related to $context_actor
*/
2021-12-11 22:18:31 +00:00
public static function language ( ? Actor $actor , ? Actor $context_actor , string $label , ? string $help = null , bool $multiple = false , bool $required = true , ? bool $use_short_display = null , ? string $form_id = null , bool $use_no_selection = false ) : array
2021-11-15 17:08:22 +00:00
{
[ $language_choices , $preferred_language_choices ] = Language :: getSortedLanguageChoices ( $actor , $context_actor , use_short_display : $use_short_display );
2021-12-10 21:03:37 +00:00
if ( $use_no_selection ) {
$no_select = _m ( '(no selection)' );
// Put it at the beginning of $preferred_language_choices
$preferred_language_choices = array_merge ([ $no_select => 'null' ], $preferred_language_choices );
// but at the top of $language_choices
$language_choices [ $no_select ] = 'null' ;
}
2021-11-15 17:08:22 +00:00
return [
2021-12-10 13:52:18 +00:00
$form_id ? ? 'language' . ( $multiple ? 's' : '' ),
2021-11-15 17:08:22 +00:00
ChoiceType :: class ,
[
2021-11-28 12:25:23 +00:00
'label' => $label ,
2021-11-15 17:08:22 +00:00
'preferred_choices' => $preferred_language_choices ,
2021-11-25 23:08:30 +00:00
'choices' => $language_choices ,
'required' => $required ,
'multiple' => $multiple ,
2021-11-28 12:25:23 +00:00
'help' => $help ,
2021-11-15 17:08:22 +00:00
],
];
}
2021-07-29 18:27:01 +01:00
}