带有Lexik Form Filter的symfony2过滤器包:

时间:2014-11-21 01:51:29

标签: forms symfony filter

我有实体诊所,

表格类型:

namespace AgriHealth\AhpBundle\Form\Type\Clinic;

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

class ClinicFilterType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'filter_text');
    }

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

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

控制器设置

public function indexAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('AgriHealthAhpBundle:Clinic')->findAll();
    $filter = $this->renderFilter($request);

    return $this->render('AgriHealthAhpBundle:Clinic:index.html.twig', array(
        'entities' => $entities,
        'form' => $filter
    ));
}

private function renderFilter(Request $request)
{
    $form = $this->get('form.factory')->create(new ClinicFilterType());

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

        // initialize a query builder
        $filterBuilder = $this->get('doctrine.orm.entity_manager')
            ->getRepository('AgriHealthAhpBundle:Clinic')
            ->createQueryBuilder('e');

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

        // now look at the DQL =)
        var_dump($filterBuilder->getDql());
    }

    return $this->render('AgriHealthAhpBundle:Clinic:filter.html.twig', array(
        'form' => $form->createView(),
    ));

}

我正在

Catchable Fatal Error: Object of class Symfony\Component\Form\FormView could not be converted to string

我是否忽略了表单类中的某些内容?

1 个答案:

答案 0 :(得分:1)

如何在filter.html.twig中呈现表单?也许你的模板中有拼写错误。基本的lexik过滤器表单渲染可能如下所示:

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