setDependentRelation

setDependentRelation(string $fieldName, string $dependencyFromField, string $fieldNameRelation)
Available from version >= 2.6.1

There are cases that dropdown list that was built from setRelation has dependencies between them. The most common example that we see very often on registration forms is that the continent dropdown will filter the countries and countries dropdown will filter the cities.

You can see an example of the expected results here:

Depended Relation Animated

The usage is coming with an extra line of code that is looking like this:

$crud->setDependentRelation('relationFieldName', 'dependencyFieldName', 'parentId')

So for example if we have a country setRelation and a city setRelation that looks like this:

$crud->setRelation('country_id','countries','name');
$crud->setRelation('city_id','cities','name');

If the city_id is depended (filterted) from country_id then we will need to add only one extra line that will look like this:

$crud->setDependentRelation('city_id','country_id','country');

With this line of code the result will be that when the country changes then it filters the city_id dropdown will have filtered only the ones from this country.

You can see a full example below:

$crud->setTable('customers_db');
$crud->setSubject('Customer', 'Customers');

$crud->displayAs('continent_id', 'Continent');
$crud->displayAs('country_id', 'Country');
$crud->displayAs('city_id', 'City');

$crud->setRelation('continent_id','continents','name');
$crud->setRelation('country_id','countries','name');
$crud->setRelation('city_id','cities','name');

$crud->setDependentRelation('country_id','continent_id','continent_code');
$crud->setDependentRelation('city_id','country_id','country');

$output = $output->render();

In case you would like to see the results of the above example we do have the data copied here: https://gist.github.com/scoumbourdis/bc72fce984fedb2d57e4256bc438c4ae