Bonjour,
Voila je rencontre un petit problème avec mon code.
Dans mon cas, j'ai 30 entités qu'il n'y a pas de relation entre eux et toutes les entités ont deux colonnes. Je voulais avoir 6 ou 7 entités dans une forme, mais je ne sais pas quelle est la meilleure façon de le faire? ceci est mon code ...
ceci est mon contrôleur:
public function general1Action(Request $request)
{
$example1 = new Example1();
$example2 = new Example2();
$example3 = new Example3();
$example4 = new Example4();
$formexample1 = $this->createForm('...Bundle\Form\Example1Type', $example1);
$formexample2 = $this->createForm('...Bundle\Form\ Example2Type', $example2);
$formexample3 = $this->createForm('...Bundle\Form\ Example3Type', $example3);
$fprmexample4 = $this->createForm('...Bundle\Form\Example4Type', $example4);
$example1->handleRequest($request);
$example2->handleRequest($request);
$example3->handleRequest($request);
$example4->handleRequest($request);
$em = $this->getDoctrine()->getManager();
if ($example1->isSubmitted() && $example1->isValid()) {
/**
* To generate the value example1 first column(e1fc) and second column(e1sc)
*/
$prefix=$this->container->getParameter('prefix');
$e1fcCle=$em->getRepository("...Bundle:Example1")->genereCle('Example1',$prefix);
$example1->sete1fc($e1fcCle);
/**
* To generate the value example1 second column(e1sc)
*/
$example1->sete1sc("e1sc".($e1fcCle));
/**
* to check, the fields are not empty
*/
if(($formexample2["e2sc"]->getData())!=""){
$example2->sete1fc($e1fcCle);
$em->persist($example2);
}
if(($formexample3["e3sc"]->getData())!=""){
$example3->sete1fc($e1fcCle);
$em->persist($example3);
}
if(($formexample4["e4sc"]->getData())!=""){
$example4-> sete1fc($e1fcCle);
$em->persist($example4);
}
$em->persist($example1);
$em->flush();
return $this->forward('...Bundle:General...: general2',
array('E1FC' => $e1fcCle));
}
return $this->render('.../general1.html.twig', array(
'example1' => $example1,
'formExample1' => $formexample1->createView(),
'formExample2' => $formexample2->createView(),
'formExample3' => $formexample3->createView(),
'formExample4' => $formexample4->createView(),
));
}` ```
and this is my general1.html.twig :
{% extends 'base.html.twig' %}
{% block body %}
{{ form_start(formExample1) }}
{{ form_row(formExample2.e2sc) }}
{{ form_row(formExample3.e3sc) }}
{{ form_row(formExample4.e4sc) }}
<input type="submit" value="Next" />
{{ form_widget(formExample1._token) }}
{{ form_end(formExample1, {"render_rest":false}) }}
<ul>
<li>
<a href="{{ path('..._index') }}">Back to the list</a>
</li>
</ul>
{% endblock %}`,
I have another question : when i try to use isValid() for the other form like this
if(($formexample2["e2sc"]->getData())!=""&& $example2->isValid())
i have this error: Fatal error: Call to a member function get...() on null
Bonjour,
Pour chaque type d'entité, créer une classe formulaire (sans submit), ex Entity1Type pour Entity1
namespace AppBundle\Form;
use AppBundle\Form;
//...
class Entity1Type extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add("prop_one", ...)
->add("prop_two", ...)
//.... etc...
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
"data_class"=>Entity1::class
]);
}
}
Créer un objet qui rassemble les entites que tu veux mettre dans ton formulaire
namespace AppBundle\Form\Model;
class MyModel {
/**
* @var \AppBundle\Entity\Entity1
* @Assert\Valid()
*/
private $entity1;
/**
* @var \AppBundle\Entity\Entity2
* @Assert\Valid()
*/
private $entity2;
/**
* @var \AppBundle\Entity\Entity3
* @Assert\Valid()
*/
private $entity3;
/**
* @var \AppBundle\Entity\Entity4
* @Assert\Valid()
*/
private $entity4;
//... etc ...
// getters & setters ...
}
Créer une classe formulaire pour MyModel :
namespace AppBundle\Form;
use AppBundle\Form\Model\MyModel;
//...
class MyModelForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add("entity1", Entity1Type::class, [...])
->add("entity2", Entity2Type::class, [...])
->add("entity3", Entity3Type::class, [...])
->add("entity4", Entity4Type::class, [...])
//.... etc ...
->add("submit", SubmitType::class, ["label"=>"Enregistrer"])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
"data_class"=>MyModel::class
]);
}
}
dans ton controller :
public function monactionAction(Request $request)
{
// ...
$model = new MyModel();
$model->setEntity1($entity1)->setEntity2($entity2)....... etc;
$form = $this->createForm(MyModelForm::class, $model);
$form->handleRequest($request);
if($form->isValid()){
// ici les entités ont leurs nouvelles valeurs
// ... code 'maison', persistence, ...
// redirection
}
return $this->render("mapage.html.twig", ["form"=>$form->createView()];
}