0
votes

I'm new with Symfony2 and trying to work with the Lexik Filter bundle; I'm having 2 entities (Parents-Training) linked in a n-m (Many-To-Many) relationship as defined below, i'm trying to filter the list of Parents by names: `tuto\LexikTestBundle\Entity\Parents:

type: entity
table: Parents
repositoryClass: tuto\LexikTestBundle\Repository\ParentRepository
id:
    id:
        type: integer
        generator:
            strategy: AUTO
fields:
    Firstname:
        type: string
        length: 50
    Lastname:
        type: string
        length: 50
    DOB:
        type: datetime
    Email:
        type: string
        length: 50
manyToMany:
    Trainings:
        targetEntity: Training
        mappedBy: parents`  

I'm following the tutorial working with the ver 3.0.8 and defined the following Custom Filter type:

namespace tuto\LexikTestBundle\Form\Filter;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;


class MyParentsType extends AbstractType
{
    /**
     * Returns the name of this type.
     *
     * @return string The name of this type
     */
    public function getName()
    {
        return 'parents_filter';
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('Firstname', 'filter_text');
        $builder->add('Lastname', 'filter_text');

    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            [
                'csrf_protection' => false,
                'validation_groups' => ['filtering'] // avoid NotBlank() constraint-related message
            ]
        );
    }
}

And the following FilterAction in the controller :

public function indexAction(Request $request)
{


    $em = $this->getDoctrine()->getManager();
    $entities = $em->getRepository('tutoLexikTestBundle:Parents')->findAll();

    $form = $this->testFilterAction($request);

    return $this->render('tutoLexikTestBundle:Parents:index.html.twig', [
        'entities' => $entities,
        'form' => $form,
    ]
    );
}

public function testFilterAction(Request $request)
{
    $form= $this->get('form.factory')->create(new MyParentsType());

    if($request->query->has($form->getName())) {
        // manually bind values from the request
        $form->submit($request->query->get($form->getName()));

        $queryBuilder = $this->get('doctrine.orm.entity_manager')
            ->getRepository('LexikTestBundle:Parents')
            ->createQueryBuilder('e');

        //build the query from the given object
        $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form,$queryBuilder);
        var_dump($queryBuilder->getDql());
    }

    return $this->render(
        'tutoLexikTestBundle:Default:testFilter.html.twig',
        ['form'=>$form->createView()]
    );
}

The twig implemented is

<form method="get" action=".">
  {{ form_rest(form) }}
  <input type="submit" name="submit-filter" value="filter" />
</form>

I've gone trough the following answers and seem not to have the same error:

But i'm still facing the Catchable Fatal Error: Argument 1 passed to Symfony\Component\Form\FormRenderer::searchAndRenderBlock() must be an instance of Symfony\Component\Form\FormView, instance of Symfony\Component\HttpFoundation\Response given any hint would be welcome.

1

1 Answers

1
votes

You should render a form view and not a form object :

public function indexAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $entities = $em->getRepository('tutoLexikTestBundle:Parents')->findAll();

    $form = $this->testFilterAction($request);

    return $this->render('tutoLexikTestBundle:Parents:index.html.twig', [
        'entities' => $entities,
        'form' => $form->createView(),
    ]);
}

Your testFilterAction returns a Response object and not a form object or view please change your method as below:

Note: you can nest form template in your tutoLexikTestBundle:Parents:index.html.twig view

public function testFilterAction(Request $request)
{
    $form = $this->get('form.factory')->create(new MyParentsType());

    if ($request->query->has($form->getName())) {
        // manually bind values from the request
        $form->submit($request->query->get($form->getName()));

        $queryBuilder = $this->get('doctrine.orm.entity_manager')
            ->getRepository('LexikTestBundle:Parents')
            ->createQueryBuilder('e');

        // build the query from the given object
        $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form,$queryBuilder);
        var_dump($queryBuilder->getDql());
    }

    return $form; // return form object
}