Question

So after read the docs and apply just the same concepts in my code I'm still getting the error:

This form should not contain extra fields

Below is the code for the form that generates the error:

namespace Common\CommonBundle\Form;

use Symfony\Component\Form\AbstractType,
    Symfony\Component\Form\FormBuilderInterface,
    Symfony\Component\OptionsResolver\OptionsResolverInterface,
    Common\CommonBundle\Form\AddressExtraInfoType;

class StandardAddressType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('extra_info', new AddressExtraInfoType());
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Common\CommonBundle\Entity\StandardAddress',
            'cascade_validation' => true
        ));
    }

    public function getName() {
        return 'standard_address';
    }

}

Where is the error in my code?

Add some missed info

In answer to why I said error happen in that file (for me) here is the result of a JSON I return if $form->isValid() pass:

{
  "success": false,
  "errors": {
    "formCompany": [
      "This form should not contain extra fields."
    ],
    "formStandardAddress": [
      "This form should not contain extra fields."
    ],
    "formPhone": [],
    "formCourrierAddress": []
  }
}

Errors happen in formCompany and formStandardAddress. Below is the entity for StandardAddress.php (I removed non required methods, just leave the relevant part):

<?php

namespace Common\CommonBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Fresh\Bundle\DoctrineEnumBundle\Validator\Constraints as DoctrineAssert;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Common\CommonBundle\Entity\Country;
use Common\CommonBundle\Entity\State;
use Common\CommonBundle\Entity\City;
use Common\CommonBundle\Entity\AddressExtraInfo;

/**
 * @ORM\Entity
 * @ORM\Table(name="standard_address")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 */
class StandardAddress {

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="City")
     * @ORM\JoinColumn(name="city", referencedColumnName="iso", nullable=false)
     */
    protected $city;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Country")
     * @ORM\JoinColumn(name="country", referencedColumnName="iso", nullable=false)
     */
    protected $country;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="State")
     * @ORM\JoinColumn(name="state", referencedColumnName="iso", nullable=false)
     */
    protected $state;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="AddressExtraInfo")
     * @ORM\JoinColumn(name="address_extra_info", referencedColumnName="id", nullable=false)
     */
    protected $extra_info;

    /**
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="created", type="datetime")
     */
    protected $created;

    /**
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="modified", type="datetime")
     */
    protected $modified;

    /**
     * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
     */
    protected $deletedAt;

    public function setExtraInfo(AddressExtraInfo $extra_info) {
        $this->extra_info = $extra_info;
    }

    public function getExtraInfo() {
        return $this->extra_info;
    }

}

Notice there is a extra_info inside it.

Était-ce utile?

La solution

The field extra_info should be referred in 'Common\CommonBundle\Entity\StandardAddress' class; and corresponding geter and setter for the field. It's better to have any relation between these entities.

Your StandAddress entity mus have methods like,

    /**
     * @Assert\Type(type="Common\CommonBundle\Entity\extra_infoEntity")
     * the entity referring extra_info
     */
    protected $extraInfo;

    // ...

    public function getExtraInfo()
    {
        return $this->extraInfo;
    }

    public function setExtraInfo(extra_infoEntity $extraInfo = null)
    {
        $this->extraInfo = $extraInfo;
    }

Then only you can import another FormType within your current FormType

You may Try with these changes in your Entity annotation

       /**
         * @ORM\Id
         * @ORM\ManyToOne(targetEntity="AddressExtraInfo")
         * @ORM\JoinColumns({
         *    @ORM\JoinColumn(name="address_extra_info", referencedColumnName="id",nullable=false)
         * })
         */
        protected $extra_info;

Annotate @ORM\JoinColumn inside @ORM\JoinColumns

This type of implementation worked for me.

For complete reference see the DOCUMENTATION

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top