IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Symfony PHP Discussion :

Formulaire champs entity et récupération twig [2.x]


Sujet :

Symfony PHP

  1. #1
    Nouveau membre du Club
    Inscrit en
    Juin 2013
    Messages
    34
    Détails du profil
    Informations forums :
    Inscription : Juin 2013
    Messages : 34
    Points : 33
    Points
    33
    Par défaut Formulaire champs entity et récupération twig
    Bonjour,

    J'ai actuellement un champ entity de la forme suivante:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    $builder
                ->add('langue', 'entity', array(
                        'multiple'  => true,
                        'expanded' => true,
                        'class' => 'ProjetMainBundle:Langue',
                        'query_builder' => function(EntityRepository $er) {
                            return $er->createQueryBuilder('u')
                            ->orderBy('u.ordre', 'ASC');
                        },
                        'property' => 'nom',
                        'required' => false
                ))

    Cependant lors de l'affichage twig je ne peux récupérer que le champ "nom" (mis en option property dans le Form)?

    Il n'est pas possible de récupérer un autre champ de mon entity ("url" par exemple pour récupérer l'image associée à l'age)?

    Je voudrai faire quelquechose comme ceci dans twig:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    {% for langue in form.langue %}
        <div class="img_checkbox">
            {{ form_widget(image_langue, {'attr': {'class': 'checkbox'}}) }}
            <img class="form-img-multi-clickable" src="{{ langue.vars.url }}" width="100" height="100" alt="Image {{ langue.vars.label }}"/>
        </div>
    {% endfor %}
    Actuellement je n'ai pas le champ url.

  2. #2
    Membre éprouvé
    Homme Profil pro
    Inscrit en
    Juin 2011
    Messages
    725
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juin 2011
    Messages : 725
    Points : 1 050
    Points
    1 050
    Par défaut
    Bonjour,
    Comme ceci peut-être:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    {% for langue in form.langue %}
        <div class="img_checkbox">
            {{ form_widget(image_langue, {'attr': {'class': 'checkbox'}}) }}
            <img class="form-img-multi-clickable" src="{{ langue.vars.value.url }}" width="100" height="100" alt="Image {{ langue.vars.label }}"/>
        </div>
    {% endfor %}

  3. #3
    Nouveau membre du Club
    Inscrit en
    Juin 2013
    Messages
    34
    Détails du profil
    Informations forums :
    Inscription : Juin 2013
    Messages : 34
    Points : 33
    Points
    33
    Par défaut
    Bonjour,

    Malheureusement non.

    {{ langue.vars.value }} renvoie 1 pour la première langue, 2 pour la seconde, etc

    langue.vars.value.url n'existe pas.

  4. #4
    Membre éprouvé
    Homme Profil pro
    Inscrit en
    Juin 2011
    Messages
    725
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juin 2011
    Messages : 725
    Points : 1 050
    Points
    1 050
    Par défaut
    Que dit le débogage ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    {% for langue in form.langue %}
        <div class="img_checkbox">
            {{ form_widget(image_langue, {'attr': {'class': 'checkbox'}}) }}
    {{dump(langue.vars)}}
    {#ou bien #}
    {{dump(langue)}}
        </div>
    {% endfor %}

  5. #5
    Nouveau membre du Club
    Inscrit en
    Juin 2013
    Messages
    34
    Détails du profil
    Informations forums :
    Inscription : Juin 2013
    Messages : 34
    Points : 33
    Points
    33
    Par défaut
    Voici le résultat pour dump(langue.vars)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    array
      'value' => string '1' (length=1)
      'attr' => 
        array
          empty
      'form' => 
        object(Symfony\Component\Form\FormView)[1827]
          public 'vars' => 
            array
              'value' => string '1' (length=1)
              'attr' => 
                array
                  ...
              'form' => 
                &object(Symfony\Component\Form\FormView)[1827]
              'id' => string 'informations_langue_1' (length=21)
              'name' => string '1' (length=1)
              'full_name' => string 'informations[langue][]' (length=22)
              'read_only' => boolean false
              'errors' => 
                array
                  ...
              'valid' => boolean true
              'data' => boolean false
              'disabled' => boolean false
              'required' => boolean false
              'max_length' => null
              'pattern' => null
              'size' => null
              'label' => string 'Français' (length=11)
              'multipart' => boolean false
              'label_attr' => 
                array
                  ...
              'compound' => boolean false
              'block_prefixes' => 
                array
                  ...
              'unique_block_prefix' => string '_informations_langue_1' (length=22)
              'translation_domain' => string 'messages' (length=8)
              'cache_key' => string '_informations_langue_1_checkbox' (length=31)
              'checked' => boolean false
          public 'parent' => 
            object(Symfony\Component\Form\FormView)[1824]
              public 'vars' => 
                array
                  ...
              public 'parent' => 
                object(Symfony\Component\Form\FormView)[1685]
                  ...
              public 'children' => 
                array
                  ...
              private 'rendered' => boolean false
          public 'children' => 
            array
              empty
          private 'rendered' => boolean false
      'id' => string 'informations_langue_1' (length=21)
      'name' => string '1' (length=1)
      'full_name' => string 'informations[langue][]' (length=22)
      'read_only' => boolean false
      'errors' => 
        array
          empty
      'valid' => boolean true
      'data' => boolean false
      'disabled' => boolean false
      'required' => boolean false
      'max_length' => null
      'pattern' => null
      'size' => null
      'label' => string 'Français' (length=11)
      'multipart' => boolean false
      'label_attr' => 
        array
          empty
      'compound' => boolean false
      'block_prefixes' => 
        array
          0 => string 'form' (length=4)
          1 => string 'field' (length=5)
          2 => string 'checkbox' (length=8)
          3 => string '_informations_langue_1' (length=22)
      'unique_block_prefix' => string '_informations_langue_1' (length=22)
      'translation_domain' => string 'messages' (length=8)
      'cache_key' => string '_informations_langue_1_checkbox' (length=31)
      'checked' => boolean false

    Pour dump(langue):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    object(Symfony\Component\Form\FormView)[1827]
      public 'vars' => 
        array
          'value' => string '1' (length=1)
          'attr' => 
            array
              empty
          'form' => 
            &object(Symfony\Component\Form\FormView)[1827]
          'id' => string 'informations_langue_1' (length=21)
          'name' => string '1' (length=1)
          'full_name' => string 'informations[langue][]' (length=22)
          'read_only' => boolean false
          'errors' => 
            array
              empty
          'valid' => boolean true
          'data' => boolean false
          'disabled' => boolean false
          'required' => boolean false
          'max_length' => null
          'pattern' => null
          'size' => null
          'label' => string 'Français' (length=11)
          'multipart' => boolean false
          'label_attr' => 
            array
              empty
          'compound' => boolean false
          'block_prefixes' => 
            array
              0 => string 'form' (length=4)
              1 => string 'field' (length=5)
              2 => string 'checkbox' (length=8)
              3 => string '_informations_langue_1' (length=22)
          'unique_block_prefix' => string '_informations_langue_1' (length=22)
          'translation_domain' => string 'messages' (length=8)
          'cache_key' => string '_informations_langue_1_checkbox' (length=31)
          'checked' => boolean false
      public 'parent' => 
        object(Symfony\Component\Form\FormView)[1824]
          public 'vars' => 
            array
              'value' => 
                array
                  ...
              'attr' => 
                array
                  ...
              'form' => 
                &object(Symfony\Component\Form\FormView)[1824]
              'id' => string 'informations_langue' (length=19)
              'name' => string 'langue' (length=6)
              'full_name' => string 'informations[langue]' (length=20)
              'read_only' => boolean false
              'errors' => 
                array
                  ...
              'valid' => boolean true
              'data' => null
              'disabled' => boolean false
              'required' => boolean false
              'max_length' => null
              'pattern' => null
              'size' => null
              'label' => null
              'multipart' => boolean false
              'label_attr' => 
                array
                  ...
              'compound' => boolean true
              'block_prefixes' => 
                array
                  ...
              'unique_block_prefix' => string '_informations_langue' (length=20)
              'translation_domain' => string 'messages' (length=8)
              'cache_key' => string '_informations_langue_entity' (length=27)
              'multiple' => boolean true
              'expanded' => boolean true
              'preferred_choices' => 
                array
                  ...
              'choices' => 
                array
                  ...
              'separator' => string '-------------------' (length=19)
              'empty_value' => null
              'is_selected' => 
                object(Closure)[1823]
                  ...
          public 'parent' => 
            object(Symfony\Component\Form\FormView)[1685]
              public 'vars' => 
                array
                  ...
              public 'parent' => null
              public 'children' => 
                array
                  ...
              private 'rendered' => boolean false
          public 'children' => 
            array
              1 => 
                &object(Symfony\Component\Form\FormView)[1827]
              2 => 
                object(Symfony\Component\Form\FormView)[1829]
                  ...
          private 'rendered' => boolean false
      public 'children' => 
        array
          empty
      private 'rendered' => boolean false


    Pour moi ça vient de la ligne:
    dans le FormType (1er message).
    Vu qu'on lui dit de prendre le champ 'nom', il ne met pas dans le form les autres champs de mon entité Langue?

    Impossible de mettre un array pour "property".


    Sinon je peux récupérer toutes les infos dans mon controller en faisant: "$form->getLangue()" ca me retourne une collection d'entités 'Langue'. Je pourrais ensuite passer cette collection à la vue twig mais ce n'est clairement pas propre et risque de causer des bugs.

  6. #6
    Membre régulier
    Profil pro
    Inscrit en
    Mai 2010
    Messages
    125
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2010
    Messages : 125
    Points : 108
    Points
    108
    Par défaut
    Salut, je pense c'est possible tu fais une deuxieme boucle incluse dans la première sur langue (si j'ai bien compris tes variables)

  7. #7
    Nouveau membre du Club
    Inscrit en
    Juin 2013
    Messages
    34
    Détails du profil
    Informations forums :
    Inscription : Juin 2013
    Messages : 34
    Points : 33
    Points
    33
    Par défaut
    Citation Envoyé par SAmpistaroy Voir le message
    Salut, je pense c'est possible tu fais une deuxieme boucle incluse dans la première sur langue (si j'ai bien compris tes variables)
    Hum je n'ai pas bien comprit ta seconde boucle.
    J'ai essayé ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    {% for langue in form.langue %}
                                                        {% for langue2 in langue.nom %}
                                                            {{ dump(langue2) }}
                                                        {% endfor %}
                                                    {% endfor %}
    Mais ca me renvoye ce message d'erreur:
    Method "nom" for object "Symfony\Component\Form\FormView" does not exist
    Il n'y a pas de nom de champ "nom" dans le formulaire.

  8. #8
    Membre régulier
    Profil pro
    Inscrit en
    Mai 2010
    Messages
    125
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2010
    Messages : 125
    Points : 108
    Points
    108
    Par défaut
    essaye avec langue a la place de langue.nom et tu mets langue2.nom après
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    {% for langue in form.langue %}
                                                        {% for langue2 in langue %}
                                                            {{ dump(langue2.nom) }}
                                                        {% endfor %}
                                                    {% endfor %}

  9. #9
    Membre régulier
    Profil pro
    Inscrit en
    Mai 2010
    Messages
    125
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2010
    Messages : 125
    Points : 108
    Points
    108
    Par défaut
    Voila moi ce que j'ai réussis a faire avec 3 entités:
    -uzer
    -uzercateg
    -uzercarac

    relation:
    uzer <= M2M =>uzercateg
    uzer<= M2M => uzercarac
    uzercarac<= M2O => uzercateg

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
     
      {% if uzer.uzercategs.count > 0 %}
        {% for categorie in uzer.uzercategs %}
        <h4>{{ categorie.title }}</h4>
     
         {% if uzer.uzercaracs.count > 0 %}
           {% for caract in uzer.uzercaracs %}
           {% if categorie.id==caract.uzercateg.id%}
             {{ caract.title }}
         {% endif %}
           {% endfor %}
         {% endif %}
     
        {% endfor %}
      {% endif %}
    et je n'envois que uzer dans ma page.

  10. #10
    Membre éprouvé
    Homme Profil pro
    Inscrit en
    Juin 2011
    Messages
    725
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juin 2011
    Messages : 725
    Points : 1 050
    Points
    1 050
    Par défaut
    essaie quelque chose comme cela
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    {% for langue in form.langue %}
        <div class="img_checkbox">
            {{ form_widget(image_langue, {'attr': {'class': 'checkbox'}}) }}
            {%set object=form.langue.vars.choice_list[langue.vars.value].choice%}
            <img src="{{object.url}}"/>
        </div>
    {% endfor %}
    n'hesites pas à faire un {{dump(form.langue.vars)}}

  11. #11
    Nouveau membre du Club
    Inscrit en
    Juin 2013
    Messages
    34
    Détails du profil
    Informations forums :
    Inscription : Juin 2013
    Messages : 34
    Points : 33
    Points
    33
    Par défaut
    Citation Envoyé par SAmpistaroy Voir le message
    essaye avec langue a la place de langue.nom et tu mets langue2.nom après
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    {% for langue in form.langue %}
                                                        {% for langue2 in langue %}
                                                            {{ dump(langue2.nom) }}
                                                        {% endfor %}
                                                    {% endfor %}
    Ne fonctionne pas. Enfin pas d'erreur mais rien ne s'affiche.
    Je crois que langue est un tableau vide.



    Citation Envoyé par arnooo999 Voir le message
    essaie quelque chose comme cela
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    {% for langue in form.langue %}
        <div class="img_checkbox">
            {{ form_widget(image_langue, {'attr': {'class': 'checkbox'}}) }}
            {%set object=form.langue.vars.choice_list[langue.vars.value].choice%}
            <img src="{{object.url}}"/>
        </div>
    {% endfor %}
    n'hesites pas à faire un {{dump(form.langue.vars)}}
    Merci c'est cela à deux trois choses près.
    Voici ce que j'ai utilisé pour les futurs intéressés:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    {% for langue in form.langue %}
        <div class="img_checkbox">
            {{ form_widget(langue, {'attr': {'class': 'checkbox'}}) }}
            {%set object=form.langue.vars.choices[langue.vars.value].data%}
            <img src="{{object.url}}"/>
        </div>
    {% endfor %}

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 6
    Dernier message: 24/06/2015, 17h20
  2. [2.x] Problème validation champs entity dans formulaire service
    Par Naezys dans le forum Symfony
    Réponses: 2
    Dernier message: 09/02/2015, 13h55
  3. [2.x] Formulaire avec champ entity
    Par jillthe1 dans le forum Symfony
    Réponses: 15
    Dernier message: 21/10/2014, 14h43
  4. [2.x] Formulaire avec champ entity
    Par jillthe1 dans le forum Symfony
    Réponses: 8
    Dernier message: 17/08/2014, 01h56
  5. [HTML][FORMULAIRE] Probleme dans la récupération des données
    Par baddounet dans le forum Balisage (X)HTML et validation W3C
    Réponses: 6
    Dernier message: 15/08/2005, 18h51

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo