Skip to content

Commit

Permalink
Merge pull request #36 from silverstripeltd/pulls/silverstripe-5
Browse files Browse the repository at this point in the history
Update requirements to Silverstripe/PHP supported versions (incl Silverstripe 5)
  • Loading branch information
sheadawson authored Feb 22, 2023
2 parents dda6ba3 + e59daa7 commit bb05e74
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
# silverstripe-dependentdropdownfield

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

## Requirements

SilverStripe 4
SilverStripe 4 || 5

## Installation

```
composer require sheadawson/silverstripe-dependentdropdownfield
```

## Usage example

```php
// 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
// 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) {
$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,
// 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
// 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)
);
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
"issues": "https://github.com/sheadawson/silverstripe-dependentdropdownfield/issues"
},
"require": {
"php": ">=5.6",
"silverstripe/vendor-plugin": "^1.0",
"silverstripe/framework": "^4.0"
"php": ">=7.4",
"silverstripe/vendor-plugin": "^1 || ^2",
"silverstripe/framework": "^4.11 || ^5"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 3 additions & 4 deletions src/Forms/DependentDropdownField.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use SilverStripe\Control\Controller;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Convert;
use SilverStripe\ORM\Map;
use SilverStripe\View\Requirements;
use SilverStripe\Forms\FormField;
Expand Down Expand Up @@ -80,7 +79,7 @@ public function load($request)
}
}

$response->setBody(Convert::array2json($results));
$response->setBody(json_encode($results));

return $response;
}
Expand Down Expand Up @@ -154,7 +153,7 @@ public function getSource()
return $source;
}
}

/**
* @param \Closure $source
* @return $this
Expand All @@ -174,7 +173,7 @@ public function Field($properties = [])
if (!is_subclass_of(Controller::curr(), LeftAndMain::class)) {
Requirements::javascript('silverstripe/admin:thirdparty/jquery-entwine/dist/jquery.entwine-dist.js');
}

Requirements::javascript(
'sheadawson/silverstripe-dependentdropdownfield:client/js/dependentdropdownfield.js'
);
Expand Down

0 comments on commit bb05e74

Please sign in to comment.