A silverstripe dropdown field that has it's options populated via ajax, based on the value of the field it depends on
PHP JavaScript
Clone or download
Latest commit 0d725ba Feb 16, 2018
Permalink
Failed to load latest commit information.
client/js SS4 upgrade. Feb 16, 2018
src/Forms SS4 upgrade. Feb 16, 2018
.editorconfig Added standard .editorconfig file Dec 17, 2015
.scrutinizer.yml SS4 upgrade. Feb 16, 2018
.upgrade.yml SS4 upgrade. Feb 16, 2018
LICENSE.md Create LICENSE.md Sep 20, 2015
README.md Update README.md Feb 16, 2018
_config.php SS4 upgrade. Feb 16, 2018
composer.json SS4 upgrade. Feb 16, 2018

README.md

silverstripe-dependentdropdownfield

A SilverStripe dropdown field that has it's options populated via ajax, based on the value of the field it depends on.

Requirements

SilverStripe 4

Usage example

// 1. Create a callable function that returns an array of options for the DependentDropdownField. 
// When the value of the field it depends on changes, this function is called passing the 
// updated value as the first parameter ($val)
$datesSource = function($val) {	
	if ($val == 'one') {
		// return appropriate options array if the value is one.
	}
	if ($val == 'two') {
		// return appropriate options array if the value is two.
	}
}; 

$fields = FieldList::create(
	// 2. Add your first field to your field list, 
	$fieldOne = DropdownField::create('FieldOne', 'Field One', ['one' => 'One', 'two' => 'Two']),
	// 3. Add your DependentDropdownField, setting the source as the callable function 
	// you created and setting the field it depends on to the appropriate field
	DependentDropdownField::create('FieldTwo', 'Field Two', $datesSource)->setDepends($fieldOne)
);