0

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 !!!

8
  • The problem, is that I have an ajax query on my Entreprise query or The 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 see postalcode anywhere? Maybe you just wrote postalcode instead of entreprise.postalcode? The error say it's looking into App\Entity\User, but it should be App\Entity\Entreprise. Make your question more clear (title included) and add twig view please. Also, do you create/edit from owning or inverse side? Commented Oct 5, 2018 at 14:30
  • You're right, its not an ajax problem !! but I use it for an ajax method. I'll try Commented Oct 5, 2018 at 14:37
  • Doesn't seems like it but... Is form a collection of forms? How to Embed a Collection of Forms Commented Oct 5, 2018 at 14:51
  • I don't think so. The form is just embedded like that : ->add('entreprise', EntrepriseType::class, $options) and I need the options to get the entitymanager in my EntrepriseType Commented Oct 5, 2018 at 14:55
  • I think that your main problem. You're actually using 2 forms. User form and Entreprise form, which will become a child of User. 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 for Entreprise (Not sure that it's really needed as it's OneToOnehere) Commented Oct 5, 2018 at 14:59

1 Answer 1

1

You need to generate your setters and getters for the entity object:

php app/console doctrine:generate:entities App

you wil see the actual file change and you'll be able to use them property postalCode Currently it's private, that's why it shows as if it doesn't exist

Sign up to request clarification or add additional context in comments.

3 Comments

He most likely did but didn't post them... We most of the time don't need them as we know them
The problem is that the the form is embedded and the property postalcode is in the form that is embedded
Did you resolve the issue or still need help debugging it? @cretthie

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.