Skip to content

Commit

Permalink
Ability to set custom invalidValueMode in constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
michallohnisky committed Oct 23, 2018
1 parent e124c89 commit c6169e9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions src/Components/AjaxSelect/DI/AjaxSelectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand All @@ -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]);

Expand Down

0 comments on commit c6169e9

Please sign in to comment.