Skip to content

Commit

Permalink
Sync with validator and html (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Jul 26, 2022
1 parent d1b8854 commit 761ef2d
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/Field/Base/BaseField.php
Expand Up @@ -170,7 +170,7 @@ private function renderOpenContainerAndContent(string $content): string
$attributes = $this->containerAttributes;
$this->prepareContainerAttributes($attributes);
if ($attributes !== []) {
$containerTag = $containerTag->attributes($attributes);
$containerTag = $containerTag->addAttributes($attributes);
}

return $containerTag->open()
Expand Down
2 changes: 1 addition & 1 deletion src/Field/Base/ButtonField.php
Expand Up @@ -179,7 +179,7 @@ final protected function generateInput(): string
->type($this->getType());

if (!empty($this->buttonAttributes)) {
$button = $button->attributes($this->buttonAttributes);
$button = $button->addAttributes($this->buttonAttributes);
}

$content = $this->renderContent();
Expand Down
2 changes: 1 addition & 1 deletion src/Field/ButtonGroup.php
Expand Up @@ -59,7 +59,7 @@ public function buttonAttributes(array $attributes): self
public function addButtonAttributes(array $attributes): self
{
$new = clone $this;
$new->widget = $this->widget->buttonAttributes($attributes);
$new->widget = $this->widget->addButtonAttributes($attributes);
return $new;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Field/CheckboxList.php
Expand Up @@ -45,7 +45,7 @@ public function checkboxAttributes(array $attributes): self
public function addCheckboxAttributes(array $attributes): self
{
$new = clone $this;
$new->widget = $this->widget->checkboxAttributes($attributes);
$new->widget = $this->widget->addCheckboxAttributes($attributes);
return $new;
}

Expand All @@ -65,7 +65,7 @@ public function individualInputAttributes(array $attributes): self
public function addIndividualInputAttributes(array $attributes): self
{
$new = clone $this;
$new->widget = $this->widget->individualInputAttributes($attributes);
$new->widget = $this->widget->addIndividualInputAttributes($attributes);
return $new;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Field/File.php
Expand Up @@ -190,7 +190,7 @@ protected function generateInput(): string
if ($this->uncheckValue !== null) {
$tag = $tag->uncheckValue($this->uncheckValue);
if (!empty($this->uncheckInputAttributes)) {
$tag = $tag->uncheckInputAttributes($this->uncheckInputAttributes);
$tag = $tag->addUncheckInputAttributes($this->uncheckInputAttributes);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Field/Part/Error.php
Expand Up @@ -166,7 +166,7 @@ protected function run(): string
}

return CustomTag::name($this->tag)
->attributes($this->attributes)
->addAttributes($this->attributes)
->content($message)
->encode($this->encode)
->render();
Expand Down
2 changes: 1 addition & 1 deletion src/Field/Part/Label.php
Expand Up @@ -142,7 +142,7 @@ protected function run(): string
}
}

$tag = Html::label($content)->attributes($labelAttributes);
$tag = Html::label($content)->addAttributes($labelAttributes);

if (!$this->encode) {
$tag = $tag->encode(false);
Expand Down
4 changes: 2 additions & 2 deletions src/Field/RadioList.php
Expand Up @@ -43,7 +43,7 @@ public function radioAttributes(array $attributes): self
public function addRadioAttributes(array $attributes): self
{
$new = clone $this;
$new->widget = $this->widget->radioAttributes($attributes);
$new->widget = $this->widget->addRadioAttributes($attributes);
return $new;
}

Expand All @@ -63,7 +63,7 @@ public function individualInputAttributes(array $attributes): self
public function addIndividualInputAttributes(array $attributes): self
{
$new = clone $this;
$new->widget = $this->widget->individualInputAttributes($attributes);
$new->widget = $this->widget->addIndividualInputAttributes($attributes);
return $new;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Field/Range.php
Expand Up @@ -233,7 +233,7 @@ protected function generateInput(): string
$tag = $tag
->showOutput()
->outputTag($this->outputTag)
->outputAttributes($this->outputAttributes);
->addOutputAttributes($this->outputAttributes);
}

return $tag->render();
Expand Down
2 changes: 1 addition & 1 deletion src/Field/Select.php
Expand Up @@ -294,7 +294,7 @@ protected function generateInput(): string
$selectAttributes = $this->getInputAttributes();

return $this->select
->attributes($selectAttributes)
->addAttributes($selectAttributes)
->name($this->getInputName())
->values($value)
->render();
Expand Down
11 changes: 6 additions & 5 deletions src/FormModelInterface.php
Expand Up @@ -81,11 +81,12 @@ public function getFormName(): string;
* {
* return [
* new \Yiisoft\Validator\Rule\Required(),
* (new \Yiisoft\Validator\Rule\HasLength())
* ->min(4)
* ->max(40)
* ->tooShortMessage('Is too short.')
* ->tooLongMessage('Is too long.'),
* new \Yiisoft\Validator\Rule\HasLength(
* min: 4,
* max: 40,
* lessThanMinMessage: 'Is too short.',
* greaterThanMaxMessage: 'Is too long.',
* ),
* new \Yiisoft\Validator\Rule\Email()
* ];
* }
Expand Down
20 changes: 10 additions & 10 deletions tests/Helper/HtmlFormErrorsTest.php
Expand Up @@ -32,7 +32,7 @@ public function testClearAllErrors(): void
$validator = $this->createValidatorMock();
$this->assertTrue($formModel->load($this->data));
$this->assertFalse($validator
->validate($formModel)
->validate($formModel, $formModel->getRules())
->isValid());
$this->assertSame($this->expected, HtmlFormErrors::getAllErrors($formModel));
$formModel->getFormErrors()->clear();
Expand All @@ -45,7 +45,7 @@ public function testClearForAttribute(): void
$validator = $this->createValidatorMock();
$this->assertTrue($formModel->load($this->data));
$this->assertFalse($validator
->validate($formModel)
->validate($formModel, $formModel->getRules())
->isValid());
$this->assertSame($this->expected, HtmlFormErrors::getAllErrors($formModel));
$this->assertEmpty($formModel
Expand All @@ -60,7 +60,7 @@ public function testGetAllErrors(): void
$validator = $this->createValidatorMock();
$this->assertTrue($formModel->load($this->data));
$this->assertFalse($validator
->validate($formModel)
->validate($formModel, $formModel->getRules())
->isValid());
$this->assertSame($this->expected, HtmlFormErrors::getAllErrors($formModel));
}
Expand All @@ -71,7 +71,7 @@ public function testGetErrors(): void
$validator = $this->createValidatorMock();
$this->assertTrue($formModel->load($this->data));
$this->assertFalse($validator
->validate($formModel)
->validate($formModel, $formModel->getRules())
->isValid());
$this->assertSame(['This value is not a valid email address.'], HtmlFormErrors::getErrors($formModel, 'login'));
}
Expand All @@ -82,7 +82,7 @@ public function testGetErrorSummary(): void
$validator = $this->createValidatorMock();
$this->assertTrue($formModel->load($this->data));
$this->assertFalse($validator
->validate($formModel)
->validate($formModel, $formModel->getRules())
->isValid());
$this->assertSame(
['This value is not a valid email address.', 'Is too short.'],
Expand All @@ -96,7 +96,7 @@ public function testGetErrorSummaryFirstErrors(): void
$validator = $this->createValidatorMock();
$this->assertTrue($formModel->load($this->data));
$this->assertFalse($validator
->validate($formModel)
->validate($formModel, $formModel->getRules())
->isValid());
$this->assertSame(
['login' => 'This value is not a valid email address.', 'password' => 'Is too short.'],
Expand All @@ -110,7 +110,7 @@ public function testGetFirstError(): void
$validator = $this->createValidatorMock();
$this->assertTrue($formModel->load($this->data));
$this->assertFalse($validator
->validate($formModel)
->validate($formModel, $formModel->getRules())
->isValid());
$this->assertSame(
'This value is not a valid email address.',
Expand All @@ -134,7 +134,7 @@ public function testHasError(): void
$validator = $this->createValidatorMock();
$this->assertTrue($formModel->load($this->data));
$this->assertFalse($validator
->validate($formModel)
->validate($formModel, $formModel->getRules())
->isValid());
$this->assertTrue(HtmlFormErrors::hasErrors($formModel));
}
Expand All @@ -145,7 +145,7 @@ public function testGetErrorSummaryOnlyAttributes(): void
$validator = $this->createValidatorMock();
$this->assertTrue($formModel->load($this->data));
$this->assertFalse($validator
->validate($formModel)
->validate($formModel, $formModel->getRules())
->isValid());
$this->assertSame(
['This value is not a valid email address.'],
Expand All @@ -163,7 +163,7 @@ public function testGetErrorNestedAttribute(): void
$validator = $this->createValidatorMock();
$this->assertTrue($formModel->load(['FormWithNestedAttribute' => ['user.login' => 'ad']]));
$this->assertFalse($validator
->validate($formModel)
->validate($formModel, $formModel->getRules())
->isValid());
$this->assertSame(
['id' => 'Value cannot be blank.', 'user.login' => 'Is too short.'],
Expand Down
2 changes: 1 addition & 1 deletion tests/TestSupport/Form/FormWithNestedAttribute.php
Expand Up @@ -46,7 +46,7 @@ public function getRules(): array
'id' => [new Required()],
'user.login' => [
new Required(),
new HasLength(min: 3, tooShortMessage: 'Is too short.'),
new HasLength(min: 3, lessThanMinMessage: 'Is too short.'),
],
];
}
Expand Down
4 changes: 2 additions & 2 deletions tests/TestSupport/Form/LoginForm.php
Expand Up @@ -83,7 +83,7 @@ private function loginRules(): array
{
return [
new Required(),
new HasLength(min: 4, max: 40, tooShortMessage: 'Is too short.', tooLongMessage: 'Is too long.'),
new HasLength(min: 4, max: 40, lessThanMinMessage: 'Is too short.', greaterThanMaxMessage: 'Is too long.'),
new Email(),
];
}
Expand All @@ -92,7 +92,7 @@ private function passwordRules(): array
{
return [
new Required(),
new HasLength(min: 8, tooShortMessage: 'Is too short.'),
new HasLength(min: 8, lessThanMinMessage: 'Is too short.'),
];
}
}
2 changes: 1 addition & 1 deletion tests/TestSupport/Validator/ValidatorMock.php
Expand Up @@ -18,7 +18,7 @@ public function __construct()
$this->validator = new Validator(new SimpleRuleHandlerContainer());
}

public function validate($data, iterable $rules = []): Result
public function validate($data, ?iterable $rules = []): Result
{
return $this->validator->validate($data, $rules);
}
Expand Down

0 comments on commit 761ef2d

Please sign in to comment.