Skip to content

Add support for dependent / chained AssociativeField#7655

Open
Seb33300 wants to merge 1 commit into
EasyCorp:5.xfrom
Seb33300:dependent-AssociationField
Open

Add support for dependent / chained AssociativeField#7655
Seb33300 wants to merge 1 commit into
EasyCorp:5.xfrom
Seb33300:dependent-AssociationField

Conversation

@Seb33300

@Seb33300 Seb33300 commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Hello,

Fixes #4716

Here is a proposal to support dependent/chained dropdowns on AssociationField.
This allows the options of an AssociationField to vary based on the values selected in other form fields.

This feature only works on AssociationField with the autocomplete feature enabled.
You will also need to use setQueryBuilder() to filter your results. A new param $dependsOn has been added to the callable for this purpose.

Here is my proposal to support dependent/chained dropdowns on AssociationField.
This allows the options of an AssociationField to change based on the values selected in other form fields.

This feature only works with AssociationField when the autocomplete feature is enabled.
You will also need to use setQueryBuilder() to filter the results. For this purpose, a new $dependsOn parameter has been added to the callable.

Example usage:

yield CountryField::new('country');

yield AssociationField::new('state')
    ->autocomplete(
        // Specify the field(s) this AssociationField depends on
        dependsOn: 'country'
    )
    ->setQueryBuilder(function (QueryBuilder $queryBuilder, array $dependsOn) {
        return $queryBuilder
            ->andWhere('entity.country = :country')
            ->setParameter('country', $dependsOn['country']);
    })

You can also depend on multiple fields:

yield AssociationField::new('building');

yield NumberField::new('numberOfParticipants');

yield AssociationField::new('meetingRoom')
    ->autocomplete(
        dependsOn: ['building', 'numberOfParticipants']
    )
    ->setQueryBuilder(function (QueryBuilder $queryBuilder, array $dependsOn) {
        return $queryBuilder
            ->andWhere('entity.building = :building')
            ->andWhere('entity.capacity >= :numberOfParticipants')
            ->setParameter('building', $dependsOn['building'])
            ->setParameter('numberOfParticipants', $dependsOn['numberOfParticipants']);
    });

@Seb33300 Seb33300 changed the title [AssociationField] Add support for dependent / chained AssociativeField Add support for dependent / chained AssociativeField Jun 11, 2026
@Seb33300 Seb33300 force-pushed the dependent-AssociationField branch from b5467fa to 06f75f8 Compare June 11, 2026 06:45
@Seb33300 Seb33300 force-pushed the dependent-AssociationField branch from 06f75f8 to d037507 Compare June 11, 2026 06:49
@javiereguiluz javiereguiluz added this to the 5.x milestone Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dependent / Chained AssociativeField

2 participants