diff --git a/README.md b/README.md index c2e85fe..b2f2a50 100644 --- a/README.md +++ b/README.md @@ -35,14 +35,22 @@ Disclaimer: This extension and/or its documentation is nowhere near complete. Pr This extension adds following methods to `Nette\Forms\Container` and thus to all derived classes: -- `addDynamicSelect($name, $title, $items, $itemFactory)` +- `addDynamicSelect($name, $title, $items, $itemFactory = null, $config = [])` - dynamic select with one value -- `addDynamicMultiSelect($name, $title, $items, $itemFactory)` +- `addDynamicMultiSelect($name, $title, $items, $itemFactory = null, $config = [])` - dynamic select with multiple values -- `addAjaxSelect($name, $title, $entityName = $name)` +- `addAjaxSelect($name, $title, $entityName = $name, $config = [])` - ajax select with one value -- `addAjaxMultiSelect($name, $title, $entityName = $name)` +- `addAjaxMultiSelect($name, $title, $entityName = $name, $config = [])` - ajax select with multiple values + +### Config + +```php +[ + AjaxSelectExtension::CONFIG_INVALID_VALUE_MODE => AjaxSelectExtension::INVALID_VALUE_MODE_*, +] +``` ### Dynamic Select diff --git a/src/Components/AjaxSelect/DI/AjaxSelectExtension.php b/src/Components/AjaxSelect/DI/AjaxSelectExtension.php index fe8694d..835e347 100644 --- a/src/Components/AjaxSelect/DI/AjaxSelectExtension.php +++ b/src/Components/AjaxSelect/DI/AjaxSelectExtension.php @@ -84,21 +84,23 @@ public function afterCompile(\Nette\PhpGenerator\ClassType $class) { $initialize->addBody(__CLASS__ . '::register($this, ?);', [ $this->config ]); } - public static function register(\Nette\DI\Container $container, $config) { + public static function register(\Nette\DI\Container $container, $globalConfig) { // lazy service getter $serviceGetter = function () use ($container) { return $container->getByType(AjaxSelect\Services\AjaxService::class); }; // control factory factory :) - $factory = function ($class) use ($serviceGetter, $config) { + $factory = function ($class) use ($serviceGetter, $globalConfig) { if (in_array($class, [AjaxSelect\AjaxSelect::class, AjaxSelect\AjaxMultiSelect::class])) { // pro ajax entity - return function (\Nette\Forms\Container $container, $name, $label = NULL, $entityName = NULL) use ($class, $serviceGetter, $config) { + return function (\Nette\Forms\Container $container, $name, $label = NULL, $entityName = NULL, $config = []) use ($class, $serviceGetter, $globalConfig) { /** @var AjaxSelect\AjaxSelect|AjaxSelect\DynamicSelect|mixed $control */ $control = new $class($label); + $config = array_intersect_key($config, array_flip([static::CONFIG_INVALID_VALUE_MODE])) + $globalConfig; + // set invalid value mode $control->setInvalidValueMode($config[static::CONFIG_INVALID_VALUE_MODE]); @@ -115,10 +117,12 @@ public static function register(\Nette\DI\Container $container, $config) { } elseif (in_array($class, [AjaxSelect\DynamicSelect::class, AjaxSelect\DynamicMultiSelect::class])) { // pro dymanic select - return function (\Nette\Forms\Container $container, $name, $label = NULL, $items = NULL, $itemFactory = NULL) use ($class, $serviceGetter, $config) { + return function (\Nette\Forms\Container $container, $name, $label = NULL, $items = NULL, $itemFactory = NULL, $config = []) use ($class, $serviceGetter, $globalConfig) { /** @var AjaxSelect\AjaxSelect|AjaxSelect\DynamicSelect|mixed $control */ $control = new $class($label, $items); + $config = array_intersect_key($config, array_flip([static::CONFIG_INVALID_VALUE_MODE])) + $globalConfig; + // set invalid value mode $control->setInvalidValueMode($config[static::CONFIG_INVALID_VALUE_MODE]);