Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating empty entity on a OneToOne-relation in Symfony
    primarykey
    data
    text
    <p>I'm running a Symfony 2.4 project with Doctrine 2.4 and having some issues with form creation/validation.</p> <p>I have an entity "Person" with a <strong>OneToOne-Relation</strong> to an "Address" entity (unidirectional) - so the person can have one or none addresses.</p> <pre><code>/** * @var Address * @ORM\OneToOne(targetEntity="Koala\AddressBundle\Entity\Address", cascade={"all"}) * @Assert\Valid */ private $address; public function setAddress(Address $address = null) { $this-&gt;address = $address; } </code></pre> <p>In my PersonType class for the form rendering I have added the code as follows:</p> <pre><code>$builder-&gt;add('address', AddressType::SERVICE_NAME, array( 'label' =&gt; 'address._label', 'required' =&gt; false ) ); </code></pre> <p>Loading/Displaying a Person-entity that doesn't have an Address works fine - i.e. no fields are shown for the address because in my Twig-template I have: </p> <pre><code>{% if form.address.vars.value|default is not empty %} {# address form here #} {% endif %} </code></pre> <p>Also loading the edit form works fine, i.e. no address form is shown, as I don't want to have an address in that place of the App (but in general Person could have an address).</p> <p>The problem now is, as soon as I change something in the persons form and submit the changes to the server, form validation fails because Symfony creates a new empty Address object for which the validation fails because there are some mandatory fields in an Address. But here I explicitly don't want an address. I thought that having the if-condition (see above) should solve this. Because using two different twig-templates that quite look the same - just one not having an address seemed ugly to me and would violate the <a href="http://en.wikipedia.org/wiki/DRY_principle" rel="nofollow">DRY-Principle</a>.</p> <p>What am I doing wrong?<br> How can I stop Symfony from creating an empty Address-object for a person?</p> <p><strong>EDIT:</strong><br> For better clarification, here the code of the controller method handling edit:</p> <pre><code>public function editAction($id, $version, Request $request) { try { $company = $this-&gt;companyRepository-&gt;find($id, LockMode::PESSIMISTIC_WRITE, $version); $routeId = array('id' =&gt; $id, 'version' =&gt; $company-&gt;getVersion()); $form = $this-&gt;createForm(CompanyType::SERVICE_NAME, $company, array( 'action' =&gt; $this-&gt;router-&gt;generate('replaced_for_stackoverflow', $routeId), 'method' =&gt; 'POST') ); if ($request-&gt;isMethod('POST')) { $form-&gt;handleRequest($request); if ($form-&gt;isValid()) { // If isValid() were true, entity changes would get saved. } // But: isValid() is false, because there is an Address-Entity in one of the Person-entities attached to the company-entity. And this is because Symfony creates an Address-Instance by itself. And I don't know how to turn this behavior off. } } // Rest of method left out as irrelevant to the problem. } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload