-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't pass a type instance. This is deprecated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How it is done now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sergiu-popa use FQCN instead. In your project you could use |
||
|
||
// 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 | ||
)); | ||
} | ||
|
||
/** | ||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. which not-blank ? there are none There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copied from bundle's installation instructions. |
||
)); | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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