The problem is that the the form is embedded and the property postalcode is in the form that is embedded
I made 2 entity in my symfony project with a OneToOne relation.
The User entity :
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
// A voir !!!!
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
/**
* @ORM\Entity(repositoryClass="App \Repository\UserRepository")
* @UniqueEntity(
* fields= {"email"},
* message= "L'email est déjà utilisé"
* )
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
...
/**
* @ORM\OneToOne(targetEntity="App\Entity\Entreprise", mappedBy="user", cascade={"persist", "remove"})
*/
private $entreprise;
The Entreprise Entity
/**
* @ORM\Entity(repositoryClass="App\Repository\EntrepriseRepository")
*/
class Entreprise
{
/**
* @ORM\Id()
* @ORM\OneToOne(targetEntity="App\Entity\User", inversedBy="entreprise", cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* @ORM\Column(name="postalcode", type="string", length=16, nullable=false)
*/
private $postalcode;
public function setPostalcode($postalcode)
{
$this->postalcode = $postalcode;
return $this;
}
public function getPostalcode()
{
return $this->postalcode;
}
And now, I made 2 FormType One is the normal UserType that embed the EntrepriseType. The problem, is that I have an ajax query on my Entreprise query.
Like that : The UserType :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$this->entityManager = $options['entity_manager'];
$builder
->add('email', EmailType::class)
->add('username', TextType::class)
->add('password', PasswordType::class)
->add('confirm_password', PasswordType::class)
->add('termsaccepted', CheckboxType::class, array(
'mapped' => false,
'constraints' => new IsTrue(),))
->add('entreprise', EntrepriseType::class, $options)
;
}
And the EntrepriseType :
private $entityManager;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$this->entityManager = $options['entity_manager'];
$builder
->add('postalcode', TextType::class, array(
'attr'=> array('class'=>'postalcode form-control',
'maxlength'=>4,
'value'=>'')))
->add('ville', ChoiceType::class, array(
'attr'=>array('class'=>'ville form-control')))
->add('adresse', TextType::class)
->add('complementadresse', TextType::class)
;
$city = function(FormInterface $form, $codepostal){
$repo = $this->entityManager->getRepository("App:Localite");
$localitestofind = $repo->findBy(array('codepostal'=>$codepostal));
$localites = array();
if($localitestofind)
{
foreach($localitestofind as $localitetofind)
{
$localites[$localitetofind->getNom()] = $localitetofind->getNom();
}
}
$form->add('ville', ChoiceType::class, array(
'attr'=>array('class'=>'ville form-control'),
'choices'=> $localites));
};
$builder->get('postalcode')->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) use ($city){
$city($event->getForm()->getParent(), $event->getForm()->getData());
});
}
My Twig file :
{{form_start(form)}}
{{form_row(form.username, {'label':'Nom d\'utilisateur','attr':{'placeholder':'Nom d\'utilisateur'}})}}
{{form_row(form.email, {'label':'Adresse email','attr':{'placeholder':'[email protected]'}})}}
{{form_row(form.password, {'label':'Mot de passe','attr':{'placeholder':'Mot de passe'}})}}
{{form_row(form.confirm_password, {'label':'Répéter le mot de passe','attr':{'placeholder':'Confirmer votre mot de passe '}})}}
{{form_row(form.entreprise.adresse, {'label':'Adresse','attr':{'placeholder':'Rue et n°'}})}}
{{form_row(form.entreprise.complementadresse, {'label':'Complément d\'adresse','attr':{'placeholder':'Espace business 2'}})}}
{{form_row(form.entreprise.postalcode, {'label':'Code postal','attr':{'placeholder':'Code postal', 'class':'postalcode form-control', 'maxlength':'4', 'value':''}})}}
{{form_row(form.entreprise.ville, {'label':'Ville','attr':{'placeholder':'Ville', 'class':'ville form-control'}})}}
{{form_row(form.termsaccepted, {'label':"En soumettant ce formulaire, vous acceptez nos conditions d'utilisation et notre politique de Protection des données."})}}
<button type="submit" class="btn btn-success" >Inscription</button>
{{ form_end(form)}}
The problem is that my "postalcode" is not recognized : so the error is :
Neither the property "postalcode" nor one of the methods "getPostalcode()", "postalcode()", "isPostalcode()", "hasPostalcode()", "__get()" exist and have public access in class "App\Entity\User".
Do you have an idea on how to do ? Thanks for help !!!
The problem, is that I have an ajax query on my Entreprise queryorThe problem is that my "postalcode" is not recognized? What is the real problem in the end? To me it's not related to Ajax at all. Have you dumped data in your twig view with {{ dump() }} ? Do you seepostalcodeanywhere? Maybe you just wrotepostalcodeinstead ofentreprise.postalcode? The error say it's looking intoApp\Entity\User, but it should beApp\Entity\Entreprise. Make your question more clear (title included) and add twig view please. Also, do you create/edit from owning or inverse side?forma collection of forms? How to Embed a Collection of FormsUserform andEntrepriseform, which will become a child ofUser. Look at the link above to know how to handle collection forms. Also, as you're doing things on the inverse side, look at there answer here (OneToMany doesn't save changes). You might have to handle data yourself forEntreprise(Not sure that it's really needed as it'sOneToOnehere)