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 :

erreur validation formulaire FosUserBundle


Sujet :

Symfony PHP

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    501
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 501
    Points : 102
    Points
    102
    Par défaut erreur validation formulaire FosUserBundle
    Bonjour, petit soucie de formulaire FosUserBundle

    Donc voila quand j'enregistre un nouvelle utilisateur via mon formulaire d' enregistrement (methode registerAction()), je voudrais qu'après soumission du formulaire récupérer les erreurs :

    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
        
          public function registerAction(Request $request)
        {
            $errors= null;
            /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
            $formFactory = $this->container->get('fos_user.registration.form.factory');
            /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
            $userManager = $this->container->get('fos_user.user_manager');
            /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
            $dispatcher = $this->container->get('event_dispatcher');
    
            $user = $userManager->createUser();
            $user->setEnabled(true);
    
            $event = new GetResponseUserEvent($user, $request);
            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);
    
            if (null !== $event->getResponse()) {
                return $event->getResponse();
            }
    
            $form = $formFactory->createForm();
            $form->setData($user);
    
               
            if ('POST' === $request->getMethod()) {
           
                $form->bind($request);
                     
                  var_dump($form->getErrors());exit;
                  var_dump($form->getErrorsAsString());exit;
    
                if ($form->isValid()) {
                    
                      
                    $event = new FormEvent($form, $request);
                    $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
    
                    $userManager->updateUser($user);
    
       
                    if (null === $response = $event->getResponse()) {
                        $url = $this->container->get('router')->generate('fos_user_registration_confirmed');
                        $response = new RedirectResponse($url);
                    }
    
                    $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
    
                    return $response;
                }else{
    
                       $validator = $this->container->get('validator');
                       $errors = $validator->validate($user);
                       var_dump($errors);exit;
                  }
                
            }
    
            return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
                'form' => $form->createView(),
            ));
        }
    alors je me trouve face à une mascarade, quand je fais un var_dump de $form->getErrors(), il y a aucune erreur pourtant OUI il y a bien des erreurs car je ne peut pas valider (methode isValid() ) et le var_dump de getErrorsAsString() me renvoye bien mes erreurs.

    De plus si j'utilise le code ci dessous

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
             $validator = $this->container->get('validator');
                       $errors = $validator->validate($user);
                       var_dump($errors);exit;
    aucune erreur egalement

    Une idée les amis

    Merci

  2. #2
    Membre expert
    Avatar de dukoid
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2012
    Messages
    2 100
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2012
    Messages : 2 100
    Points : 3 004
    Points
    3 004
    Par défaut
    salut jean paul,

    bon pour ton soucis, quel genre d'erreur tu as ?
    c'es pas un problème de CRSF ?

  3. #3
    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,

    As tu regardé du coté des groupes de validation
    http://symfony.com/fr/doc/current/bo...-de-validation

    Le FOSUserBundle utilise plusieurs groupe de validation.
    https://github.com/FriendsOfSymfony/...validation.xml

  4. #4
    Membre expérimenté Avatar de Nico_F
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2011
    Messages
    728
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Avril 2011
    Messages : 728
    Points : 1 310
    Points
    1 310
    Par défaut
    Hello, si le getErrorsAsString() te renvoie bien tes erreurs, peux-tu nous les montrer stp ?

  5. #5
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    501
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 501
    Points : 102
    Points
    102
    Par défaut re
    non pas CRSF

    dans mes autres formulaires je peux manipuler mes erreurs comme bon me semble, avec

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    $validator = $this->container->get('validator');
                       $errors = $validator->validate($user);
                       var_dump($errors);exit;
    ou
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
      var_dump($form->getErrors());exit;
    [/CODE]

    je veux afficher mes erreur en utilisant:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
              $validator = $this->container->get('validator');
                       $errors = $validator->validate($user);
                       var_dump($errors);exit;
    mais ca me renvoie

    array (size=0)
    empty

    alors que avec

    var_dump($form->getErrorsAsString());exit;

    j'ai bien
    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
    string 'nom:
        No errors
    prenom:
        No errors
    adrfacturation:
        No errors
    codeadrf:
        No errors
    villeadrf:
        No errors
    adrlivraison:
        No errors
    codeadrl:
        No errors
    villeadrl:
        No errors
    email:
        ERROR: L'adresse e-mail est déjà utilisée
    username:
        No errors
    plainPassword:
        first:
            No errors
        second:
            No errors
    news:
        No errors
    ' (length=371)
    Comprend tu on je veux en venir, je veux pas corriger mes erreur elle sont volontaire pour l'exmple, je veux le manipuler pour ensuite afficher comme bon me semeble dans mon template.

    Merci l'ami

  6. #6
    Membre expérimenté Avatar de Nico_F
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2011
    Messages
    728
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Avril 2011
    Messages : 728
    Points : 1 310
    Points
    1 310
    Par défaut
    Un petit dump de l'objet $validator aussi stp ?

  7. #7
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    501
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 501
    Points : 102
    Points
    102
    Par défaut re
    voila l'ami

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    object(Symfony\Component\Validator\ConstraintViolationList)[506]
      private 'violations' => 
        array (size=0)
          empty
    Merci

  8. #8
    Membre expérimenté Avatar de Nico_F
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2011
    Messages
    728
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Avril 2011
    Messages : 728
    Points : 1 310
    Points
    1 310
    Par défaut
    Intéressant : ton $validator n'est pas une instance d'une classe Validator. :/

    Tu as copié tout le dump ou uniquement la partie des violations ? Parce que si tu as tout dumpé la méthode validate() ne devrait même pas être accessible.

    Dans le doute et pour éviter de creuser des pistes inutilement : quelle est la version de Symfony que tu utilises ?

  9. #9
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    501
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 501
    Points : 102
    Points
    102
    Par défaut re
    le dump de cecie

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    $validator = $this->container->get('validator');
                       $errors = $validator->validate($user);
                       var_dump($errors);exit;
    me donne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    object(Symfony\Component\Validator\ConstraintViolationList)[506]
      private 'violations' => 
        array (size=0)
          empty
    j'ai symfony2.3

    mais c'est pas le porobleme car dans mes autre formulaire j'ai acces a mes erreurs

  10. #10
    Membre expérimenté Avatar de Nico_F
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2011
    Messages
    728
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Avril 2011
    Messages : 728
    Points : 1 310
    Points
    1 310
    Par défaut
    Pour la version de symfony je me doute que ce n'est pas le problème, c'est juste pour m'assurer qu'on ait les mêmes classes Symfony de base.

    Peut-être que tu peux nous montrer à quoi ressemble un contrôleur similaire qui fonctionne pour voir s'il y a des différences qui t'auraient échappé.

    Je réitère : c'est bien le dump de $validator que je veux voir : pas celui de la variable $errors.

  11. #11
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    501
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 501
    Points : 102
    Points
    102
    Par défaut re
    Salut les copains, donc pour ce vendredi voici donc:

    le dump de l'objet validators demandais pas Nico_F

    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
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
     
    object(Symfony\Component\Validator\Validator)[326]
      private 'metadataFactory' => 
        object(Symfony\Component\Validator\Mapping\ClassMetadataFactory)[327]
          protected 'loader' => 
            object(Symfony\Component\Validator\Mapping\Loader\LoaderChain)[328]
              protected 'loaders' => 
                array (size=3)
                  ...
          protected 'cache' => null
          protected 'loadedClasses' => 
            array (size=14)
              'Symfony\Component\Security\Core\User\UserInterface' => 
                object(Symfony\Component\Validator\Mapping\ClassMetadata)[450]
                  ...
              'Symfony\Component\Security\Core\User\AdvancedUserInterface' => 
                object(Symfony\Component\Validator\Mapping\ClassMetadata)[447]
                  ...
              'Serializable' => 
                object(Symfony\Component\Validator\Mapping\ClassMetadata)[449]
                  ...
              'FOS\UserBundle\Model\UserInterface' => 
                object(Symfony\Component\Validator\Mapping\ClassMetadata)[442]
                  ...
              'FOS\UserBundle\Model\GroupableInterface' => 
                object(Symfony\Component\Validator\Mapping\ClassMetadata)[446]
                  ...
              'FOS\UserBundle\Model\User' => 
                object(Symfony\Component\Validator\Mapping\ClassMetadata)[435]
                  ...
              'Ecommerce\UserBundle\Entity\User' => 
                object(Symfony\Component\Validator\Mapping\ClassMetadata)[432]
                  ...
              'Traversable' => 
                object(Symfony\Component\Validator\Mapping\ClassMetadata)[569]
                  ...
              'IteratorAggregate' => 
                object(Symfony\Component\Validator\Mapping\ClassMetadata)[370]
                  ...
              'ArrayAccess' => 
                object(Symfony\Component\Validator\Mapping\ClassMetadata)[591]
                  ...
              'Countable' => 
                object(Symfony\Component\Validator\Mapping\ClassMetadata)[592]
                  ...
              'Symfony\Component\Form\FormInterface' => 
                object(Symfony\Component\Validator\Mapping\ClassMetadata)[571]
                  ...
              'Symfony\Component\Form\Form' => 
                object(Symfony\Component\Validator\Mapping\ClassMetadata)[551]
                  ...
              'Symfony\Component\Form\Util\OrderedHashMap' => 
                object(Symfony\Component\Validator\Mapping\ClassMetadata)[398]
                  ...
      private 'validatorFactory' => 
        object(Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory)[336]
          protected 'container' => 
            object(appDevDebugProjectContainer)[306]
              protected 'parameterBag' => null
              protected 'services' => 
                array (size=90)
                  ...
              protected 'methodMap' => 
                array (size=283)
                  ...
              protected 'aliases' => 
                array (size=11)
                  ...
              protected 'scopes' => 
                array (size=1)
                  ...
              protected 'scopeChildren' => 
                array (size=1)
                  ...
              protected 'scopedServices' => 
                array (size=1)
                  ...
              protected 'scopeStacks' => 
                array (size=0)
                  ...
              protected 'loading' => 
                array (size=0)
                  ...
              public 'parameters' => 
                array (size=514)
                  ...
          protected 'validators' => 
            array (size=6)
              'security.validator.user_password' => string 'security.validator.user_password' (length=32)
              'doctrine.orm.validator.unique' => 
                object(Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator)[585]
                  ...
              'Symfony\Component\Form\Extension\Validator\Constraints\FormValidator' => 
                object(Symfony\Component\Form\Extension\Validator\Constraints\FormValidator)[595]
                  ...
              'Symfony\Component\Validator\Constraints\NotBlankValidator' => 
                object(Symfony\Component\Validator\Constraints\NotBlankValidator)[639]
                  ...
              'Symfony\Component\Validator\Constraints\LengthValidator' => 
                object(Symfony\Component\Validator\Constraints\LengthValidator)[638]
                  ...
              'Symfony\Component\Validator\Constraints\EmailValidator' => 
                object(Symfony\Component\Validator\Constraints\EmailValidator)[645]
                  ...
      private 'translator' => 
        object(Symfony\Bundle\FrameworkBundle\Translation\Translator)[74]
          protected 'container' => 
            object(appDevDebugProjectContainer)[306]
              protected 'parameterBag' => null
              protected 'services' => 
                array (size=90)
                  ...
              protected 'methodMap' => 
                array (size=283)
                  ...
              protected 'aliases' => 
                array (size=11)
                  ...
              protected 'scopes' => 
                array (size=1)
                  ...
              protected 'scopeChildren' => 
                array (size=1)
                  ...
              protected 'scopedServices' => 
                array (size=1)
                  ...
              protected 'scopeStacks' => 
                array (size=0)
                  ...
              protected 'loading' => 
                array (size=0)
                  ...
              public 'parameters' => 
                array (size=514)
                  ...
          protected 'options' => 
            array (size=2)
              'cache_dir' => string 'F:/projets/Symfony/app/cache/dev/translations' (length=45)
              'debug' => boolean true
          protected 'loaderIds' => 
            array (size=10)
              'translation.loader.php' => 
                array (size=1)
                  ...
              'translation.loader.yml' => 
                array (size=1)
                  ...
              'translation.loader.xliff' => 
                array (size=2)
                  ...
              'translation.loader.po' => 
                array (size=1)
                  ...
              'translation.loader.mo' => 
                array (size=1)
                  ...
              'translation.loader.qt' => 
                array (size=1)
                  ...
              'translation.loader.csv' => 
                array (size=1)
                  ...
              'translation.loader.res' => 
                array (size=1)
                  ...
              'translation.loader.dat' => 
                array (size=1)
                  ...
              'translation.loader.ini' => 
                array (size=1)
                  ...
          protected 'catalogues' => 
            array (size=1)
              'fr' => 
                object(Symfony\Component\Translation\MessageCatalogue)[646]
                  ...
          protected 'locale' => string 'fr' (length=2)
          private 'fallbackLocales' (Symfony\Component\Translation\Translator) => 
            array (size=1)
              0 => string 'fr' (length=2)
          private 'loaders' (Symfony\Component\Translation\Translator) => 
            array (size=0)
              empty
          private 'resources' (Symfony\Component\Translation\Translator) => 
            array (size=49)
              'af' => 
                array (size=1)
                  ...
              'ar' => 
                array (size=6)
                  ...
              'bg' => 
                array (size=6)
                  ...
              'ca' => 
                array (size=7)
                  ...
              'cs' => 
                array (size=8)
                  ...
              'cy' => 
                array (size=1)
                  ...
              'da' => 
                array (size=8)
                  ...
              'de' => 
                array (size=11)
                  ...
              'el' => 
                array (size=3)
                  ...
              'en' => 
                array (size=13)
                  ...
              'es' => 
                array (size=9)
                  ...
              'et' => 
                array (size=4)
                  ...
              'eu' => 
                array (size=2)
                  ...
              'fa' => 
                array (size=7)
                  ...
              'fi' => 
                array (size=6)
                  ...
              'fr' => 
                array (size=18)
                  ...
              'gl' => 
                array (size=3)
                  ...
              'he' => 
                array (size=4)
                  ...
              'hr' => 
                array (size=6)
                  ...
              'hu' => 
                array (size=7)
                  ...
              'hy' => 
                array (size=2)
                  ...
              'id' => 
                array (size=4)
                  ...
              'it' => 
                array (size=8)
                  ...
              'ja' => 
                array (size=8)
                  ...
              'lb' => 
                array (size=5)
                  ...
              'lt' => 
                array (size=6)
                  ...
              'mn' => 
                array (size=2)
                  ...
              'nb' => 
                array (size=4)
                  ...
              'nl' => 
                array (size=9)
                  ...
              'no' => 
                array (size=2)
                  ...
              'pl' => 
                array (size=9)
                  ...
              'pt' => 
                array (size=5)
                  ...
              'pt_BR' => 
                array (size=8)
                  ...
              'ro' => 
                array (size=6)
                  ...
              'ru' => 
                array (size=10)
                  ...
              'sk' => 
                array (size=8)
                  ...
              'sl' => 
                array (size=7)
                  ...
              'sq' => 
                array (size=1)
                  ...
              'sr_Cyrl' => 
                array (size=4)
                  ...
              'sr_Latn' => 
                array (size=8)
                  ...
              'sv' => 
                array (size=7)
                  ...
              'tr' => 
                array (size=8)
                  ...
              'uk' => 
                array (size=6)
                  ...
              'zh_CN' => 
                array (size=7)
                  ...
              'lv' => 
                array (size=5)
                  ...
              'pt_PT' => 
                array (size=3)
                  ...
              'ua' => 
                array (size=2)
                  ...
              'az' => 
                array (size=1)
                  ...
              'vi' => 
                array (size=2)
                  ...
          private 'selector' (Symfony\Component\Translation\Translator) => 
            object(Symfony\Component\Translation\MessageSelector)[73]
      private 'translationDomain' => string 'validators' (length=10)
      private 'objectInitializers' => 
        array (size=2)
          0 => 
            object(Symfony\Bridge\Doctrine\Validator\DoctrineInitializer)[337]
              protected 'registry' => 
                object(Doctrine\Bundle\DoctrineBundle\Registry)[290]
                  ...
          1 => 
            object(FOS\UserBundle\Validator\Initializer)[338]
              private 'userManager' => 
                object(FOS\UserBundle\Doctrine\UserManager)[271]
                  ...
    De plus un exemple d'un controller ou je manipule moi même les erreur au lieu de l'aient laissé ce gérer par l'objet formulaire
    Donc ci-dessous un formulaire de contact trés simple avec les erreurs passer au template dan la variables $errors
    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
     
    public function contactAction(Request $request) {
     
        $errors=null;
     
        $em = $this->getDoctrine()->getManager();
     
        //$contact = $em->getRepository('EcommerceAdminBundle:Contact')->findOneById(2);
     
        $form = $this->get('form_contact.contact.form');
        //$form = $this->createForm('contact_form',$contact);
     
        $contact = new Contact();
     
        //associé a add_data du model de donnée du form que l'objet $contact soit vide ou existant
        $form->setData($contact);
     
     
             if ($request->getMethod() == 'POST') {
     
               $form->handleRequest($request);  
     
               $contact=$form->getData();
               //var_dump($form->getData());exit;
               //$tab=$request->request->get('contact_form');
              //\var_dump($form->get('name')->getData());exit;
               //renvoit une instance de la classe Ecommerce\AdminBundle\Entity\Contact
               // en cohérence avec l'option data_class de Ecommerce\AdminBundle\Form\Type\ContactType
              // $contact=$form->getData();
               //var_dump($contact);exit;
              //var_dump($form->getErrorsAsString());exit;
                  if ($form->isValid()) {
     
                       $em = $this->getDoctrine()->getManager();
                       $em->persist($form->getData());
                       $em->flush();
     
                  }else{
     
                       $validator = $this->get('validator');
                       $errors = $validator->validate($contact);
                  }
              }
     
                          return $this->render('EcommerceAdminBundle:Contact:contact.html.twig',
                          array( 'form'=> $form->createView(),'errors'=>$errors ));
    }
    }
    Merci

  12. #12
    Membre expérimenté Avatar de Nico_F
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2011
    Messages
    728
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Avril 2011
    Messages : 728
    Points : 1 310
    Points
    1 310
    Par défaut
    Et en faisant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    // $errors = $validator->validate($user);
    $errors = $validator->validate($form->getData());

  13. #13
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    501
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 501
    Points : 102
    Points
    102
    Par défaut
    toujours vide
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    object(Symfony\Component\Validator\ConstraintViolationList)[506]
      private 'violations' => 
        array (size=0)
          empty

  14. #14
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    501
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 501
    Points : 102
    Points
    102
    Par défaut
    Pourtant j'ai bien mes objets:

    var_dump($form->getData());
    var_dump($user);

    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
    object(Ecommerce\UserBundle\Entity\User)[317]
      protected 'id' => null
      private 'nom' => string 'l' (length=1)
      private 'prenom' => string 'l' (length=1)
      private 'adrfacturation' => string 'l' (length=1)
      private 'codeadrf' => string 'l' (length=1)
      private 'villeadrf' => string 'ff' (length=2)
      private 'adrlivraison' => string 'l' (length=1)
      private 'codeadrl' => string 'll' (length=2)
      private 'villeadrl' => string 'fd' (length=2)
      private 'news' => boolean false
      private 'commandes' => null
      protected 'username' => string 'lgfgf' (length=5)
      protected 'usernameCanonical' => string 'lgfgf' (length=5)
      protected 'email' => string 'lgacquere@free.fr' (length=17)
      protected 'emailCanonical' => string 'lgacquere@free.fr' (length=17)
      protected 'enabled' => boolean true
      protected 'salt' => string '5ozfjz9j794wk8gsk0ow08sg0wwk8os' (length=31)
      protected 'password' => null
      protected 'plainPassword' => string 'lo' (length=2)
      protected 'lastLogin' => null
      protected 'confirmationToken' => null
      protected 'passwordRequestedAt' => null
      protected 'groups' => null
      protected 'locked' => boolean false
      protected 'expired' => boolean false
      protected 'expiresAt' => null
      protected 'roles' => 
        array (size=0)
          empty
      protected 'credentialsExpired' => boolean false
      protected 'credentialsExpireAt' => null
     
     
    object(Ecommerce\UserBundle\Entity\User)[317]
      protected 'id' => null
      private 'nom' => string 'l' (length=1)
      private 'prenom' => string 'l' (length=1)
      private 'adrfacturation' => string 'l' (length=1)
      private 'codeadrf' => string 'l' (length=1)
      private 'villeadrf' => string 'ff' (length=2)
      private 'adrlivraison' => string 'l' (length=1)
      private 'codeadrl' => string 'll' (length=2)
      private 'villeadrl' => string 'fd' (length=2)
      private 'news' => boolean false
      private 'commandes' => null
      protected 'username' => string 'lgfgf' (length=5)
      protected 'usernameCanonical' => string 'lgfgf' (length=5)
      protected 'email' => string 'lgacquere@free.fr' (length=17)
      protected 'emailCanonical' => string 'lgacquere@free.fr' (length=17)
      protected 'enabled' => boolean true
      protected 'salt' => string '5ozfjz9j794wk8gsk0ow08sg0wwk8os' (length=31)
      protected 'password' => null
      protected 'plainPassword' => string 'lo' (length=2)
      protected 'lastLogin' => null
      protected 'confirmationToken' => null
      protected 'passwordRequestedAt' => null
      protected 'groups' => null
      protected 'locked' => boolean false
      protected 'expired' => boolean false
      protected 'expiresAt' => null
      protected 'roles' => 
        array (size=0)
          empty
      protected 'credentialsExpired' => boolean false
      protected 'credentialsExpireAt' => null

Discussions similaires

  1. Erreur validation de formulaire
    Par cecjmii dans le forum Access
    Réponses: 4
    Dernier message: 20/03/2015, 17h23
  2. Réponses: 6
    Dernier message: 24/04/2013, 17h14
  3. erreur validation formulaire
    Par lolie dans le forum ASP.NET MVC
    Réponses: 1
    Dernier message: 09/10/2011, 02h20
  4. Validation formulaire message erreur
    Par fouf_01 dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 18/05/2009, 08h52
  5. gestion des erreurs (validation de formulaires)
    Par mattyeux dans le forum Ruby on Rails
    Réponses: 6
    Dernier message: 26/11/2007, 07h53

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