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:35] formateur |
symfony_4:fonctionnement:les_formulaires_gestion_des_formulaires [2022/11/08 09:50] (Version actuelle) admin [Rendu en twig] |
||
|---|---|---|---|
| Ligne 91: | Ligne 91: | ||
| ' | ' | ||
| ), | ), | ||
| + | )) | ||
| + | </ | ||
| + | |||
| + | Choix multiple : | ||
| + | <code PHP> | ||
| + | use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | ||
| + | |||
| + | ... | ||
| + | | ||
| + | -> | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ), | ||
| + | ' | ||
| + | ' | ||
| )) | )) | ||
| </ | </ | ||
| Ligne 101: | 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 | ||
| + | -> | ||
| + | ' | ||
| + | )); | ||
| </ | </ | ||
| Ligne 113: | Ligne 195: | ||
| Bouton de soumission du formulaire | Bouton de soumission du formulaire | ||
| <code PHP> | <code PHP> | ||
| - | | + | use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
| - | -> | + | |
| - | ' | + | ... |
| - | )); | + | |
| - | } | + | -> |
| + | ' | ||
| + | )); | ||
| </ | </ | ||
| ---- | ---- | ||
| - | ===== Fonction de configuration : création du formulaire | + | ===== Fonction de configuration : Définition de la classe liée ===== |
| Fonction qui permet d' | Fonction qui permet d' | ||
| Ligne 136: | 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 %} | ||
| </ | </ | ||