Last active
July 24, 2018 12:26
-
-
Save RaphaelBlehoue/9bd2306d2af955c239ff0442053a5d29 to your computer and use it in GitHub Desktop.
Problème avec la sérialisation (JMS Sérializer )des données en poster en Ajax avec un Formulaire symfony 4 - Erreur Afficher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$form_company.submit( function(e){ | |
e.preventDefault(); | |
const url = $(this).attr('action'); | |
const data = $(this).serializeArray(); | |
console.log(data); | |
$.ajax({ | |
url: url, | |
dataType: 'json', | |
type: 'post', | |
contentType: 'application/json', | |
data: data, | |
error: function( jqXhr, textStatus, errorThrown ){ | |
console.log( errorThrown ); | |
} | |
}).done(function (data, textStatus, jQxhr) { | |
console.log(data); | |
console.log(textStatus); | |
console.log(jQxhr); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Entity; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Doctrine\Common\Collections\Collection; | |
use Doctrine\ORM\Mapping as ORM; | |
use Gedmo\Mapping\Annotation as Gedmo; | |
use Symfony\Component\Validator\Constraints AS Assert; | |
/** | |
* @ORM\Entity(repositoryClass="App\Repository\CompanyRepository") | |
*/ | |
class Company | |
{ | |
/** | |
* @ORM\Id() | |
* @ORM\GeneratedValue() | |
* @ORM\Column(type="integer") | |
*/ | |
protected $id; | |
/** | |
* @ORM\Column(type="string", length=255) | |
* @Assert\NotNull(message="Entrez le nom de l'entreprise") | |
*/ | |
protected $name; | |
/** | |
* @ORM\Column(type="string", length=255, nullable=true) | |
* @Assert\NotNull(message="Entrez le siège social de l'entreprise") | |
*/ | |
protected $seat; | |
/** | |
* @var Bool | |
* @ORM\Column(type="boolean", nullable=true) | |
*/ | |
protected $is_reinforcement; | |
/** | |
* @var Bool | |
* @ORM\Column(type="boolean", nullable=true) | |
*/ | |
protected $is_satisfied; | |
/** | |
* @var | |
* @ORM\Column(type="text", nullable=true) | |
*/ | |
protected $comment_reinforcement; | |
/** | |
* @ORM\Column(type="string", length=255, nullable=true) | |
* @Gedmo\Slug(fields={"name", "id"}, separator="_", updatable=false) | |
*/ | |
protected $slug; | |
/** | |
* @var | |
* @ORM\ManyToOne(targetEntity="App\Entity\Domain", inversedBy="companies") | |
* @Assert\NotBlank(message="Selectionnez le domaine d'activité") | |
*/ | |
protected $domain; | |
/** | |
* @var | |
* @ORM\ManyToOne(targetEntity="App\Entity\Legal", inversedBy="companies") | |
* @Assert\NotBlank(message="Selectionnez la forme juridique") | |
*/ | |
protected $legal; | |
/** | |
* @var | |
* @ORM\OneToMany(targetEntity="App\Entity\Shareholder", mappedBy="company", cascade={"persist","remove"}) | |
* | |
*/ | |
protected $shareholders; | |
/** | |
* @var | |
* @ORM\OneToMany( | |
* targetEntity="App\Entity\Department", | |
* mappedBy="company", | |
* fetch="EXTRA_LAZY", | |
* orphanRemoval=true, | |
* cascade={"persist","remove"} | |
* ) | |
* | |
*/ | |
protected $departments; | |
public function __construct() | |
{ | |
$this->shareholders = new ArrayCollection(); | |
$this->departments = new ArrayCollection(); | |
} | |
public function getId() | |
{ | |
return $this->id; | |
} | |
public function getName(): ?string | |
{ | |
return $this->name; | |
} | |
public function setName(string $name): self | |
{ | |
$this->name = $name; | |
return $this; | |
} | |
public function getSeat(): ?string | |
{ | |
return $this->seat; | |
} | |
public function setSeat(?string $seat): self | |
{ | |
$this->seat = $seat; | |
return $this; | |
} | |
public function setWebsite(?string $website): self | |
{ | |
$this->website = $website; | |
return $this; | |
} | |
public function getSlug(): ?string | |
{ | |
return $this->slug; | |
} | |
public function setSlug(?string $slug): self | |
{ | |
$this->slug = $slug; | |
return $this; | |
} | |
public function getDomain(): ?Domain | |
{ | |
return $this->domain; | |
} | |
public function setDomain(?Domain $domain): self | |
{ | |
$this->domain = $domain; | |
return $this; | |
} | |
public function getLegal(): ?Legal | |
{ | |
return $this->legal; | |
} | |
public function setLegal(?Legal $legal): self | |
{ | |
$this->legal = $legal; | |
return $this; | |
} | |
/** | |
* @return Collection|Shareholder[] | |
*/ | |
public function getShareholders(): Collection | |
{ | |
return $this->shareholders; | |
} | |
public function addShareholder(Shareholder $shareholder): self | |
{ | |
if (!$this->shareholders->contains($shareholder)) { | |
$this->shareholders[] = $shareholder; | |
$shareholder->setCompany($this); | |
} | |
return $this; | |
} | |
public function removeShareholder(Shareholder $shareholder): self | |
{ | |
if ($this->shareholders->contains($shareholder)) { | |
$this->shareholders->removeElement($shareholder); | |
// set the owning side to null (unless already changed) | |
if ($shareholder->getCompany() === $this) { | |
$shareholder->setCompany(null); | |
} | |
} | |
return $this; | |
} | |
/** | |
* @return Collection|Department[] | |
*/ | |
public function getDepartments(): Collection | |
{ | |
return $this->departments; | |
} | |
public function addDepartment(Department $department): self | |
{ | |
if (!$this->departments->contains($department)) { | |
$this->departments[] = $department; | |
$department->setCompany($this); | |
} | |
return $this; | |
} | |
public function removeDepartment(Department $department): self | |
{ | |
if ($this->departments->contains($department)) { | |
$this->departments->removeElement($department); | |
// set the owning side to null (unless already changed) | |
if ($department->getCompany() === $this) { | |
$department->setCompany(null); | |
} | |
} | |
return $this; | |
} | |
public function getIsReinforcement(): ?bool | |
{ | |
return $this->is_reinforcement; | |
} | |
public function setIsReinforcement(?bool $is_reinforcement): self | |
{ | |
$this->is_reinforcement = $is_reinforcement; | |
return $this; | |
} | |
public function getIsSatisfied(): ?bool | |
{ | |
return $this->is_satisfied; | |
} | |
public function setIsSatisfied(?bool $is_satisfied): self | |
{ | |
$this->is_satisfied = $is_satisfied; | |
return $this; | |
} | |
public function getCommentReinforcement(): ?string | |
{ | |
return $this->comment_reinforcement; | |
} | |
public function setCommentReinforcement(?string $comment_reinforcement): self | |
{ | |
$this->comment_reinforcement = $comment_reinforcement; | |
return $this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Form; | |
use App\Entity\Company; | |
use App\Entity\Domain; | |
use App\Entity\Legal; | |
use Symfony\Bridge\Doctrine\Form\Type\EntityType; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\Extension\Core\Type\TextType; | |
use Symfony\Component\Form\Extension\Core\Type\UrlType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
class CompanyType extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('name', TextType::class, [ | |
'label' => false, | |
'required' => true, | |
]) | |
->add('seat', TextType::class, [ | |
'label' => false, | |
'required' => true | |
]) | |
->add('domain', EntityType::class, [ | |
'class' => Domain::class, | |
'choice_label' => 'name', | |
'label' => false, | |
'required' => true, | |
'placeholder' => 'Selectionnez' | |
]) | |
->add('legal', EntityType::class, [ | |
'class' => Legal::class, | |
'choice_label' => 'name', | |
'label' => false, | |
'required' => true, | |
'placeholder' => 'Selectionnez' | |
]) | |
; | |
} | |
public function configureOptions(OptionsResolver $resolver) | |
{ | |
$resolver->setDefaults([ | |
'data_class' => Company::class | |
]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Form\CompanyType; | |
use Doctrine\ORM\EntityManagerInterface; | |
use Psr\Container\ContainerInterface; | |
use Symfony\Component\HttpFoundation\JsonResponse; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\Routing\Annotation\Route; | |
use App\Entity\Company; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
/** | |
* Class AccountController | |
* @package App\Controller | |
* @Route("/account") | |
*/ | |
class AccountController extends Controller | |
{ | |
/** | |
* @Route("/info-start", name="start_member", methods={"GET","POST"}, schemes={"%secure_channel%"}, options={"expose"=true}) | |
* @param Request $request | |
* @return Response | |
*/ | |
public function startUserInfo(Request $request, EntityManagerInterface $em ) | |
{ | |
$company = new Company(); | |
$form = $this->createForm(CompanyType::class, $company); | |
$form->handleRequest($request); | |
$data = $request->getContent(); | |
if ($request->isXmlHttpRequest()) { | |
if (!$data) { | |
return new JsonResponse([ | |
'stateError' => true, | |
'errors' => 'Vous n\'avez envoyer aucune donnée', | |
]); | |
} | |
$dataForm = $this->get('jms_serializer')->deserialize($data, Company::class, 'json'); | |
return new JsonResponse(['data' => $dataForm], Response::HTTP_OK); | |
} else { | |
if ($form->isSubmitted() && $form->isValid()) { | |
$em->persist($company); | |
$em->flush(); | |
return $this->redirectToRoute('second_member'); | |
} | |
} | |
return $this->render('network/pages/wizard.html.twig',[ | |
'form' => $form->createView() | |
]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="form-fse"> | |
{{ form_start(form, | |
{ 'method': 'POST', | |
'action': path('start_member'), | |
'attr': { | |
'class': 'card company-steps', | |
novalidate: 'novalidate' | |
} } ) | |
}} | |
<div class="card-body"> | |
<div class="row"> | |
<div class="form-group col-md-6"> | |
{{ form_row(form.name) }} | |
<div class="invalid-feedback"></div> | |
</div> | |
<div class="form-group col-md-6"> | |
{{ form_row(form.seat) }} | |
<div class="invalid-feedback"></div> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="form-group col-md-6"> | |
{{ form_row(form.legal) }} | |
<div class="invalid-feedback"></div> | |
</div> | |
<div class="form-group col-md-6"> | |
{{ form_row(form.domain) }} | |
<div class="invalid-feedback"></div> | |
</div> | |
</div> | |
</div> | |
<footer class="card-footer text-right"> | |
<button class="btn btn-primary" | |
type="submit">{{ 'Suivant' }} | |
</button> | |
</footer> | |
{{ form_end(form) }} | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment