Skip to content

Commit

Permalink
Fix #200: Don't enrich fields from rules with non-null "when" paramet…
Browse files Browse the repository at this point in the history
…er (#209)
  • Loading branch information
vjik committed Jun 16, 2022
1 parent 79bb78a commit 087da0f
Show file tree
Hide file tree
Showing 33 changed files with 162 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Field/Base/DateTimeInputField.php
Expand Up @@ -11,6 +11,7 @@
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Html\Html;
use Yiisoft\Validator\BeforeValidationInterface;
use Yiisoft\Validator\Rule\Required;

use function is_string;
Expand Down Expand Up @@ -148,6 +149,10 @@ protected function beforeRender(): void
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
}

if ($rule instanceof Required) {
$this->inputAttributes['required'] = true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Field/Email.php
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Html\Html;
use Yiisoft\Validator\BeforeValidationInterface;
use Yiisoft\Validator\Rule\HasLength;
use Yiisoft\Validator\Rule\Regex;
use Yiisoft\Validator\Rule\Required;
Expand Down Expand Up @@ -209,6 +210,10 @@ protected function beforeRender(): void
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
}

if ($rule instanceof Required) {
$this->inputAttributes['required'] = true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Field/File.php
Expand Up @@ -12,6 +12,7 @@
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Html\Html;
use Yiisoft\Validator\BeforeValidationInterface;
use Yiisoft\Validator\Rule\Required;

/**
Expand Down Expand Up @@ -161,6 +162,10 @@ protected function beforeRender(): void
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
}

if ($rule instanceof Required) {
$this->inputAttributes['required'] = true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Field/Number.php
Expand Up @@ -14,6 +14,7 @@
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Html\Html;
use Yiisoft\Validator\BeforeValidationInterface;
use Yiisoft\Validator\Rule\Number as NumberRule;
use Yiisoft\Validator\Rule\Required;

Expand Down Expand Up @@ -169,6 +170,10 @@ protected function beforeRender(): void
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
}

if ($rule instanceof Required) {
$this->inputAttributes['required'] = true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Field/Password.php
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Html\Html;
use Yiisoft\Validator\BeforeValidationInterface;
use Yiisoft\Validator\Rule\HasLength;
use Yiisoft\Validator\Rule\Regex;
use Yiisoft\Validator\Rule\Required;
Expand Down Expand Up @@ -200,6 +201,10 @@ protected function beforeRender(): void
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
}

if ($rule instanceof Required) {
$this->inputAttributes['required'] = true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Field/Range.php
Expand Up @@ -12,6 +12,7 @@
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Html\Html;
use Yiisoft\Validator\BeforeValidationInterface;
use Yiisoft\Validator\Rule\Number as NumberRule;
use Yiisoft\Validator\Rule\Required;

Expand Down Expand Up @@ -197,6 +198,10 @@ protected function beforeRender(): void
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
}

if ($rule instanceof Required) {
$this->inputAttributes['required'] = true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Field/Select.php
Expand Up @@ -14,6 +14,7 @@
use Yiisoft\Html\Tag\Optgroup;
use Yiisoft\Html\Tag\Option;
use Yiisoft\Html\Tag\Select as SelectTag;
use Yiisoft\Validator\BeforeValidationInterface;
use Yiisoft\Validator\Rule\Required;

/**
Expand Down Expand Up @@ -249,6 +250,10 @@ protected function beforeRender(): void
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
}

if ($rule instanceof Required) {
$this->inputAttributes['required'] = true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Field/Telephone.php
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Html\Html;
use Yiisoft\Validator\BeforeValidationInterface;
use Yiisoft\Validator\Rule\HasLength;
use Yiisoft\Validator\Rule\Regex;
use Yiisoft\Validator\Rule\Required;
Expand Down Expand Up @@ -195,6 +196,10 @@ protected function beforeRender(): void
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
}

if ($rule instanceof Required) {
$this->inputAttributes['required'] = true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Field/Text.php
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Html\Html;
use Yiisoft\Validator\BeforeValidationInterface;
use Yiisoft\Validator\Rule\HasLength;
use Yiisoft\Validator\Rule\Regex;
use Yiisoft\Validator\Rule\Required;
Expand Down Expand Up @@ -211,6 +212,10 @@ protected function beforeRender(): void
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
}

if ($rule instanceof Required) {
$this->inputAttributes['required'] = true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Field/Textarea.php
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Html\Html;
use Yiisoft\Validator\BeforeValidationInterface;
use Yiisoft\Validator\Rule\HasLength;
use Yiisoft\Validator\Rule\Required;

Expand Down Expand Up @@ -225,6 +226,10 @@ protected function beforeRender(): void
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
}

if ($rule instanceof Required) {
$this->inputAttributes['required'] = true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Field/Url.php
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Html\Html;
use Yiisoft\Validator\BeforeValidationInterface;
use Yiisoft\Validator\Rule\HasLength;
use Yiisoft\Validator\Rule\Regex;
use Yiisoft\Validator\Rule\Required;
Expand Down Expand Up @@ -196,6 +197,10 @@ protected function beforeRender(): void
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
}

if ($rule instanceof Required) {
$this->inputAttributes['required'] = true;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Field/Base/DateTimeInputFieldTest.php
Expand Up @@ -189,6 +189,23 @@ public function testEnrichmentFromRules(): void
$this->assertSame($expected, $result);
}

public function testEnrichmentFromRulesWithWhen(): void
{
$result = StubDateTimeInputField::widget()
->formAttribute(new DateForm(), 'second')
->hideLabel()
->enrichmentFromRules(true)
->render();

$expected = <<<HTML
<div>
<input type="datetime" id="dateform-second" name="DateForm[second]">
</div>
HTML;

$this->assertSame($expected, $result);
}

public function testInvalidValue(): void
{
$widget = StubDateTimeInputField::widget()
Expand Down
4 changes: 4 additions & 0 deletions tests/Field/EmailTest.php
Expand Up @@ -268,6 +268,10 @@ public function dataEnrichmentFromRules(): array
'<input type="email" id="emailform-nocode" name="EmailForm[nocode]">',
'nocode',
],
'required-with-when' => [
'<input type="email" id="emailform-requiredwhen" name="EmailForm[requiredWhen]">',
'requiredWhen',
],
];
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Field/FileTest.php
Expand Up @@ -248,6 +248,23 @@ public function testEnrichmentFromRules(): void
$this->assertSame($expected, $result);
}

public function testEnrichmentFromRulesWithWhen(): void
{
$result = File::widget()
->formAttribute(new FileForm(), 'photo')
->hideLabel()
->enrichmentFromRules(true)
->render();

$expected = <<<HTML
<div>
<input type="file" id="fileform-photo" name="FileForm[photo]">
</div>
HTML;

$this->assertSame($expected, $result);
}

public function testInvalidValue(): void
{
$field = File::widget()
Expand Down
4 changes: 4 additions & 0 deletions tests/Field/NumberTest.php
Expand Up @@ -300,6 +300,10 @@ public function dataEnrichmentFromRules(): array
'<input type="number" id="numberform-step" name="NumberForm[step]" min="5" max="95">',
'step',
],
'required-with-when' => [
'<input type="number" id="numberform-requiredwhen" name="NumberForm[requiredWhen]">',
'requiredWhen',
],
];
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Field/PasswordTest.php
Expand Up @@ -230,6 +230,10 @@ public function dataEnrichmentFromRules(): array
'<input type="password" id="passwordform-nocode" name="PasswordForm[nocode]">',
'nocode',
],
'required-with-when' => [
'<input type="password" id="passwordform-requiredwhen" name="PasswordForm[requiredWhen]">',
'requiredWhen',
],
];
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Field/RangeTest.php
Expand Up @@ -352,6 +352,10 @@ public function dataEnrichmentFromRules(): array
'<input type="range" id="rangeform-count" name="RangeForm[count]" min="1" max="9">',
'count',
],
'required-with-when' => [
'<input type="range" id="rangeform-requiredwhen" name="RangeForm[requiredWhen]">',
'requiredWhen',
],
];
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Field/SelectTest.php
Expand Up @@ -596,6 +596,25 @@ public function testEnrichmentFromRules(): void
$this->assertSame($expected, $result);
}

public function testEnrichmentFromRulesWithWhen(): void
{
$result = Select::widget()
->formAttribute(new SelectForm(), 'requiredWhen')
->optionsData(['red' => 'Red'])
->enrichmentFromRules(true)
->hideLabel()
->useContainer(false)
->render();

$expected = <<<HTML
<select id="selectform-requiredwhen" name="SelectForm[requiredWhen]">
<option value="red">Red</option>
</select>
HTML;

$this->assertSame($expected, $result);
}

public function testImmutability(): void
{
$field = Select::widget();
Expand Down
4 changes: 4 additions & 0 deletions tests/Field/TelephoneTest.php
Expand Up @@ -220,6 +220,10 @@ public function dataEnrichmentFromRules(): array
'<input type="tel" id="telephoneform-nocode" name="TelephoneForm[nocode]">',
'nocode',
],
'required-with-when' => [
'<input type="tel" id="telephoneform-requiredwhen" name="TelephoneForm[requiredWhen]">',
'requiredWhen',
],
];
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Field/TextTest.php
Expand Up @@ -672,6 +672,10 @@ public function dataEnrichmentFromRules(): array
'<input type="text" id="textform-nocode" name="TextForm[nocode]" value>',
'nocode',
],
'required-with-when' => [
'<input type="text" id="textform-requiredwhen" name="TextForm[requiredWhen]">',
'requiredWhen',
],
];
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Field/TextareaTest.php
Expand Up @@ -251,6 +251,10 @@ public function dataEnrichmentFromRules(): array
'<textarea id="textareaform-shortdesc" name="TextareaForm[shortdesc]" maxlength="199" minlength="10"></textarea>',
'shortdesc',
],
'required-with-when' => [
'<textarea id="textareaform-requiredwhen" name="TextareaForm[requiredWhen]"></textarea>',
'requiredWhen',
],
];
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Field/UrlTest.php
Expand Up @@ -254,6 +254,10 @@ public function dataEnrichmentFromRules(): array
'<input type="url" id="urlform-beach2" name="UrlForm[beach2]" value pattern="^((?i)http|https):\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)">',
'beach2',
],
'required-with-when' => [
'<input type="url" id="urlform-requiredwhen" name="UrlForm[requiredWhen]">',
'requiredWhen',
],
];
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Support/Form/DateForm.php
Expand Up @@ -13,12 +13,14 @@ final class DateForm extends FormModel
private ?string $startDate = null;
private ?string $endDate = null;
private ?string $main = null;
private ?string $second = null;
private int $age = 42;

public function getRules(): array
{
return [
'main' => [new Required()],
'second' => [new Required(when: static fn () => false)],
];
}

Expand Down

0 comments on commit 087da0f

Please sign in to comment.