Supprimer saut de ligne de la BDD php /symfony3

Résolu
dellai.najeh - 25 oct. 2018 à 22:58
 nejeh - 26 oct. 2018 à 09:36
Bonsoir,

merci de me donne l'aide j'ai passé toute la journée à la recherche et les essaies mais je n'arrive à rien.

en fait j'ai un champs de textarea de type text, lors de l'insertion d'une valeur NULL ou vide dans la BDD mysql, je trouve un <br> comme valeur dans ce champ.
comment puis-je le rendre NULL?
merci d'avance

voilà mon code : ce code a pu remplace le <br> par NULL mais le problème ici est quand j'écrit une valeur non null il insère aussi NULL

/**
     * Finds and displays an Promoter entity.
     *
     * @Route("/{id}/update", name="edit_project")
     * @Template("FormationMiniPBundle:Project:edit.html.twig")
     */
    public function updateAction(Project $project, Request $request) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('FormationMiniPBundle:Project')->find($project);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Project entity.');
        }


        $editForm = $this->createForm('Formation\MiniPBundle\Form\ProjectType', $entity);
        // $deleteForm = $this->createDeleteForm($id);
        /*
          $request = $this->getRequest();

          $editForm->bindRequest($request); */
         $editForm->handleRequest($request);
         $newp=$editForm->get('newPrice')->getData();
         var_dump($newp);
         $newt=$editForm->get('newText')->getData();
//         var_dump($newt);

        if (($editForm->isSubmitted()) && ($editForm->isValid())) {
            $mystring1 = "/<br>/";
            $pos1 = stripos($mystring1, $newp);
            $pos2 = stripos($mystring1, $newt);

               if($pos1==false){
                   $entity->setNewPrice(null);
                   var_dump( $entity->setNewPrice(null));
               }
               else{
                   echo "text";
                   $entity->setNewPrice($newp);
               }

            if($pos2==false){
                $entity->setNewText(null);
            }else{
            $entity->setNewText($newt);
}

            exit;
            $em->persist($entity);
            $em->flush();
            return $this->redirectToRoute('index_project', array('project' => $project->getId()));
        }

        return $this->render('FormationMiniPBundle:Project:edit.html.twig', array(
            'project' => $project,
            'edit_form' => $editForm->createView(),
            // 'delete_form' => $deleteForm->createView(),
        ));
    }





2 réponses

jordane45 Messages postés 38151 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 2 mai 2024 344
26 oct. 2018 à 07:14
Bonjour
Comment as tu écris ton textarea ?
Tu ne dois pas mettre de retour a la ligne avant le </textarea>
0
Bonjour,
je vous remercie pour ta réponse , voilà la solution si quelqu'un rencontrerais ce problème il peut l'utiliser. Vous pouver le mettre " Résolu".

   public function updateAction(Project $project, Request $request) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('FormationMiniPBundle:Project')->find($project);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Project entity.');
        }


        $editForm = $this->createForm('Formation\MiniPBundle\Form\ProjectType', $entity);
   
         $editForm->handleRequest($request);
         $newp=$editForm->get('newPrice')->getData();
  
         $newt=$editForm->get('newText')->getData();

        if (($editForm->isSubmitted()) && ($editForm->isValid())) {
            $mystring1 = "<br>";
  
            if (strcmp($newp, $mystring1) == 0) {
                $entity->setNewPrice(null);
            }
            if (strcmp($newt, $mystring1) == 0) {
                $entity->setNewText(null);
            }

            $em->persist($entity);
            $em->flush();
            return $this->redirectToRoute('index_project', array('project' => $project->getId()));
        }

        return $this->render('FormationMiniPBundle:Project:edit.html.twig', array(
            'project' => $project,
            'edit_form' => $editForm->createView(),
            // 'delete_form' => $deleteForm->createView(),
        ));
    }
0
Rejoignez-nous