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

Implement "Lookup" form field decorator #550

romaninsh opened this Issue Aug 27, 2018 · 2 comments


None yet
4 participants
Copy link

romaninsh commented Aug 27, 2018

This is a replacement for AutoComplete decorator which adds a pretty cool support for filtering.

Basic Usage:

$lookup = $form->add('city_id', ['Lookup']);
$lookup->setModel(new City($app->db));

Would display this:

screen shot 2018-08-27 at 19 30 48

We assume that City model has 'country' field, which could be imported:

// in City Model
$this->hasOne('country_id', new Country())->addTitle();

Multiple Filters

It's possible to define multiple filters for a lookup:

$lookup = $form->add('country_id', ['Lookup']);
$lookup->setModel(new Country($app->db));
$lookup->addFilter('is_eu', 'EU?');  // second argument is label

screen shot 2018-08-27 at 19 33 43

Filters will filter themselves out too. If you select "Yes" under EU, then you will only see Languages from the EU countries. Filters will also have option 'All' on top of them to reset a filter.

Quirk: selecting filters will only affect drop-down options. They will not reset current values. If you select "Thailand" and after that select 'Is EU' this will not reset "Thailand". That is so that implementation is simple and quick.

Further enhancements

Non-title field

When calling setModel, the second argument should be able to specify field to display in the drop-down:

$lookup->setModel(new Country($app->db), '[name]');


When displaying drop-down values, display number of results for each value, e.g.:

Is EU ▼
  [ Yes (28) ]
  [ No (167) ]


Specify manual condition setting callback:

$lookup->addFilter('gdp', 'GDP', [
'> 10000' => function($m) { $m->addCondition('gdp', '>', 10000; },
'< 10000' => function($m) { $m->addCondition('gdp', '<', 10000; },

Calllback second argument

When specifying callback, second argument should contain selected value:

$yr = function($m, $v) {
  $m->addCondition($m->expr('year([date]) = []', [$v]));
$lookup->addFilter('date', 'Year', [ '2000'=>$yr, '2001'=>$yr ]);
$lookup->addFilter('date', 'Month', [ 'Jan'=>$m, ... ]);

This implies that we can have multiple filters with same field.


This comment has been minimized.

Copy link

skondakov commented Aug 28, 2018

This looks an interesting addon. I did not have the use for such component yet. I did integrate filtering in our ECommerce platform, but customers prefer the filtering to be done with checkboxes and do they are able to filter on more than one entry from filter category.

But this seems helpful for some basic filtering though.


This comment has been minimized.

Copy link

gowrav-vishwakarma commented Aug 28, 2018

Looks really helpful !!!

@ibelar ibelar referenced this issue Aug 29, 2018


Feature/lookup field #551

3 of 4 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment