Skip to content

Commit 84663c0

Browse files
committed
docs for dependent fields
1 parent f7966bc commit 84663c0

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,88 @@ $builder
241241
]);
242242
```
243243

244+
### Including other field values in request
245+
246+
If you need to include other field values because the selection depends on it you can add the `req_params` option.
247+
The key is the name of the parameter in the query string, the value is the path in the FormView (if you don't know the path you can do something like `{{ dump(form) }}` in your template.)
248+
249+
The `property` option refers to your entity field which is used for the label as well as for the search term.
250+
251+
In the callback you get the QueryBuilder to modify the result query and the data object as parameter (data can be a simple Request object or a plain array. See AutocompleteService.php for more details).
252+
253+
```php
254+
$builder
255+
->add('firstName', TextType::class)
256+
->add('lastName', TextType::class)
257+
->add('state', EntityType::class, array('class' => State::class))
258+
->add('county', Select2EntityType::class, [
259+
'required' => true,
260+
'multiple' => false,
261+
'remote_route' => 'ajax_autocomplete',
262+
'class' => County::class,
263+
'minimum_input_length' => 0,
264+
'page_limit' => 10,
265+
'scroll' => true,
266+
'allow_clear' => false,
267+
'req_params' => ['state' => 'parent.children[state]'],
268+
'property' => 'name',
269+
'callback' => function (QueryBuilder $qb, $data) {
270+
$qb->andWhere('e.state = :state');
271+
272+
if ($data instanceof Request) {
273+
$qb->setParameter('state', $data->get('state'));
274+
} else {
275+
$qb->setParameter('state', $data['state']);
276+
}
277+
278+
},
279+
])
280+
->add('city', Select2EntityType::class, [
281+
'required' => true,
282+
'multiple' => false,
283+
'remote_route' => 'ajax_autocomplete',
284+
'class' => City::class,
285+
'minimum_input_length' => 0,
286+
'page_limit' => 10,
287+
'scroll' => true,
288+
'allow_clear' => false,
289+
'req_params' => ['county' => 'parent.children[county]'],
290+
'property' => 'name',
291+
'callback' => function (QueryBuilder $qb, $data) {
292+
$qb->andWhere('e.county = :county');
293+
294+
if ($data instanceof Request) {
295+
$qb->setParameter('county', $data->get('county'));
296+
} else {
297+
$qb->setParameter('county', $data['county']);
298+
}
299+
300+
},
301+
]);
302+
```
303+
304+
Because the handling of requests is usually very similar you can use a service which helps you with your results. To use it just add a route in one of your controllers:
305+
306+
```php
307+
/**
308+
* @param Request $request
309+
*
310+
* @Route("/autocomplete", name="ajax_autocomplete")
311+
*
312+
* @return Response
313+
*/
314+
public function autocompleteAction(Request $request)
315+
{
316+
// Check security etc. if needed
317+
318+
$as = $this->get('tetranz_select2entity.autocomplete_service');
319+
320+
$result = $as->getAutocompleteResults($request, YourFormType::class);
321+
322+
return new JsonResponse($result);
323+
}
324+
```
325+
244326

245327
### Templating
246328

0 commit comments

Comments
 (0)