Validation formulaire manytomany
Bonjour à tous,
Je fais appel à vous car je ne trouve pas la source de mon erreur. :cry:
Je suis entreint de développer une sorte d'appli qui permet de gérer différents genre de document comme les factures, avoir, etc ...
Je souhaite donc qu'on puisse crée un document avec plusieurs documents.
J'ai donc une entite document :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
| <?php
namespace Sdz\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Documents
*
* @ORM\Table(name="documents")
* @ORM\Entity
*/
class Documents
{
/**
* @var integer
*
* @ORM\Column(name="id_document", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idDocument;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime", nullable=false)
*/
private $date;
/**
* @var float
*
* @ORM\Column(name="prix", type="float", nullable=false)
*/
private $prix;
/**
* @var \Documents
*
* @ORM\ManyToOne(targetEntity="Documents")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_ref_facture", referencedColumnName="id_document")
* })
*/
private $idRefFacture;
/**
* @var \User
*
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_user", referencedColumnName="id_user")
* })
*/
private $idUser;
/**
* @var \TypeDocument
*
* @ORM\ManyToOne(targetEntity="TypeDocument")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_type_document", referencedColumnName="id_type_document")
* })
*/
private $idTypeDocument;
/**
* @var \SocieteClient
*
* @ORM\ManyToOne(targetEntity="SocieteClient")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_societe", referencedColumnName="id_societe")
* })
*/
private $idSociete;
/**
* @var \Produit
*
* @ORM\ManyToMany(targetEntity="Produit", cascade={"persist"})
*
*/
protected $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
/**
* Get idDocument
*
* @return integer
*/
public function getIdDocument()
{
return $this->idDocument;
}
/**
* Set date
*
* @param \DateTime $date
* @return Documents
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set prix
*
* @param float $prix
* @return Documents
*/
public function setPrix($prix)
{
$this->prix = $prix;
return $this;
}
/**
* Get prix
*
* @return float
*/
public function getPrix()
{
return $this->prix;
}
/**
* Set idRefFacture
*
* @param \Sdz\BlogBundle\Entity\Documents $idRefFacture
* @return Documents
*/
public function setIdRefFacture(\Sdz\BlogBundle\Entity\Documents $idRefFacture = null)
{
$this->idRefFacture = $idRefFacture;
return $this;
}
/**
* Get idRefFacture
*
* @return \Sdz\BlogBundle\Entity\Documents
*/
public function getIdRefFacture()
{
return $this->idRefFacture;
}
/**
* Set idUser
*
* @param \Sdz\BlogBundle\Entity\User $idUser
* @return Documents
*/
public function setIdUser(\Sdz\BlogBundle\Entity\User $idUser = null)
{
$this->idUser = $idUser;
return $this;
}
/**
* Get idUser
*
* @return \Sdz\BlogBundle\Entity\User
*/
public function getIdUser()
{
return $this->idUser;
}
/**
* Set idTypeDocument
*
* @param \Sdz\BlogBundle\Entity\TypeDocument $idTypeDocument
* @return Documents
*/
public function setIdTypeDocument(\Sdz\BlogBundle\Entity\TypeDocument $idTypeDocument = null)
{
$this->idTypeDocument = $idTypeDocument;
return $this;
}
/**
* Get idTypeDocument
*
* @return \Sdz\BlogBundle\Entity\TypeDocument
*/
public function getIdTypeDocument()
{
return $this->idTypeDocument;
}
/**
* Set idSociete
*
* @param \Sdz\BlogBundle\Entity\SocieteClient $idSociete
* @return Documents
*/
public function setIdSociete(\Sdz\BlogBundle\Entity\SocieteClient $idSociete = null)
{
$this->idSociete = $idSociete;
return $this;
}
/**
* Get idSociete
*
* @return \Sdz\BlogBundle\Entity\SocieteClient
*/
public function getIdSociete()
{
return $this->idSociete;
}
public function getProducts()
{
return $this->products;
}
/**
* Set products
*
* @param \Sdz\BlogBundle\Entity\Produit $idSociete
* @return Produit
*/
public function setProducts(ArrayCollection $products)
{
$this->products = $products;
}
/**
* Set idDocument
*
* @param \Sdz\BlogBundle\Entity\Documents $iddocument
* @return Documents
*/
public function setIdDocument(\Sdz\BlogBundle\Entity\Documents $iddocument=null){
$this->idDocument=$iddocument;
return $this;
}
} |
et une entite produit :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| <?php
namespace Sdz\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Produit
*
* @ORM\Table(name="produit")
* @ORM\Entity
*/
class Produit
{
/**
* @var integer
*
* @ORM\Column(name="id_produit", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idProduit;
/**
* @var string
*
* @ORM\Column(name="name_product", type="string", length=100, nullable=false)
*/
private $nameProduct;
/**
* @var float
*
* @ORM\Column(name="price_product_ht", type="float", nullable=false)
*/
private $priceProductHt;
/**
* @var \Tva
*
* @ORM\ManyToOne(targetEntity="Tva")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_tva", referencedColumnName="id_tva")
* })
*/
private $idTva;
/**
* Get idProduit
*
* @return integer
*/
public function getIdProduit()
{
return $this->idProduit;
}
/**
* Set nameProduct
*
* @param string $nameProduct
* @return Produit
*/
public function setNameProduct($nameProduct)
{
$this->nameProduct = $nameProduct;
return $this;
}
/**
* Get nameProduct
*
* @return string
*/
public function getNameProduct()
{
return $this->nameProduct;
}
/**
* Set priceProductHt
*
* @param float $priceProductHt
* @return Produit
*/
public function setPriceProductHt($priceProductHt)
{
$this->priceProductHt = $priceProductHt;
return $this;
}
/**
* Get priceProductHt
*
* @return float
*/
public function getPriceProductHt()
{
return $this->priceProductHt;
}
/**
* Set idTva
*
* @param \Sdz\BlogBundle\Entity\Tva $idTva
* @return Produit
*/
public function setIdTva(\Sdz\BlogBundle\Entity\Tva $idTva = null)
{
$this->idTva = $idTva;
return $this;
}
/**
* Get idTva
*
* @return \Sdz\BlogBundle\Entity\Tva
*/
public function getIdTva()
{
return $this->idTva;
}
} |
Pour le formulaire, j'ai suivis un tuto qui m'a montré créer un formulaire imbriqué :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| <?php
namespace Sdz\BlogBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class DocumentsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('date','date')
->add('prix','integer')
->add('idDocument','entity',array(
'class'=>'SdzBlogBundle:Documents',
'property'=>'idDocument',
'label'=>'document ref'
))
->add('idTypeDocument','entity',array(
'class' => 'SdzBlogBundle:TypeDocument',
'property' => 'nameDocument',
'label'=>'Type de document '
))
->add('idSociete','entity',array(
'class'=>'SdzBlogBundle:SocieteClient',
'property'=>'nameSociete',
'label'=>'Societe'
))
->add('idUser','entity',array(
'class'=>'SdzBlogBundle:User',
'property'=>'lastname',
'label'=>'Utilisateur'
))
->add('products','collection', array(
'type'=> new ProduitType(),
'allow_add' => true,
'by_reference' => false,
));
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Sdz\BlogBundle\Entity\Documents'
));
}
public function getName()
{
return 'sdz_blogbundle_documentstype';
}
} |
Jusqu'ici tout va bien. Mais quand je valide mon formulaire
Code:
Method "name_product" for object "Symfony\Component\Form\FormView" does not exist in SdzBlogBundle:Blog:ajouter.html.twig at line 82
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <div id="date" class="date">
{{ form_row(form.date) }}
</div>
<div id="prix" class="prix">
{{ form_row(form.prix) }}
</div>
<div id="idDocument" class="idDocument">
{{ form_row(form.idDocument) }}
</div>
<div id="idTypeDocument" class="idTypeDocument">
{{ form_row(form.idTypeDocument) }}
</div>
<div id="idSociete" class="idSociete">
{{ form_row(form.idSociete) }}
</div>
<div id="idSociete" class="idSociete">
{{ form_row(form.idSociete) }}
</div>
<h4>produit</h4>
<ul class="products" data-prototype="{{ form_widget(form.products.vars.prototype)|e }}">
{# itère sur chaque tag existant et affiche son unique champ : name #}
{% for tag in form.products %}
<li>{{ form_row(tag.name_product) }}</li>
{% endfor %}
</ul> |
J'ai déboguée un peu avec des die, etc ... et j'ai vu que mon formulaire n'est pas valid dans le controller.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| if( $request->getMethod() == 'POST' )
{
$form->bind($request);
if( $form->isValid() )
{
$em = $this->getDoctrine()->getEntityManager();
$em->persist($doc);
$em->flush();
return $this->redirect( $this->generateUrl('sdzblog_accueil')
);
}
} |
L'affichage de mon formulaire fonctionne, c'est la validation qui me pose problème.
Voila, je me remet à vous. Si vous pouviez me guider vers une solution, ca serait sympa :)
Au plaisir,
RYL