Skip to content

Commit

Permalink
Add validation classes when used custom error (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Dec 26, 2023
1 parent b92c2f3 commit 7beb9da
Show file tree
Hide file tree
Showing 30 changed files with 499 additions and 32 deletions.
12 changes: 10 additions & 2 deletions src/Field/Base/DateTimeInputField.php
Expand Up @@ -170,11 +170,19 @@ abstract protected function getInputType(): string;

protected function prepareContainerAttributes(array &$attributes): void
{
$this->addValidationClassToAttributes($attributes, $this->getInputData());
$this->addValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}

protected function prepareInputAttributes(array &$attributes): void
{
$this->addInputValidationClassToAttributes($attributes, $this->getInputData());
$this->addInputValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}
}
5 changes: 5 additions & 0 deletions src/Field/Base/PartsField.php
Expand Up @@ -446,6 +446,11 @@ final protected function generateEndContent(): string
return $this->makeContent($this->templateEnd, $parts);
}

final protected function hasCustomError(): bool
{
return isset($this->errorConfig['message()']);
}

private function generateInputContainerBegin(): string
{
return $this->inputContainerTag === null
Expand Down
21 changes: 15 additions & 6 deletions src/Field/Base/ValidationClass/ValidationClassTrait.php
Expand Up @@ -57,21 +57,29 @@ public function inputValidClass(?string $class): self
return $new;
}

protected function addValidationClassToAttributes(array &$attributes, InputDataInterface $inputData): void
{
protected function addValidationClassToAttributes(
array &$attributes,
InputDataInterface $inputData,
?bool $hasCustomError = null,
): void {
$this->addClassesToAttributes(
$attributes,
$inputData,
$hasCustomError,
$this->invalidClass,
$this->validClass,
);
}

protected function addInputValidationClassToAttributes(array &$attributes, InputDataInterface $inputData): void
{
protected function addInputValidationClassToAttributes(
array &$attributes,
InputDataInterface $inputData,
?bool $hasCustomError = null,
): void {
$this->addClassesToAttributes(
$attributes,
$inputData,
$hasCustomError,
$this->inputInvalidClass,
$this->inputValidClass,
);
Expand All @@ -80,14 +88,15 @@ protected function addInputValidationClassToAttributes(array &$attributes, Input
private function addClassesToAttributes(
array &$attributes,
InputDataInterface $inputData,
?bool $hasCustomError,
?string $invalidClass,
?string $validClass,
): void {
if (!$inputData->isValidated()) {
if (!$inputData->isValidated() && $hasCustomError === null) {
return;
}

$hasErrors = !empty($inputData->getValidationErrors());
$hasErrors = $hasCustomError || !empty($inputData->getValidationErrors());

if ($hasErrors && $invalidClass !== null) {
Html::addCssClass($attributes, $invalidClass);
Expand Down
12 changes: 10 additions & 2 deletions src/Field/Checkbox.php
Expand Up @@ -275,11 +275,19 @@ private function prepareValue(mixed $value): ?string

protected function prepareContainerAttributes(array &$attributes): void
{
$this->addValidationClassToAttributes($attributes, $this->getInputData());
$this->addValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}

protected function prepareInputAttributes(array &$attributes): void
{
$this->addInputValidationClassToAttributes($attributes, $this->getInputData());
$this->addInputValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}
}
14 changes: 13 additions & 1 deletion src/Field/CheckboxList.php
Expand Up @@ -159,9 +159,17 @@ protected function generateInput(): string
}
/** @psalm-var iterable<int, Stringable|scalar> $value */

$checkboxAttributes = [];
$this->addInputValidationClassToAttributes(
$checkboxAttributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);

return $this->widget
->name($name)
->values($value)
->addCheckboxAttributes($checkboxAttributes)
->render();
}

Expand Down Expand Up @@ -189,6 +197,10 @@ protected function renderError(Error $error): string

protected function prepareContainerAttributes(array &$attributes): void
{
$this->addValidationClassToAttributes($attributes, $this->getInputData());
$this->addValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}
}
12 changes: 10 additions & 2 deletions src/Field/Email.php
Expand Up @@ -224,12 +224,20 @@ protected function generateInput(): string

protected function prepareContainerAttributes(array &$attributes): void
{
$this->addValidationClassToAttributes($attributes, $this->getInputData());
$this->addValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}

protected function prepareInputAttributes(array &$attributes): void
{
$this->preparePlaceholderInInputAttributes($attributes);
$this->addInputValidationClassToAttributes($attributes, $this->getInputData());
$this->addInputValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}
}
12 changes: 10 additions & 2 deletions src/Field/File.php
Expand Up @@ -179,11 +179,19 @@ protected function generateInput(): string

protected function prepareContainerAttributes(array &$attributes): void
{
$this->addValidationClassToAttributes($attributes, $this->getInputData());
$this->addValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}

protected function prepareInputAttributes(array &$attributes): void
{
$this->addInputValidationClassToAttributes($attributes, $this->getInputData());
$this->addInputValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}
}
12 changes: 10 additions & 2 deletions src/Field/Number.php
Expand Up @@ -185,12 +185,20 @@ protected function generateInput(): string

protected function prepareContainerAttributes(array &$attributes): void
{
$this->addValidationClassToAttributes($attributes, $this->getInputData());
$this->addValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}

protected function prepareInputAttributes(array &$attributes): void
{
$this->preparePlaceholderInInputAttributes($attributes);
$this->addInputValidationClassToAttributes($attributes, $this->getInputData());
$this->addInputValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}
}
12 changes: 10 additions & 2 deletions src/Field/Password.php
Expand Up @@ -215,12 +215,20 @@ protected function generateInput(): string

protected function prepareContainerAttributes(array &$attributes): void
{
$this->addValidationClassToAttributes($attributes, $this->getInputData());
$this->addValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}

protected function prepareInputAttributes(array &$attributes): void
{
$this->preparePlaceholderInInputAttributes($attributes);
$this->addInputValidationClassToAttributes($attributes, $this->getInputData());
$this->addInputValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}
}
14 changes: 13 additions & 1 deletion src/Field/RadioList.php
Expand Up @@ -161,9 +161,17 @@ protected function generateInput(): string
}
/** @psalm-var Stringable|scalar $value */

$radioAttributes = [];
$this->addInputValidationClassToAttributes(
$radioAttributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);

return $this->widget
->name($name)
->value($value)
->addRadioAttributes($radioAttributes)
->render();
}

Expand Down Expand Up @@ -191,6 +199,10 @@ protected function renderError(Error $error): string

protected function prepareContainerAttributes(array &$attributes): void
{
$this->addValidationClassToAttributes($attributes, $this->getInputData());
$this->addValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}
}
12 changes: 10 additions & 2 deletions src/Field/Range.php
Expand Up @@ -221,11 +221,19 @@ protected function generateInput(): string

protected function prepareContainerAttributes(array &$attributes): void
{
$this->addValidationClassToAttributes($attributes, $this->getInputData());
$this->addValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}

protected function prepareInputAttributes(array &$attributes): void
{
$this->addInputValidationClassToAttributes($attributes, $this->getInputData());
$this->addInputValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}
}
12 changes: 10 additions & 2 deletions src/Field/Select.php
Expand Up @@ -287,11 +287,19 @@ protected function generateInput(): string

protected function prepareContainerAttributes(array &$attributes): void
{
$this->addValidationClassToAttributes($attributes, $this->getInputData());
$this->addValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}

protected function prepareInputAttributes(array &$attributes): void
{
$this->addInputValidationClassToAttributes($attributes, $this->getInputData());
$this->addInputValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}
}
12 changes: 10 additions & 2 deletions src/Field/Telephone.php
Expand Up @@ -210,12 +210,20 @@ protected function generateInput(): string

protected function prepareContainerAttributes(array &$attributes): void
{
$this->addValidationClassToAttributes($attributes, $this->getInputData());
$this->addValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}

protected function prepareInputAttributes(array &$attributes): void
{
$this->preparePlaceholderInInputAttributes($attributes);
$this->addInputValidationClassToAttributes($attributes, $this->getInputData());
$this->addInputValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}
}
12 changes: 10 additions & 2 deletions src/Field/Text.php
Expand Up @@ -226,12 +226,20 @@ protected function generateInput(): string

protected function prepareContainerAttributes(array &$attributes): void
{
$this->addValidationClassToAttributes($attributes, $this->getInputData());
$this->addValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}

protected function prepareInputAttributes(array &$attributes): void
{
$this->preparePlaceholderInInputAttributes($attributes);
$this->addInputValidationClassToAttributes($attributes, $this->getInputData());
$this->addInputValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}
}
12 changes: 10 additions & 2 deletions src/Field/Textarea.php
Expand Up @@ -241,12 +241,20 @@ protected function generateInput(): string

protected function prepareContainerAttributes(array &$attributes): void
{
$this->addValidationClassToAttributes($attributes, $this->getInputData());
$this->addValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}

protected function prepareInputAttributes(array &$attributes): void
{
$this->preparePlaceholderInInputAttributes($attributes);
$this->addInputValidationClassToAttributes($attributes, $this->getInputData());
$this->addInputValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}
}
12 changes: 10 additions & 2 deletions src/Field/Url.php
Expand Up @@ -210,12 +210,20 @@ protected function generateInput(): string

protected function prepareContainerAttributes(array &$attributes): void
{
$this->addValidationClassToAttributes($attributes, $this->getInputData());
$this->addValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}

protected function prepareInputAttributes(array &$attributes): void
{
$this->preparePlaceholderInInputAttributes($attributes);
$this->addInputValidationClassToAttributes($attributes, $this->getInputData());
$this->addInputValidationClassToAttributes(
$attributes,
$this->getInputData(),
$this->hasCustomError() ? true : null,
);
}
}

0 comments on commit 7beb9da

Please sign in to comment.