Skip to content
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

Update docs #6336

Merged
merged 1 commit into from
Aug 28, 2020
Merged
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
49 changes: 35 additions & 14 deletions docs/reference/form_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -267,20 +267,41 @@ The available options are:

From the ``$admin`` parameter it is possible to get the ``Datagrid`` and the ``Request``::

$formMapper
->add('category', ModelAutocompleteType::class, [
'property' => 'title',
'callback' => function ($admin, $property, $value) {
$datagrid = $admin->getDatagrid();
$queryBuilder = $datagrid->getQuery();
$queryBuilder
->andWhere($queryBuilder->getRootAlias() . '.foo=:barValue')
->setParameter('barValue', $admin->getRequest()->get('bar'))
;
$datagrid->setValue($property, null, $value);
},
])
;
$formMapper
->add('category', ModelAutocompleteType::class, [
'property' => 'title',
'callback' => static function (AdminInterface $admin, string $property, $value): void {
$datagrid = $admin->getDatagrid();
$queryBuilder = $datagrid->getQuery();
$queryBuilder
->andWhere($queryBuilder->getRootAlias() . '.foo=:barValue')
->setParameter('barValue', $admin->getRequest()->get('bar'))
;
$datagrid->setValue($property, null, $value);
},
])
;

If you want to dynamically change the ``property`` being filtered on to something else,
you can use a prefix system, as follows.
When the user types **id: 20** the property used for filtering is "id".
When they type **username: awesome_user_name**, it will be "username"::

$formMapper
->add('category', ModelAutocompleteType::class, [
'property' => 'title',
'callback' => static function (AdminInterface $admin, string $property, string $value): void {
$datagrid = $admin->getDatagrid();

$valueParts = explode(':', $value);
if (count($valueParts) === 2 && in_array($valueParts[0], ['id', 'email', 'username'])) {
[$property, $value] = $valueParts;
}

$datagrid->setValue($datagrid->getFilter($property)->getFormName(), null, $value);
},
])
;

``to_string_callback``
defaults to ``null``. Callable function that can be used to change the default toString behavior of entity::
Expand Down