Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
|
symfony_4:fonctionnement:les_formulaires_gestion_des_formulaires [2018/10/23 07:25] formateur [Les types de champs] |
symfony_4:fonctionnement:les_formulaires_gestion_des_formulaires [2022/11/08 09:50] (Version actuelle) admin [Rendu en twig] |
||
|---|---|---|---|
| Ligne 48: | Ligne 48: | ||
| ===== Les types de champs ===== | ===== Les types de champs ===== | ||
| - | La liste des différents | + | Les principaux |
| - | * [[https:// | + | |
| - | Quelques types principaux : | ||
| * Entité (objet de l' | * Entité (objet de l' | ||
| * [[https:// | * [[https:// | ||
| Ligne 57: | Ligne 55: | ||
| * [[https:// | * [[https:// | ||
| * [[https:// | * [[https:// | ||
| + | * [[https:// | ||
| * Texte | * Texte | ||
| * [[https:// | * [[https:// | ||
| Ligne 62: | Ligne 61: | ||
| * Choix | * Choix | ||
| * [[https:// | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| * Date | * Date | ||
| * [[https:// | * [[https:// | ||
| Ligne 70: | Ligne 71: | ||
| * [[https:// | * [[https:// | ||
| * [[https:// | * [[https:// | ||
| + | |||
| + | La liste complète des types de champs est présente sur la documentation de Symfony : [[https:// | ||
| + | |||
| + | ===== Les types de champs : Quelques exemples ===== | ||
| ==== Liste déroulante : ChoiceType ==== | ==== Liste déroulante : ChoiceType ==== | ||
| Ligne 86: | Ligne 91: | ||
| ' | ' | ||
| ), | ), | ||
| + | )) | ||
| + | </ | ||
| + | |||
| + | Choix multiple : | ||
| + | <code PHP> | ||
| + | use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | ||
| + | |||
| + | ... | ||
| + | | ||
| + | -> | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ), | ||
| + | ' | ||
| + | ' | ||
| )) | )) | ||
| </ | </ | ||
| Ligne 96: | Ligne 118: | ||
| Attention, required à " | Attention, required à " | ||
| <code PHP> | <code PHP> | ||
| - | | + | use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
| - | ' | + | |
| - | ' | + | ... |
| - | )) | + | |
| + | -> | ||
| + | | ||
| + | | ||
| + | | ||
| </ | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ==== Exemples généraux ==== | ||
| + | |||
| + | Quelques exemples : | ||
| + | <code PHP> | ||
| + | use Symfony\Component\Form\Extension\Core\Type\ResetType; | ||
| + | use Symfony\Component\Form\Extension\Core\Type\IntegerType; | ||
| + | use Symfony\Component\Form\Extension\Core\Type\NumberType; | ||
| + | use Symfony\Component\Form\Extension\Core\Type\EmailType; | ||
| + | use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
| + | use Symfony\Component\Form\Extension\Core\Type\HiddenType; | ||
| + | use Symfony\Component\Form\Extension\Core\Type\FileType; | ||
| + | use Symfony\Component\Form\Extension\Core\Type\SubmitType; | ||
| + | |||
| + | ... | ||
| + | | ||
| + | //Ajout du bouton reset | ||
| + | -> | ||
| + | ' | ||
| + | )); | ||
| + | | ||
| + | //Ajout du champ de nombre entier | ||
| + | -> | ||
| + | |||
| + | //Ajout d'un champ de nombre | ||
| + | -> | ||
| + | ' | ||
| + | ' | ||
| + | ), | ||
| + | ' | ||
| + | ' | ||
| + | ) | ||
| + | | ||
| + | //Ajout d'un champ mail | ||
| + | -> | ||
| + | ' | ||
| + | ' | ||
| + | )) | ||
| + | | ||
| + | //Ajout d'un champ text | ||
| + | -> | ||
| + | ' | ||
| + | )) | ||
| + | | ||
| + | //Ajout d'un champ text masqué absent de la classe, juste utile pour la gestion du formulaire | ||
| + | -> | ||
| + | ' | ||
| + | ) | ||
| + | ) | ||
| + | | ||
| + | //Ajout de la carte d' | ||
| + | -> | ||
| + | | ||
| + | //Ajout du bouton de soumission du formulaire | ||
| + | -> | ||
| + | ' | ||
| + | )); | ||
| + | |||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ==== Soumission du formulaire : SubmitType ==== | ||
| Bouton de soumission du formulaire | Bouton de soumission du formulaire | ||
| <code PHP> | <code PHP> | ||
| - | | + | use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
| - | -> | + | |
| - | ' | + | ... |
| - | )); | + | |
| - | } | + | -> |
| + | ' | ||
| + | )); | ||
| </ | </ | ||
| - | Fonction | + | ---- |
| + | |||
| + | ===== Fonction de configuration | ||
| + | |||
| + | Fonction qui permet d' | ||
| <code PHP> | <code PHP> | ||
| + | use App\Entity\Test; | ||
| + | |||
| + | ... | ||
| + | | ||
| public function configureOptions(OptionsResolver $resolver) | public function configureOptions(OptionsResolver $resolver) | ||
| { | { | ||
| Ligne 119: | Ligne 220: | ||
| )); | )); | ||
| } | } | ||
| - | }</ | + | </ |
| ---- | ---- | ||
| - | ==== Exemple complet | + | ===== Rendu en twig ===== |
| + | |||
| + | === Affichage des champs dans le formulaire | ||
| + | Code basique : (Tout en un) | ||
| <code PHP> | <code PHP> | ||
| - | <?php | + | {% if form.date is defined %} |
| - | // / | + | <div class=" |
| - | namespace App\Form; | + | {{ form_row(form.date) }} |
| + | </ | ||
| + | {% endif %} | ||
| + | </ | ||
| - | use App\Entity\SWFile; | + | Code plus avancé : contrôle de l' |
| - | use Symfony\Component\Form\AbstractType; | + | <code PHP> |
| - | use Symfony\Component\Form\Extension\Core\Type\CheckboxType; | + | {{ form_start(form, |
| - | use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | + | {% if form.date is defined %} |
| - | use Symfony\Component\Form\Extension\Core\Type\SubmitType; | + | {{ form_error(form.date) }} |
| - | use Symfony\Component\Form\FormBuilderInterface; | + | < |
| - | use Symfony\Component\OptionsResolver\OptionsResolver; | + | {{ form_widget(form.date) }} |
| - | use Symfony\Component\Form\Extension\Core\Type\TextType; | + | {{ form_label(form.date, |
| + | </ | ||
| + | {% endif %} | ||
| + | {{ form_end(form) }} | ||
| + | </ | ||
| - | class SWFileType extends AbstractType | + | Affichage de toutes les erreurs du formulaire dans le cadre de l'usage de rendu sans // |
| - | { | + | <code PHP> |
| - | public function buildForm(FormBuilderInterface $builder, array $options) | + | <ul> |
| - | { | + | {% for error in form.vars.errors.form.getErrors(true) %} |
| - | $builder | + | < |
| - | ->add('name', TextType::class, array( | + | |
| - | ' | + | </ul> |
| - | )) | + | |
| - | ->add(' | + | |
| - | ' | + | |
| - | 'Style (CSS)' => " | + | |
| - | ' | + | |
| - | ' | + | |
| - | ), | + | |
| - | )) | + | |
| - | -> | + | |
| - | ' | + | |
| - | )) | + | |
| - | -> | + | |
| - | ' | + | |
| - | ' | + | |
| - | )) | + | |
| - | -> | + | |
| - | ' | + | |
| - | )) | + | |
| - | ; | + | |
| - | } | + | |
| - | public function configureOptions(OptionsResolver $resolver) | + | {% if form.date is defined %} |
| - | { | + | < |
| - | | + | {{ form_widget(form.date) }} |
| - | ' | + | {{ form_label(form.date, |
| - | )); | + | </ |
| - | } | + | {% endif %} |
| - | } | + | |
| + | {% if form.date1 is defined %} | ||
| + | < | ||
| + | {{ form_widget(form.date1) }} | ||
| + | {{ form_label(form.date1, "Date 1") }} | ||
| + | </div> | ||
| + | {% endif %} | ||
| + | |||
| + | {% if form.date2 is defined %} | ||
| + | < | ||
| + | {{ form_widget(form.date2) }} | ||
| + | {{ form_label(form.date2, | ||
| + | </ | ||
| + | {% endif %} | ||
| </ | </ | ||