From 6b8c3e722e0c4385587f3fcd2566d6e04f9e68c1 Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Tue, 2 Jul 2024 23:37:10 +0200 Subject: [PATCH] feat: Added CustomFormField `autocomplete` field to provide HTML autocomplete hint --- .../migrations/Version20240702205419.php | 31 ++++ .../src/Entity/CustomFormField.php | 49 ++++++ .../src/Form/CustomFormsType.php | 12 +- .../translations/custom-forms/messages.en.xlf | 140 ++++++++++++++++++ .../translations/custom-forms/messages.fr.xlf | 140 ++++++++++++++++++ .../translations/custom-forms/messages.xlf | 140 ++++++++++++++++++ lib/Rozier/src/Forms/CustomFormFieldType.php | 36 +++++ 7 files changed, 544 insertions(+), 4 deletions(-) create mode 100644 lib/RoadizCoreBundle/migrations/Version20240702205419.php diff --git a/lib/RoadizCoreBundle/migrations/Version20240702205419.php b/lib/RoadizCoreBundle/migrations/Version20240702205419.php new file mode 100644 index 00000000..23e5cfeb --- /dev/null +++ b/lib/RoadizCoreBundle/migrations/Version20240702205419.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE custom_form_fields ADD autocomplete VARCHAR(18) DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE custom_form_fields DROP autocomplete'); + } +} diff --git a/lib/RoadizCoreBundle/src/Entity/CustomFormField.php b/lib/RoadizCoreBundle/src/Entity/CustomFormField.php index c2bc3048..b58a6f29 100644 --- a/lib/RoadizCoreBundle/src/Entity/CustomFormField.php +++ b/lib/RoadizCoreBundle/src/Entity/CustomFormField.php @@ -12,6 +12,7 @@ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Serializer\Annotation as SymfonySerializer; use RZ\Roadiz\Core\AbstractEntities\AbstractField; +use Symfony\Component\Validator\Constraints\Choice; /** * CustomFormField entities are used to create CustomForms with @@ -74,6 +75,43 @@ class CustomFormField extends AbstractField ] private bool $required = false; + /** + * @var string|null https://developer.mozilla.org/fr/docs/Web/HTML/Attributes/autocomplete + */ + #[ + ORM\Column(name: "autocomplete", type: 'string', length:18, nullable: true), + Serializer\Groups(["custom_form"]), + SymfonySerializer\Groups(["custom_form"]), + Choice([ + 'off', + 'name', + 'honorific-prefix', + 'honorific-suffix', + 'given-name', + 'additional-name', + 'family-name', + 'nickname', + 'email', + 'username', + 'organization-title', + 'organization', + 'street-address', + 'country', + 'country-name', + 'postal-code', + 'bday', + 'bday-day', + 'bday-month', + 'bday-year', + 'sex', + 'tel', + 'tel-national', + 'url', + 'photo', + ]) + ] + private ?string $autocomplete = null; + public function __construct() { parent::__construct(); @@ -133,6 +171,17 @@ public function setRequired(bool $required): CustomFormField return $this; } + public function getAutocomplete(): ?string + { + return $this->autocomplete; + } + + public function setAutocomplete(?string $autocomplete): CustomFormField + { + $this->autocomplete = $autocomplete; + return $this; + } + /** * @return string */ diff --git a/lib/RoadizCoreBundle/src/Form/CustomFormsType.php b/lib/RoadizCoreBundle/src/Form/CustomFormsType.php index 5cd0711c..059f5e28 100644 --- a/lib/RoadizCoreBundle/src/Form/CustomFormsType.php +++ b/lib/RoadizCoreBundle/src/Form/CustomFormsType.php @@ -173,10 +173,14 @@ protected function getOptionsForField(CustomFormField $field, array $formOptions ], ]; - if ($field->getPlaceholder() !== '') { + if (!empty($field->getPlaceholder())) { $option['attr']['placeholder'] = $field->getPlaceholder(); } + if ($field->getAutocomplete() !== null) { + $option['attr']['autocomplete'] = $field->getAutocomplete(); + } + if ($field->isRequired()) { $option['required'] = true; $option['constraints'] = [ @@ -198,7 +202,7 @@ protected function getOptionsForField(CustomFormField $field, array $formOptions $option["format"] = DateType::HTML5_FORMAT; break; case AbstractField::ENUM_T: - if ($field->getPlaceholder() !== '') { + if (!empty($field->getPlaceholder())) { $option['placeholder'] = $field->getPlaceholder(); } $option["choices"] = $this->getChoices($field); @@ -212,7 +216,7 @@ protected function getOptionsForField(CustomFormField $field, array $formOptions } break; case AbstractField::MULTIPLE_T: - if ($field->getPlaceholder() !== '') { + if (!empty($field->getPlaceholder())) { $option['placeholder'] = $field->getPlaceholder(); } $option["choices"] = $this->getChoices($field); @@ -255,7 +259,7 @@ protected function getOptionsForField(CustomFormField $field, array $formOptions break; case AbstractField::COUNTRY_T: $option["expanded"] = $field->isExpanded(); - if ($field->getPlaceholder() !== '') { + if (!empty($field->getPlaceholder())) { $option['placeholder'] = $field->getPlaceholder(); } if (!empty($field->getDefaultValues())) { diff --git a/lib/RoadizRozierBundle/translations/custom-forms/messages.en.xlf b/lib/RoadizRozierBundle/translations/custom-forms/messages.en.xlf index 57a07a85..4b25760f 100644 --- a/lib/RoadizRozierBundle/translations/custom-forms/messages.en.xlf +++ b/lib/RoadizRozierBundle/translations/custom-forms/messages.en.xlf @@ -57,6 +57,146 @@ 2 year + + customForm.autocomplete + Auto-complete hint + + + + customForm.autocomplete.help + Provides a specific hint to browsers and input helpers on the value to auto-complete for this field. + + + + autocomplete.no_autocomplete + -- Undefined -- + + + + autocomplete.off + Do not auto-complete this field + + + + autocomplete.name + Full name + + + + autocomplete.honorific-prefix + Prefix or title, such as "Mrs.", "Mr.", "Miss", "Ms.", "Dr.", etc + + + + autocomplete.honorific-suffix + Suffix, such as "Jr.", "B.Sc.", "PhD.", "MBASW", or "IV" + + + + autocomplete.given-name + Given (or "first") name + + + + autocomplete.additional-name + Middle name + + + + autocomplete.family-name + Family (or "last") name + + + + autocomplete.nickname + Nickname or handle + + + + autocomplete.email + Email address + + + + autocomplete.username + Username or account name + + + + autocomplete.organization-title + Job title + + + + autocomplete.organization + Company or organization name + + + + autocomplete.street-address + Street address + + + + autocomplete.country + Country or territory code + + + + autocomplete.country-name + Country or territory name + + + + autocomplete.postal-code + Postal (or ZIP) code + + + + autocomplete.bday + Birth date, as a full date + + + + autocomplete.bday-day + Day of the month of a birth date + + + + autocomplete.bday-month + Month of the year of a birth date + + + + autocomplete.bday-year + Year of a birth date + + + + autocomplete.sex + Gender identity + + + + autocomplete.tel + Full phone number, including the country code + + + + autocomplete.tel-national + Phone number without the country code component + + + + autocomplete.url + A URL, as appropriate given the context of the form + + + + autocomplete.photo + URL of an image representing the contact information given in the form. + + diff --git a/lib/RoadizRozierBundle/translations/custom-forms/messages.fr.xlf b/lib/RoadizRozierBundle/translations/custom-forms/messages.fr.xlf index 482df27c..536eab2b 100644 --- a/lib/RoadizRozierBundle/translations/custom-forms/messages.fr.xlf +++ b/lib/RoadizRozierBundle/translations/custom-forms/messages.fr.xlf @@ -57,6 +57,146 @@ 2 ans + + customForm.autocomplete + Aide à l'auto-complétion + + + + customForm.autocomplete.help + Fournit un indice spécifique aux navigateurs et aides à la saisie sur la valeur à auto-completer pour ce champ. + + + + autocomplete.no_autocomplete + -- Non défini -- + + + + autocomplete.off + Désactiver l'auto-complétion pour ce champ + + + + autocomplete.name + Nom complet + + + + autocomplete.honorific-prefix + Préfixe ou titre, par exemple « M. », « Mme. », « Me. » , etc + + + + autocomplete.honorific-suffix + Suffixe (par exemple "Jr.") + + + + autocomplete.given-name + Prénom + + + + autocomplete.additional-name + Deuxième prénom + + + + autocomplete.family-name + Nom de famille + + + + autocomplete.nickname + Surnom + + + + autocomplete.email + Adresse email + + + + autocomplete.username + Nom de compte ou un nom d'utilisateur + + + + autocomplete.organization-title + Titre de poste professionnel + + + + autocomplete.organization + Nom d'une entreprise ou d'une organisation + + + + autocomplete.street-address + Adresse postale + + + + autocomplete.country + Code de pays + + + + autocomplete.country-name + Nom de pays + + + + autocomplete.postal-code + Code postal + + + + autocomplete.bday + Date de naissance complète + + + + autocomplete.bday-day + Jour de naissance + + + + autocomplete.bday-month + Mois de naissance + + + + autocomplete.bday-year + Année de naissance + + + + autocomplete.sex + Genre + + + + autocomplete.tel + Téléphone (avec indicateur du pays) + + + + autocomplete.tel-national + Téléphone (sans indicateur du pays) + + + + autocomplete.url + Une URL, pertinente dans le contexte du formulaire + + + + autocomplete.photo + URL d'une image représentant l'information de contact + + diff --git a/lib/RoadizRozierBundle/translations/custom-forms/messages.xlf b/lib/RoadizRozierBundle/translations/custom-forms/messages.xlf index b9e93833..b6466572 100644 --- a/lib/RoadizRozierBundle/translations/custom-forms/messages.xlf +++ b/lib/RoadizRozierBundle/translations/custom-forms/messages.xlf @@ -57,6 +57,146 @@ + + customForm.autocomplete + + + + + customForm.autocomplete.help + + + + + autocomplete.no_autocomplete + + + + + autocomplete.off + + + + + autocomplete.name + + + + + autocomplete.honorific-prefix + + + + + autocomplete.honorific-suffix + + + + + autocomplete.given-name + + + + + autocomplete.additional-name + + + + + autocomplete.family-name + + + + + autocomplete.nickname + + + + + autocomplete.email + + + + + autocomplete.username + + + + + autocomplete.organization-title + + + + + autocomplete.organization + + + + + autocomplete.street-address + + + + + autocomplete.country + + + + + autocomplete.country-name + + + + + autocomplete.postal-code + + + + + autocomplete.bday + + + + + autocomplete.bday-day + + + + + autocomplete.bday-month + + + + + autocomplete.bday-year + + + + + autocomplete.sex + + + + + autocomplete.tel + + + + + autocomplete.tel-national + + + + + autocomplete.url + + + + + autocomplete.photo + + + diff --git a/lib/Rozier/src/Forms/CustomFormFieldType.php b/lib/Rozier/src/Forms/CustomFormFieldType.php index 0fc5675a..16fb552a 100644 --- a/lib/Rozier/src/Forms/CustomFormFieldType.php +++ b/lib/Rozier/src/Forms/CustomFormFieldType.php @@ -61,6 +61,42 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'label' => 'groupName', 'required' => false, 'help' => 'use_the_same_group_names_over_fields_to_gather_them_in_tabs', + ]) + ->add('autocomplete', ChoiceType::class, [ + 'label' => 'customForm.autocomplete', + 'help' => 'customForm.autocomplete.help', + 'choices' => [ + 'off', + 'name', + 'honorific-prefix', + 'honorific-suffix', + 'given-name', + 'additional-name', + 'family-name', + 'nickname', + 'email', + 'username', + 'organization-title', + 'organization', + 'street-address', + 'country', + 'country-name', + 'postal-code', + 'bday', + 'bday-day', + 'bday-month', + 'bday-year', + 'sex', + 'tel', + 'tel-national', + 'url', + 'photo', + ], + 'placeholder' => 'autocomplete.no_autocomplete', + 'choice_label' => function ($choice, $key, $value) { + return 'autocomplete.' . $value; + }, + 'required' => false, ]); }