1

i need your help please , I want to display my created form in Symfony2. I want to display my created form 92 times becouse i have 92 numbers in my database(every number is a form) , i didn't know how to do it here is my code: controller:

class DefaultController extends Controller
{
 public function QuestionsAction(Request $request)
 {   
     $questions = $this->getDoctrine()->getEntityManager()
          ->getRepository('Tests\TestsPhpBundle\Entity\Question')     
          ->findAll();
    $task = new Question();
    $forms = $this->createForm(new QuestionType(), $task);   
    if ($request->getMethod() == 'POST') {
    $forms->bindRequest($request);             
        if ($forms->isValid()) 
         {
             $em = $this->getDoctrine()->getEntityManager();
             $em->persist($task);
             $em->flush();
             }                 
  }  
       {
 return $this->render('TestsTestsPhpBundle:Default:index.html.twig', array(
        'questions' => $questions,
        'forms' => $forms->createView()
                      ));      
       }

 }
 }

my form file:

class QuestionType extends AbstractType
       {
        public function buildForm(FormBuilder $builder, array $options)
         {


         $builder
            ->add('categories',  null, array('required' => false,                                              
                                            ))                                         

            ->add('text', 'entity', array(
                         'class' => 'TestsTestsPhpBundle:Question',
                         'query_builder' => function($repository) { 
     return $repository->createQueryBuilder('p')->orderBy('p.id', 'ASC'); },
                         'property' => 'text'))
                ;
    }
     public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'Tests\TestsPhpBundle\Entity\Question',);
    }
    public function getName()
    {
        return 'question';
    }
    }

my twig file:

{% block content %}

  <h2>Questions</h2>

   {% for question in questions %}

   <dl>
    <dt>Number</dt>
    <dd>{{ question.number }}<dd>

  {% for form in forms %}

   {{ form_row(forms.categories) }}   
     {{ form_row(forms.text) }}

    </dl>
      {% endfor %}
       <hr />
    {% endfor %}
   {% endblock  %}

1 Answer 1

1

I recommend to read capter: Embedding Controller http://symfony.com/doc/2.0/book/templating.html

<div id="sidebar">
    {% render "AcmeArticleBundle:Article:recentArticles" with {'max': 3} %}
</div>

You can make a for loop within Twig Template and call an action (with parameter if needed) where you render the form. -> QuestionsAction in your case.

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

Comments

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.