I am trying to use the plugin Chained to restricted users from selecting a city for the wrong country. At the moment when I have no country selected it lets me choose from all the cities. But as soon as I select a country it will not let me select any cities. I've had this plugin working correctly using the code from the Chained website. My broken countries/cities version can be seen here login: guest password: password http://team.southpacificavionics.com/customers/add and my test version can be seen if you change "add" on that url to "test" (sorry I cant post more links). I've used ./cake bake to create my customers, countries and cities controllers, models, templates and key relationships.
You can see from this image that the cities are linked to countries
This is my add.ctp for customers
<?php echo $this->Html->script('jquery.min'); ?>
<?php echo $this->Html->script('jquery.chained'); ?>
<script type="text/javascript">
$(document).ready(function () {
$("#cities").chained("#countries");
});
</script>
<script type="text/javascript">
$(document).ready(function () {
alert('java is working');
});
</script>
<?php
/**
* @var \App\View\AppView $this
*/
?>
<div class="customers form large-9 medium-8 columns content">
<?= $this->Form->create($customer) ?>
<fieldset>
<legend><?= __('Add Customer') ?></legend>
<?php
echo $this->Form->input('country_id', ['options' => $countries, 'empty' => true,'id'=>'countries']);
echo $this->Form->input('city_id', ['options' => $cities, 'empty' => true,'id'=>'cities']);
?>
</fieldset>
<?= $this->Form->end() ?>
This is my mysql code
CREATE TABLE IF NOT EXISTS `southpac_team`.`customers` (
`id` INT NOT NULL,
`country_id` INT NULL,
`city_id` INT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `southpac_team`.`countries` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `southpac_team`.`cities` (
`id` INT NOT NULL AUTO_INCREMENT,
`country_id` INT NOT NULL,
`name` VARCHAR(100) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
You don't add class at your city dropdown options yet you should add country id as class in city dropdown
$items = $this->Cities->find('all')->all()->toArray();
$cities = [];
foreach ($items as $key => $value) {
$cities[$key]['value'] = $value['id'];
$cities[$key]['text'] = $value['indent_no'];
$cities[$key]['class'] = $value['country_id'];
}
for more check extra attribute in dorpdown