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 :

champs vides qui ne peuvent pas l'être, sauf que si [2.x]


Sujet :

Symfony PHP

  1. #1
    Membre à l'essai
    Homme Profil pro
    Inscrit en
    Juillet 2011
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Juillet 2011
    Messages : 22
    Points : 16
    Points
    16
    Par défaut champs vides qui ne peuvent pas l'être, sauf que si
    salut. j'ai un petit probleme mais je sais pas trop si ca a sa place ici ou plus dans le sous-forum doctrine2...

    d'abord, les parties de mon code concernées

    ma page new.html.twig
    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
    {% extends 'MyBundle:Default:index.html.twig' %}
    {% block title %}Créer un Noeud{% endblock %}
    {% block menu %}{% endblock %}
    {% block content %}
    <h1>AwTree creation</h1>
     
    <form action="{{ path('tree_create') }}" method="post" {{ form_enctype(form) }}>
        {{ form_widget(form) }}
        <p>
            <button type="submit">Create</button>
        </p>
    </form>
     
    <ul class="record_actions">
        <li>
            <a href="{{ path('tree') }}">
                Back to the list
            </a>
        </li>
    </ul>
     
    {% endblock %}
    mon entité
    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
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    <?php
     
    namespace MyModelBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
    use Gedmo\Mapping\Annotation as Gedmo;
     
    /**
     * @Gedmo\Tree(type="nested")
     * @ORM\Table(name="aw_tree")
     * @ORM\Entity(repositoryClass="MyModelBundle\Repository\AwTreeRepository")
     */
    class AwTree 
    {
        /**
         * @ORM\Column(name="id", type="bigint")
         * @ORM\Id
         * @ORM\GeneratedValue
         */
        private $id;
     
        /**
         * @ORM\Column(name="title", type="string", length=64, nullable=false)
         */
        private $title;
     
        /**
         * @Gedmo\TreeLeft
         * @ORM\Column(name="lft", type="integer")
         */
        private $lft;
     
        /**
         * @Gedmo\TreeLevel
         * @ORM\Column(name="lvl", type="integer")
         */
        private $lvl;
     
        /**
         * @Gedmo\TreeRight
         * @ORM\Column(name="rgt", type="integer")
         */
        private $rgt;
     
        /**
         * @Gedmo\TreeRoot
         * @ORM\Column(name="root", type="integer", nullable=true)
         */
        private $root;
     
        /**
         * @Gedmo\TreeParent
         * @ORM\ManyToOne(targetEntity="AwTree", inversedBy="children")
         * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL")
         */
        private $parent;
     
        /**
         * @ORM\OneToMany(targetEntity="AwTree", mappedBy="parent")
         * @ORM\OrderBy({"lft" = "ASC"})
         */
        private $children;
     
        /**
         * Get id
         *
         * @return bigint 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set title
         *
         * @param string $title
         */
        public function setTitle($title)
        {
            $this->title = $title;
        }
     
        /**
         * Get title
         *
         * @return string 
         */
        public function getTitle()
        {
            return $this->title;
        }
     
        /**
         * Set lft
         *
         * @param integer $lft
         */
        public function setLft($lft)
        {
            $this->lft = $lft;
        }
     
        /**
         * Get lft
         *
         * @return integer 
         */
        public function getLft()
        {
            return $this->lft;
        }
     
        /**
         * Set lvl
         *
         * @param integer $lvl
         */
        public function setLvl($lvl)
        {
            $this->lvl = $lvl;
        }
     
        /**
         * Get lvl
         *
         * @return integer 
         */
        public function getLvl()
        {
            return $this->lvl;
        }
     
        /**
         * Set rgt
         *
         * @param integer $rgt
         */
        public function setRgt($rgt)
        {
            $this->rgt = $rgt;
        }
     
        /**
         * Get rgt
         *
         * @return integer 
         */
        public function getRgt()
        {
            return $this->rgt;
        }
     
        /**
         * Set root
         *
         * @param integer $root
         */
        public function setRoot($root)
        {
            $this->root = $root;
        }
     
        /**
         * Get root
         *
         * @return integer 
         */
        public function getRoot()
        {
            return $this->root;
        }
     
        /**
         * Set parent
         *
         * @param MyModelBundle\Entity\AwTree $parent
         */
        public function setParent(AwTree $parent)
        {
            $this->parent = $parent;
        }
     
        /**
         * Get parent
         *
         * @return AwTree 
         */
        public function getParent()
        {
            return $this->parent;
        }
     
        /**
         * Get children
         *
         * @return Doctrine\Common\Collections\Collection 
         */
        public function getChildren()
        {
            return $this->children;
        }
     
        public function __construct()
        {
            $this->children = new \Doctrine\Common\Collections\ArrayCollection();
        }
     
        /**
         * Add children
         *
         * @param MyModelBundle\Entity\AwTree $children
         */
        public function addAwTree(\MyModelBundle\Entity\AwTree $children)
        {
            $this->children[] = $children;
        }
    }
    mon formulaire
    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
    <?php
     
    namespace MyModelBundle\Form;
     
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilder;
     
    class AwTreeType extends AbstractType
    {
        public function buildForm(FormBuilder $builder, array $options)
        {
            $builder
                ->add('title')
                ->add('parent')
            ;
        }
     
        public function getName()
        {
            return 'mymodelbundle_awtreetype';
        }
    }
    le controller est encore à modifier, mais c'est pas ca le problème

    le parent peut être null, d'après la db (et c'est cool, c'est ce que je veux)
    mais sur la page, il peut pas...
    en gros, quand je crée un noeud de type awTree, je lui rentre un nom et quand je tente de le créer sans parent (de toute facon, il peut pas avoir de parent quand il y a encore rien dans la db), un message apparait, me disant "veuillez selectionner un parent dans la liste", liste dans laquelle il n'y a aucun parent vu que la table est vide...

    somebody please help me

    gasmichou

  2. #2
    Inscrit
    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    319
    Détails du profil
    Informations personnelles :
    Âge : 35
    Localisation : France

    Informations forums :
    Inscription : Décembre 2004
    Messages : 319
    Points : 476
    Points
    476
    Par défaut
    Salut,

    http://symfony.com/doc/current/refer...es/entity.html

    Tu dois mettre required false dans le tableau en 3e argument de ton add.

  3. #3
    Membre à l'essai
    Homme Profil pro
    Inscrit en
    Juillet 2011
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Juillet 2011
    Messages : 22
    Points : 16
    Points
    16
    Par défaut
    ok, c'est bon
    ca marche, merci
    (bon, me reste plus qu'à modifier le controller ^^)

  4. #4
    Membre à l'essai
    Homme Profil pro
    Inscrit en
    Juillet 2011
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Juillet 2011
    Messages : 22
    Points : 16
    Points
    16
    Par défaut
    en fait, non, ca marche pas tellement :
    quand j'ai un noeud dans la base, rien ne s'affiche dans parent :
    j'ai le choix entre rien et rien d'autre...

  5. #5
    Membre à l'essai
    Homme Profil pro
    Inscrit en
    Juillet 2011
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Juillet 2011
    Messages : 22
    Points : 16
    Points
    16
    Par défaut
    j'ai aussi une autre erreur, qui n'a pas tellement l'air d'être due au controller :
    quand je crée une entité a partir de la page et non directement depuis phpmyadmin, j'ai ca :

    Citation Envoyé par Symfony
    Catchable Fatal Error: Argument 1 passed to MyModelBundle\Entity\AwTree::setParent() must be an instance of MyModelBundle\Entity\AwTree, string given, called in ....../vendor/symfony/src/Symfony/Component/Form/Util/PropertyPath.php on line 346 and defined in ....../src/MyModelBundle/Entity/AwTree.php line 179
    ma ligne 179, c'est celle là :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    public function setParent(AwTree $parent)
    sachant ca :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
        /**
         * @Gedmo\TreeParent
         * @ORM\ManyToOne(targetEntity="AwTree", inversedBy="children")
         * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL")
         */
        private $parent; 
    // parent est une instance de AwTree, ayant pour id celui qui apparaît dans la colonne parent_id de l'instance fille

  6. #6
    Inscrit
    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    319
    Détails du profil
    Informations personnelles :
    Âge : 35
    Localisation : France

    Informations forums :
    Inscription : Décembre 2004
    Messages : 319
    Points : 476
    Points
    476
    Par défaut
    Montre ton formulaire

  7. #7
    Membre à l'essai
    Homme Profil pro
    Inscrit en
    Juillet 2011
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Juillet 2011
    Messages : 22
    Points : 16
    Points
    16
    Par défaut
    c'est bon, found it.

    ancienne version du form :
    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
     
    <?php
     
    namespace MyModelBundle\Form;
     
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilder;
     
    class AwTreeType extends AbstractType
    {
        public function buildForm(FormBuilder $builder, array $options)
        {
            $builder
                ->add('title')
                ->add('parent', 'choice', array('required' => false))
            ;
        }
     
        public function getName()
        {
            return 'mymodelbundle_awtreetype';
        }
    }

    nouvelle version (un tout petit peu plus complète)
    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
     
    <?php
     
    namespace MyModelBundle\Form;
     
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilder;
     
    class AwTreeType extends AbstractType
    {
        public function buildForm(FormBuilder $builder, array $options)
        {
            $builder
                ->add('title')
                ->add('parent', 'entity', array('class'=>'MyModelBundle\Entity\AwTree', 'property'=>'title', 'required' => false))
            ;
        }
     
        public function getName()
        {
            return 'mymodelbundle_awtreetype';
        }
    }

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

Discussions similaires

  1. Champs vides qui n'apparaissent pas
    Par raven75 dans le forum Requêtes et SQL.
    Réponses: 5
    Dernier message: 09/02/2015, 16h21
  2. [MySQL] Champs photo qui ne prends pas les espaces
    Par bullrot dans le forum PHP & Base de données
    Réponses: 9
    Dernier message: 21/10/2008, 15h19
  3. Réponses: 3
    Dernier message: 04/09/2007, 21h44
  4. Requete avec champs calculés qui ne marche pas
    Par The_Super_Steph dans le forum Requêtes et SQL.
    Réponses: 4
    Dernier message: 05/06/2007, 14h39
  5. [Access2000] test si champ vide qui marche pas ...
    Par michaelbob dans le forum Access
    Réponses: 2
    Dernier message: 17/10/2005, 10h46

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