Skip to content

Add title filter in admin using LexikFormFilterBundle #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function registerBundles()
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
new CodeExplorerBundle\CodeExplorerBundle(),
new Lexik\Bundle\FormFilterBundle\LexikFormFilterBundle(),
new AppBundle\AppBundle(),
);

Expand Down
5 changes: 5 additions & 0 deletions app/Resources/views/admin/blog/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
{% block main %}
<h1>{{ 'title.post_list'|trans }}</h1>

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

<table class="table table-striped">
<thead>
<tr>
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"symfony/monolog-bundle" : "~2.7",
"symfony/swiftmailer-bundle" : "~2.3",
"symfony/symfony" : "~2.8",
"twig/extensions" : "~1.2"
"twig/extensions" : "~1.2",
"lexik/form-filter-bundle": "dev-master"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

depending on dev-master is a no-go. If the bundle does not have stable releases (which should be considered though), a branch alias should at least be used to avoid getting bumped to a new major version if they start developing it. If they don't support installing it the bundle other than with a dev-master requirement, I vote against including it in the demo.
And if they don't have a branch alias, you should open a PR adding it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

composer.phar require lexik/form-filter-bundle ~4.0

},
"require-dev": {
"sensio/generator-bundle": "~3.0"
Expand Down
26 changes: 22 additions & 4 deletions src/AppBundle/Controller/Admin/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace AppBundle\Controller\Admin;

use AppBundle\Filter\PostFilterType;
use AppBundle\Form\PostType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
Expand Down Expand Up @@ -50,12 +51,29 @@ class BlogController extends Controller
* @Route("/", name="admin_post_index")
* @Method("GET")
*/
public function indexAction()
public function indexAction(Request $request)
{
$entityManager = $this->getDoctrine()->getManager();
$posts = $entityManager->getRepository('AppBundle:Post')->findAll();
$form = $this->get('form.factory')->create(new PostFilterType());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't pass a type instance. This is deprecated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How it is done now?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sergiu-popa use FQCN instead. In your project you could use ...->create(PostFilterType::class) but in Symfony Demo we still need to use: ...->create('AppBundle\Filter\PostFilterType') for BC.


// initialize a query builder
$filterBuilder = $this->get('doctrine.orm.entity_manager')
->getRepository('AppBundle:Post')
->createQueryBuilder('p');

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

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

$posts = $filterBuilder->getQuery()->getResult();

return $this->render('admin/blog/index.html.twig', array('posts' => $posts));
return $this->render('admin/blog/index.html.twig', array(
'form' => $form->createView(),
'posts' => $posts
));
}

/**
Expand Down
29 changes: 29 additions & 0 deletions src/AppBundle/Filter/PostFilterType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace AppBundle\Filter;

use Lexik\Bundle\FormFilterBundle\Filter\FilterOperands;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PostFilterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title', 'filter_text', array(
'condition_pattern' => FilterOperands::STRING_CONTAINS));
}

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

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'csrf_protection' => false,
'validation_groups' => array('filtering') // avoid NotBlank() constraint-related message
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which not-blank ? there are none

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from bundle's installation instructions.

));
}
}