Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move out setting of validation rules enricher from theme container #351

Merged
merged 8 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/mutation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ jobs:
['ubuntu-latest']
php: >-
['8.2']
bin-name: infection
secrets:
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
"yiisoft/widget": "^2.2"
},
"require-dev": {
"infection/infection": "^0.26.19",
"maglnet/composer-require-checker": "^4.7",
"phpunit/phpunit": "^10.5",
"rector/rector": "^1.0.0",
"roave/infection-static-analysis-plugin": "^1.34",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.21"
},
Expand Down
1 change: 0 additions & 1 deletion config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ static function () use ($params) {
ThemeContainer::initialize(
$params['yiisoft/form']['themes'],
$params['yiisoft/form']['defaultTheme'],
$params['yiisoft/form']['validationRulesEnricher'],
);
},
];
1 change: 0 additions & 1 deletion config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
'default' => [],
],
'defaultTheme' => 'default',
'validationRulesEnricher' => null,
],
];
14 changes: 6 additions & 8 deletions docs/guide/en/field-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ and has validation errors.
and has validation errors.
- [`enrichFromValidationRules`](field-methods.md#enrichfromvalidationrules) - whether to
[enrich](validation-rules-enrichment.md) this field from validation rules.
- [`validationRulesEnricher`](field-methods.md#validationrulesenricher) -
[validation rules enricher](validation-rules-enrichment.md) instance.
- `fieldConfigs` - configuration sets by field type declared using [definitions](https://github.com/yiisoft/definitions)
syntax.

Expand Down Expand Up @@ -91,15 +93,10 @@ ThemeContainer::initialize(
],
],
defaultConfig: 'main',
validationRulesEnricher: new MyValidationRulesEnricher(),
);
```

You can additionally set (optional):

- config used as a default one using `defaultConfig` option;
- [enricher](validation-rules-enrichment.md) instance used to enrich fields depending on their type from given
validation rules.
You can additionally set (optional) config used as a default one using `defaultConfig` option.

## Built-in themes

Expand All @@ -124,6 +121,8 @@ ThemeContainer::initialize(
);
```

In built-in themes no [validation rules enrichers](validation-rules-enrichment.md) are used.

## Using with Yii Config

When using [Yii Config](https://github.com/yiisoft/config), there is no need to manually interact with theme cotnainer,
Expand All @@ -141,10 +140,9 @@ return [
'horizontal' => require ThemePath::BOOTSTRAP5_HORIZONTAL,
],
'defaultTheme' => 'vertical',
'validationRulesEnricher' => new MyValidationRulesEnricher(),
],
// ...
];
```

No built-in themes and validation rules enricher are used by default.
No built-in themes are used by default.
13 changes: 11 additions & 2 deletions docs/guide/en/field-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -1497,8 +1497,6 @@ Built-in fields that implement `EnrichFromValidationRulesInterface`:
Whether to enrich this field from validation rules.

```php
use Yiisoft\Form\Theme\ThemeContainer;

echo Yiisoft\Form\Field\Email::widget()->enrichFromValidationRules();
```

Expand All @@ -1508,6 +1506,17 @@ Pass `false` to not enrich this field from validation rules (default when method
echo Yiisoft\Form\Field\Email::widget()->enrichFromValidationRules(false);
```

### `validationRulesEnricher()`

[Validation rules enricher](validation-rules-enrichment.md) instance. Enrichment must be activated via
[`enrichFromValidationRules`](#enrichfromvalidationrules) in order for this method to take effect.

```php
echo Yiisoft\Form\Field\Email::widget()
->enrichFromValidationRules()
->validationRulesEnricher(new MyValidationRulesEnricher());
```

## `ValidationClassInterface` implemented fields

With these fields it's possible to reflect validation state, either on the input or on the field container.
Expand Down
5 changes: 4 additions & 1 deletion docs/guide/en/validation-rules-enrichment.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ final class MyTextField extends InputField
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
arogachev marked this conversation as resolved.
Show resolved Hide resolved
?? [];
}
}

Expand Down
5 changes: 2 additions & 3 deletions docs/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ The package is tested with [PHPUnit](https://phpunit.de/). To run tests:

## Mutation testing

The package tests are checked with [Infection](https://infection.github.io/) mutation framework with
[Infection Static Analysis Plugin](https://github.com/Roave/infection-static-analysis-plugin). To run it:
The package tests are checked with [Infection](https://infection.github.io/) mutation framework. To run it:

```shell
./vendor/bin/roave-infection-static-analysis-plugin
./vendor/bin/infection
```

## Static analysis
Expand Down
6 changes: 4 additions & 2 deletions src/Field/Base/DateTimeInputField.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Yiisoft\Form\Field\Base\EnrichFromValidationRules\EnrichFromValidationRulesTrait;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

use function is_string;
Expand Down Expand Up @@ -143,7 +142,10 @@ final public function disabled(bool $disabled = true): static
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
arogachev marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ interface EnrichFromValidationRulesInterface
* Enable/disable enrichment based on validation rules.
*/
public function enrichFromValidationRules(bool $enrich): self;

public function validationRulesEnricher(?ValidationRulesEnricherInterface $enricher): self;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@

namespace Yiisoft\Form\Field\Base\EnrichFromValidationRules;

use Yiisoft\Form\ValidationRulesEnricherInterface;

trait EnrichFromValidationRulesTrait
{
private bool $enrichFromValidationRules = false;
private ?ValidationRulesEnricherInterface $validationRulesEnricher = null;

public function enrichFromValidationRules(bool $enrich = true): self
{
$new = clone $this;
$new->enrichFromValidationRules = $enrich;
return $new;
}

public function validationRulesEnricher(?ValidationRulesEnricherInterface $enricher): self
{
$new = clone $this;
$new->validationRulesEnricher = $enricher;
return $new;
}
}
6 changes: 4 additions & 2 deletions src/Field/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

use function is_string;
Expand Down Expand Up @@ -201,7 +200,10 @@ public function tabIndex(?int $value): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Field/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Yiisoft\Form\Field\Base\InputField;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

/**
Expand Down Expand Up @@ -154,7 +153,10 @@ public function addUncheckInputAttributes(array $attributes): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Field/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

/**
Expand Down Expand Up @@ -162,7 +161,10 @@ public function tabIndex(?int $value): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Field/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

use function is_string;
Expand Down Expand Up @@ -192,7 +191,10 @@ public function size(?int $value): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Field/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Yiisoft\Form\Field\Base\InputField;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

use function is_string;
Expand Down Expand Up @@ -190,7 +189,10 @@ public function addOutputAttributes(array $attributes): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Field/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Yiisoft\Form\Field\Base\InputField;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Tag\Optgroup;
use Yiisoft\Html\Tag\Option;
use Yiisoft\Html\Tag\Select as SelectTag;
Expand Down Expand Up @@ -241,7 +240,10 @@ public function unselectValue(bool|float|int|string|Stringable|null $value): sel
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Field/Telephone.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

use function is_string;
Expand Down Expand Up @@ -187,7 +186,10 @@ public function size(?int $value): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Field/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

use function is_string;
Expand Down Expand Up @@ -203,7 +202,10 @@ public function tabIndex(?int $value): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Field/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

use function is_string;
Expand Down Expand Up @@ -218,7 +217,10 @@ public function wrap(?string $value): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Field/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

use function is_string;
Expand Down Expand Up @@ -187,7 +186,10 @@ public function size(?int $value): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
Loading