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

 PHP Discussion :

Erreur à la soumission de formulaire Symfony4


Sujet :

PHP

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Janvier 2011
    Messages : 5
    Points : 8
    Points
    8
    Par défaut Erreur à la soumission de formulaire Symfony4
    Bonjour,

    J'ai un formulaire qui demande de télécharger une image, j'essaie de modifier le code de soumission du formulaire pour l'envoyer dans le bon répertoire (et je me demande aussi si mon champ pour uploader l'image fonctionne correctement, j'ai pourtant utilisé la syntaxe officielle), j'ai ce message d'erreur à la soumission :

    Nom : 2020-05-12 14_55_28-Call to a member function guessExtension() on null (500 Internal Server Erro.png
Affichages : 114
Taille : 74,6 Ko

    , qui à à voir avec mon "$job->getLogo()" venant de mon Entité Job.

    Mon code

    Mon entité Job (src/Entity/Job.php)

    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
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
     
    <?php
    namespace App\Entity;
     
    use App\Helper\SlugifyHelper;
    use DateTime;
    use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
    use Doctrine\ORM\Mapping as ORM;
     
    class Job
    {
        // Durée de vie d'une offre en jours
    	public const OFFER_LIFETIME = 30; // en jours
     
        /** @var int */
        private $id;
     
        /** @var Category */
        private $category;
     
        /** @var string */
        private $type;
     
        /** @var string */
        private $company;
     
        /** @var string */
        private $logo;
     
        /** @var string */
        private $url;
     
        /** @var string */
        private $position;
     
        /** @var string */
        private $location;
     
        /** @var string */
        private $description;
     
        /** @var string */
        private $howToApply;
     
        /** @var string */
        private $token;
     
        /** @var bool */
        private $isPublic;
     
        /** @var bool */
        private $isActivated;
     
        /** @var string */
        private $email;
     
        /** @var DateTime */
        private $expiresAt;
     
        /** @var DateTime */
        private $createdAt;
     
        /** @var DateTime */
        private $updatedAt;
     
        /**
         * Get ID
         *
         * @return int
         */
        public function getId(): int
        {
            return $this->id;
        }
     
        /**
         * Get category
         *
         * @return Category
         */
        public function getCategory(): Category
        {
            return $this->category;
        }
     
        /**
         * Set category
         *
         * @param Category $category
         * @return Job
         */
        public function setCategory(Category $category): self
        {
            $this->category = $category;
     
            return $this;
        }
     
        /**
         * Get type
         *
         * @return string
         */
        public function getType(): ?string
        {
            return $this->type;
        }
     
        /**
         * Set type
         *
         * @param string $type
         * @return Job
         */
        public function setType(string $type): self
        {
            $this->type = $type;
     
            return $this;
        }
     
        /**
         * Get company slug
         *
         * @return string
         */
        public function getCompanySlug(): ?string
        {
            return SlugifyHelper::slugify($this->company);
        }
     
        /**
         * Get company
         *
         * @return string
         */
        public function getCompany(): ?string
        {
            return $this->company;
        }
     
        /**
         * Set company
         *
         * @param string $company
         * @return Job
         */
        public function setCompany(string $company): self
        {
            $this->company = $company;
     
            return $this;
        }
     
        /**
         * Get logo
         *
         * @return string
         */
        public function getLogo(): ?string
        {
            return $this->logo;
        }
     
        /**
         * Set logo
         *
         * @param string $logo
         * @return Job
         */
        public function setLogo(string $logo): self
        {
            $this->logo = $logo;
     
            return $this;
        }
     
        /**
         * Get url
         *
         * @return string
         */
        public function getUrl(): ?string
        {
            return $this->url;
        }
     
        /**
         * Set url
         *
         * @param string $url
         * @return Job
         */
        public function setUrl(string $url): self
        {
            $this->url = $url;
     
            return $this;
        }
     
        /**
         * Get position slug
         *
         * @return string
         */
        public function getPositionSlug(): ?string
        {
            return SlugifyHelper::slugify($this->position);
        }
     
        /**
         * Get position
         *
         * @return string
         */
        public function getPosition(): ?string
        {
            return $this->position;
        }
     
        /**
         * Set position
         *
         * @param string $position
         * @return Job
         */
        public function setPosition(string $position): self
        {
            $this->position = $position;
     
            return $this;
        }
     
        /**
         * Get location slug
         *
         * @return string
         */
        public function getLocationSlug(): ?string
        {
            return SlugifyHelper::slugify($this->location);
        }
     
        /**
         * Get location
         *
         * @return string
         */
        public function getLocation(): ?string
        {
            return $this->location;
        }
     
        /**
         * Set location
         *
         * @param string $location
         * @return Job
         */
        public function setLocation(string $location): self
        {
            $this->location = $location;
     
            return $this;
        }
     
        /**
         * Get description
         *
         * @return string
         */
        public function getDescription(): ?string
        {
            return $this->description;
        }
     
        /**
         * Set description
         *
         * @param string $description
         * @return Job
         */
        public function setDescription(string $description): self
        {
            $this->description = $description;
     
            return $this;
        }
     
        /**
         * Get howToApply
         *
         * @return string
         */
        public function getHowToApply(): ?string
        {
            return $this->howToApply;
        }
     
        /**
         * Set howToApply
         *
         * @param string $howToApply
         * @return Job
         */
        public function setHowToApply(string $howToApply): self
        {
            $this->howToApply = $howToApply;
     
            return $this;
        }
     
        /**
         * Get token
         *
         * @return string
         */
        public function getToken(): ?string
        {
            return $this->token;
        }
     
        /**
         * Set token
         *
         * @param string $token
         * @return Job
         */
        public function setToken(string $token): self
        {
            $this->token = $token;
     
            return $this;
        }
     
        /**
         * Get isPublic
         *
         * @return bool
         */
        public function isPublic(): ?bool
        {
            return $this->isPublic;
        }
     
        /**
         * Set isPublic
         *
         * @param bool $isPublic
         * @return Job
         */
        public function setIsPublic(bool $isPublic): self
        {
            $this->isPublic = $isPublic;
     
            return $this;
        }
     
        /**
         * Get isActivated
         *
         * @return bool
         */
        public function isActivated(): ?bool
        {
            return $this->isActivated;
        }
     
        /**
         * Set isActivated
         *
         * @param bool $isActivated
         * @return Job
         */
        public function setIsActivated(bool $isActivated): self
        {
            $this->isActivated = $isActivated;
     
            return $this;
        }
     
        /**
         * Get email
         *
         * @return string
         */
        public function getEmail(): ?string
        {
            return $this->email;
        }
     
        /**
         * Set email
         *
         * @param string $email
         * @return Job
         */
        public function setEmail(string $email): self
        {
            $this->email = $email;
     
            return $this;
        }
     
        /**
         * Get expiresAt
         *
         * @return DateTime
         */
        public function getExpiresAt(): ?DateTime
        {
            return $this->expiresAt;
        }
     
        /**
         * Set expiresAt
         *
         * @param DateTime $expiresAt
         * @return Job
         */
        public function setExpiresAt(DateTime $expiresAt): self
        {
            $this->expiresAt = $expiresAt;
     
            return $this;
        }
     
        /**
         * Get createdAt
         *
         * @return DateTime
         */
        public function getCreatedAt(): DateTime
        {
            return $this->createdAt;
        }
     
        /**
         * Get updateAt
         *
         * @return DateTime
         */
        public function getUpdatedAt(): ?DateTime
        {
            return $this->updatedAt;
        }
     
        /**
         * Set expiresAt
         *
         * @param LifecycleEventArgs $event
         * @return Job
         */
        public function setExpiresAtValue(LifecycleEventArgs $event): self
        {
            // nous remplissons automatiquement la date d'expiration si cette dernière n'a pas été saisie
            // manuellement
            if (!$this->expiresAt) {
                $this->expiresAt = new DateTime('+'.self::OFFER_LIFETIME.' day');
            }
     
            return $this;
        }
     
        /**
         * Set createdAt
         *
         * @param LifecycleEventArgs $event
         * @return Job
         */
        public function setCreatedAtValue(LifecycleEventArgs $event): self
        {
            $this->createdAt = new DateTime();
     
            return $this;
        }
     
        /**
         * Set updatedAt
         *
         * @param LifecycleEventArgs $event
         * @return Job
         */
        public function setUpdatedAtValue(LifecycleEventArgs $event): self
        {
            $this->updatedAt = new DateTime();
     
            return $this;
        }
    }
    Mon formulaire (src/Form/JobformType.php)

    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
     
    <?php
    namespace App\Form;
     
    use App\Entity\Job;
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolver;
    use Symfony\Component\Form\Extension\Core\Type\FileType;
    use Symfony\Component\Form\Extension\Core\Type\SubmitType;
    use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
     
    class JobformType extends AbstractType
    {
       	public function buildForm(FormBuilderInterface $builder, array $options)
        {
    		$builder->add('type', ChoiceType::class, array(
    					'label' => 'Status',
    					'choices' => array(
    					'temps plein' => 'full-time',
    					'temps partiel' => 'part-time',
    					'freelance' => 'freelance'
    				)))
    				->add('company', null, array('label' => "Société ou nom d'indépendant"))
    				->add('logo', FileType::class, array(
    					'mapped' => false,
    					'label' => 'Téléchargez votre logo'
    				))
    				->add('url', null, array('label' => 'Lien'))
    				->add('position', null, array('label' => 'Profession'))
    				->add('location', null, array('label' => 'Localisation'))
    				->add('description', null, array('label' => "Description de votre activité"))
    				->add('HowToApply', null, array('label' => "Message aux utilisateurs"))
    				->add('email', null, array('label' => 'Email'))
    				->add('envoyer', SubmitType::class)
    				->getForm(); 
     
        }
     
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults([
                'data_class' => Job::class,
            ]);
        }
    }
    Mon controlleur (le code commenté est pour essayer de faire une pagination sans exécuter de commande, si vous avez une solution, je suis aussi preneur) (src/Controller/JobController.php)

    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
     
    <?php
    namespace App\Controller;
    use App\Entity\Category;
    use App\Entity\Job;
    use App\Form\JobformType;
    use DateTime;
    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Component\Validator\Constraints as Assert;
    use Doctrine\ORM\EntityManagerInterface;
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
    use Symfony\Component\HttpFoundation\Request;
     
    class JobController extends AbstractController
    {
        public function index()
        {
    		return $this->render('job/index.html.twig');	
        }
     
    	public function home(){
    		 return $this->render('job/home.html.twig');
    	}
     
    	public function profile(EntityManagerInterface $em){
    		$jobs = $em->getRepository(Job::class)->findAll();
     
    		/*$jobParPage = 5;
    		$jobTotal = count($jobs);
            $pagesTotales = ceil($jobTotal/$jobParPage);
    		
    		if(isset($_GET['page']) AND !empty($_GET['page']) AND $_GET['page'] > 0 AND $_GET['page'] <= $pagesTotales)
    		{
    			$_GET['page'] = intval($_GET['page']);
    			$pageCourante = $_GET['page'];         
    		} else{
    			$pageCourante = 1;
    		}*/
     
    		return $this->render('job/profils.html.twig', [
                'jobs' => $jobs,
            ]);
    	}	
     
    	public function register(Request $request){
    		$job = new Job();	
     
    		$form = $this->createForm(JobformType::class, $job);
     
    		$form->handleRequest($request); 
     
    		if($form->isSubmitted() && $form->isValid()){ 			
    			$file = $request->files->get('post')['logo'];
    			$fileName = md5(uniqid()).'.'.$job->getLogo()->guessExtension();
    			$file->move($this->getParameter('upload_directory'));
     
    			$job->setToken(random_bytes(10));
    			$job->setIsPublic(true);
    			$job->setIsActivated(true);
     
    			$entityManager = $this->getDoctrine()->getManager();
     
    			$entityManager->persist($job);
    			$entityManager->flush();
     
    			return $this->render('job/home.html.twig'); 
    		} else { 				
    			return $this->render('job/registration.html.twig', [
    				'form_create_job' => $form->createView(),
    			]);
    		} 	
     
    	}
     
        public function show(EntityManagerInterface $em, int $id, string $company, string $location, string $position) : Response
        {
            // dans un projet réel, il sera nécessaire de faire une requête permettant de vérifier que tous les éléments
            // correspondent à une offre d'emploi valide
            $job = $em->getRepository(Job::class)->find($id);
     
            if (null === $job) {
                throw new NotFoundHttpException();
            }
     
            $currentDate = new DateTime();		
     
            return $this->render('job/show.html.twig', [
                'job' => $job,
            ]);
        }
    }
    Merci d'avance.

  2. #2
    Membre expert
    Avatar de Alexandre T
    Homme Profil pro
    Chef de projets AMO
    Inscrit en
    Mai 2002
    Messages
    1 213
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Chef de projets AMO
    Secteur : Transports

    Informations forums :
    Inscription : Mai 2002
    Messages : 1 213
    Points : 3 001
    Points
    3 001
    Par défaut
    Bonjour,

    Visiblement, Job est une entité liée par un ManyToOne, un oneToOne. Tu n'utilises pas le mapping par annotation, il faudrait que tu nous communiques le fichier yaml ou le fichier xml si tu es toujours bloqué.

    Mais à mon avis, ton problème vient de la configuration du FETCH de Doctrine. Par défaut, Doctrine ne charge pas le logo de ton job.

    Voici la configuration par défaut (désolé, j'ai l'habitude du mode Annotation):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    /**
     * @ManyToOne(targetEntity="Logo", cascade={"all"}, fetch="LAZY")
     */
    Dans ce cas, Lazy pour fainéant, Doctrine ne fait pas de jointure avec la table logo dans la requête DQL, du coût après hydratation logo est null.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    /**
     * @ManyToOne(targetEntity="Logo", cascade={"all"}, fetch="EAGER")
     */
    Dans ce cas, EAGER , Doctrine fait la jointure avec la table logo dans la requête DQL, du coût après hydratation logo est est instancié et utilisable.
    Alexandre Tranchant
    Chef de projet AMO pour le Cerema.
    Retrouvez mes articles sur PHP et Symfony

Discussions similaires

  1. Erreur à la soumission d'un formulaire
    Par Shanna666 dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 20/06/2012, 11h15
  2. Formulaire de contact : erreur aprés soumission
    Par Erwan31 dans le forum Langage
    Réponses: 13
    Dernier message: 21/09/2009, 18h47
  3. message d'erreur avant la soumission du formulaire
    Par ramzos82 dans le forum Struts 1
    Réponses: 2
    Dernier message: 01/05/2007, 13h28
  4. Erreur de soumission d'un formulaire
    Par sagitarium dans le forum Langage
    Réponses: 1
    Dernier message: 14/05/2006, 12h21
  5. Erreur dans un sous formulaire
    Par Didi17 dans le forum Access
    Réponses: 4
    Dernier message: 14/11/2005, 08h41

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