sheadawson/silverstripe-dependentdropdownfield

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

Installs: 190 692

Dependents: 15

Suggesters: 0

Security: 0

Stars: 28

Watchers: 8

Forks: 36

Open Issues: 9

Type:silverstripe-vendormodule

3.0.0 2023-02-22 18:12 UTC

This package is auto-updated.

Last update: 2024-10-23 01:24:10 UTC


README

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

Requirements

SilverStripe 4 || 5

Installation

composer require sheadawson/silverstripe-dependentdropdownfield

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)
);