Skip to content

Commit

Permalink
Separate chain calls (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
sankaest committed Jun 16, 2022
1 parent dafd8ce commit 9974ae2
Show file tree
Hide file tree
Showing 21 changed files with 156 additions and 52 deletions.
7 changes: 5 additions & 2 deletions docs/creating-forms.md
Expand Up @@ -111,8 +111,11 @@ $this->setTitle($title);
->csrf($csrf)
->id('form-auth-login')
->open() ?>
<?= Field::text($formModel, 'login')->autofocus()->tabindex(1) ?>
<?= Field::password($formModel, 'password')->tabindex(2) ?>
<?= Field::text($formModel, 'login')
->autofocus()
->tabindex(1) ?>
<?= Field::password($formModel, 'password')
->tabindex(2) ?>
<?= Field::submitButton()
->containerClass('d-grid gap-2 form-floating')
->buttonClass('btn btn-primary btn-lg mt-3')
Expand Down
4 changes: 3 additions & 1 deletion src/Field/Base/DateTimeInputField.php
Expand Up @@ -147,7 +147,9 @@ protected function beforeRender(): void
{
parent::beforeRender();
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
$rules = $this
->getFormModel()
->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
Expand Down
9 changes: 7 additions & 2 deletions src/Field/Base/FormAttributeTrait.php
Expand Up @@ -57,7 +57,9 @@ final protected function getFormAttributeHint(): string

final protected function getFormAttributePlaceholder(): ?string
{
$placeholder = $this->getFormModel()->getAttributePlaceholder($this->getFormAttributeName());
$placeholder = $this
->getFormModel()
->getAttributePlaceholder($this->getFormAttributeName());
return $placeholder === '' ? null : $placeholder;
}

Expand All @@ -68,6 +70,9 @@ final protected function getInputId(): string

final protected function getFirstError(): ?string
{
return $this->getFormModel()->getFormErrors()->getFirstError($this->getFormAttributeName());
return $this
->getFormModel()
->getFormErrors()
->getFirstError($this->getFormAttributeName());
}
}
4 changes: 3 additions & 1 deletion src/Field/Base/ValidationClass/ValidationClassTrait.php
Expand Up @@ -96,7 +96,9 @@ private function addClassesToAttributes(
return;
}

$hasErrors = $formModel->getFormErrors()->hasErrors($attributeName);
$hasErrors = $formModel
->getFormErrors()
->hasErrors($attributeName);

if ($hasErrors && $invalidClass !== null) {
Html::addCssClass($attributes, $invalidClass);
Expand Down
4 changes: 3 additions & 1 deletion src/Field/Email.php
Expand Up @@ -208,7 +208,9 @@ protected function beforeRender(): void
{
parent::beforeRender();
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
$rules = $this
->getFormModel()
->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
Expand Down
4 changes: 3 additions & 1 deletion src/Field/ErrorSummary.php
Expand Up @@ -138,7 +138,9 @@ protected function generateContent(): ?string
$content[] = Html::p($this->header, $this->headerAttributes)->render();
}

$content[] = Html::ul()->strings($messages, [], $this->encode)->render();
$content[] = Html::ul()
->strings($messages, [], $this->encode)
->render();

if ($this->footer !== '') {
$content[] = Html::p($this->footer, $this->footerAttributes)->render();
Expand Down
4 changes: 3 additions & 1 deletion src/Field/File.php
Expand Up @@ -160,7 +160,9 @@ protected function beforeRender(): void
{
parent::beforeRender();
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
$rules = $this
->getFormModel()
->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
Expand Down
4 changes: 3 additions & 1 deletion src/Field/Number.php
Expand Up @@ -168,7 +168,9 @@ protected function beforeRender(): void
{
parent::beforeRender();
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
$rules = $this
->getFormModel()
->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
Expand Down
4 changes: 3 additions & 1 deletion src/Field/Password.php
Expand Up @@ -199,7 +199,9 @@ protected function beforeRender(): void
{
parent::beforeRender();
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
$rules = $this
->getFormModel()
->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
Expand Down
4 changes: 3 additions & 1 deletion src/Field/Range.php
Expand Up @@ -196,7 +196,9 @@ protected function beforeRender(): void
{
parent::beforeRender();
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
$rules = $this
->getFormModel()
->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
Expand Down
4 changes: 3 additions & 1 deletion src/Field/Select.php
Expand Up @@ -248,7 +248,9 @@ protected function beforeRender(): void
{
parent::beforeRender();
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
$rules = $this
->getFormModel()
->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
Expand Down
4 changes: 3 additions & 1 deletion src/Field/Telephone.php
Expand Up @@ -194,7 +194,9 @@ protected function beforeRender(): void
{
parent::beforeRender();
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
$rules = $this
->getFormModel()
->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
Expand Down
4 changes: 3 additions & 1 deletion src/Field/Text.php
Expand Up @@ -210,7 +210,9 @@ protected function beforeRender(): void
{
parent::beforeRender();
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
$rules = $this
->getFormModel()
->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
Expand Down
4 changes: 3 additions & 1 deletion src/Field/Textarea.php
Expand Up @@ -224,7 +224,9 @@ protected function beforeRender(): void
{
parent::beforeRender();
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
$rules = $this
->getFormModel()
->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
Expand Down
4 changes: 3 additions & 1 deletion src/Field/Url.php
Expand Up @@ -195,7 +195,9 @@ protected function beforeRender(): void
{
parent::beforeRender();
if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
$rules = $this->getFormModel()->getRules()[$this->getFormAttributeName()] ?? [];
$rules = $this
->getFormModel()
->getRules()[$this->getFormAttributeName()] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof BeforeValidationInterface && $rule->getWhen() !== null) {
continue;
Expand Down
8 changes: 6 additions & 2 deletions src/FormModel.php
Expand Up @@ -246,7 +246,9 @@ private function addErrors(array $items): void
{
foreach ($items as $attribute => $errors) {
foreach ($errors as $error) {
$this->getFormErrors()->addError($attribute, $error);
$this
->getFormErrors()
->addError($attribute, $error);
}
}
}
Expand Down Expand Up @@ -274,7 +276,9 @@ private function getInflector(): Inflector
private function generateAttributeLabel(string $name): string
{
return StringHelper::uppercaseFirstCharacterInEachWord(
$this->getInflector()->toWords($name)
$this
->getInflector()
->toWords($name)
);
}

Expand Down
28 changes: 21 additions & 7 deletions src/Helper/HtmlFormErrors.php
Expand Up @@ -20,7 +20,9 @@ final class HtmlFormErrors
*/
public static function getAllErrors(FormModelInterface $formModel): array
{
return $formModel->getFormErrors()->getAllErrors();
return $formModel
->getFormErrors()
->getAllErrors();
}

/**
Expand All @@ -31,7 +33,9 @@ public static function getAllErrors(FormModelInterface $formModel): array
*/
public static function getErrors(FormModelInterface $formModel, string $attribute): array
{
return $formModel->getFormErrors()->getErrors($attribute);
return $formModel
->getFormErrors()
->getErrors($attribute);
}

/**
Expand All @@ -43,7 +47,9 @@ public static function getErrors(FormModelInterface $formModel, string $attribut
*/
public static function getErrorSummaryFirstErrors(FormModelInterface $formModel): array
{
return $formModel->getFormErrors()->getErrorSummaryFirstErrors();
return $formModel
->getFormErrors()
->getErrorSummaryFirstErrors();
}

/**
Expand All @@ -56,7 +62,9 @@ public static function getErrorSummaryFirstErrors(FormModelInterface $formModel)
*/
public static function getErrorSummary(FormModelInterface $formModel, array $onlyAttributes = []): array
{
return $formModel->getFormErrors()->getErrorSummary($onlyAttributes);
return $formModel
->getFormErrors()
->getErrorSummary($onlyAttributes);
}

/**
Expand All @@ -69,7 +77,9 @@ public static function getErrorSummary(FormModelInterface $formModel, array $onl
*/
public static function getFirstError(FormModelInterface $formModel, string $attribute): ?string
{
return $formModel->getFormErrors()->getFirstError(HtmlForm::getAttributeName($formModel, $attribute));
return $formModel
->getFormErrors()
->getFirstError(HtmlForm::getAttributeName($formModel, $attribute));
}

/**
Expand All @@ -82,7 +92,9 @@ public static function getFirstError(FormModelInterface $formModel, string $attr
*/
public static function getFirstErrors(FormModelInterface $formModel): array
{
return $formModel->getFormErrors()->getFirstErrors();
return $formModel
->getFormErrors()
->getFirstErrors();
}

/**
Expand All @@ -95,6 +107,8 @@ public static function getFirstErrors(FormModelInterface $formModel): array
*/
public static function hasErrors(FormModelInterface $formModel, ?string $attribute = null): bool
{
return $formModel->getFormErrors()->hasErrors($attribute);
return $formModel
->getFormErrors()
->hasErrors($attribute);
}
}
4 changes: 3 additions & 1 deletion tests/Field/Base/ButtonFieldTest.php
Expand Up @@ -37,7 +37,9 @@ public function testButton(): void
{
$result = StubButtonField::widget()
->button(
ButtonTag::tag()->content('Start')->class('primary')
ButtonTag::tag()
->content('Start')
->class('primary')
)
->render();

Expand Down
28 changes: 21 additions & 7 deletions tests/Field/SelectTest.php
Expand Up @@ -111,8 +111,12 @@ public function dataItems(): array
</select>
HTML,
[
Option::tag()->value('1')->content('One'),
Option::tag()->value('2')->content('Two'),
Option::tag()
->value('1')
->content('One'),
Option::tag()
->value('2')
->content('Two'),
],
],
[
Expand All @@ -126,10 +130,16 @@ public function dataItems(): array
</select>
HTML,
[
Option::tag()->value('1')->content('One'),
Option::tag()
->value('1')
->content('One'),
Optgroup::tag()->options(
Option::tag()->value('1.1')->content('One.One'),
Option::tag()->value('1.2')->content('One.Two'),
Option::tag()
->value('1.1')
->content('One.One'),
Option::tag()
->value('1.2')
->content('One.Two'),
),
],
],
Expand All @@ -156,8 +166,12 @@ public function testOptions(): void
$result = Select::widget()
->formAttribute(new SelectForm(), 'item')
->options(
Option::tag()->value('1')->content('One'),
Option::tag()->value('2')->content('Two'),
Option::tag()
->value('1')
->content('One'),
Option::tag()
->value('2')
->content('Two'),
)
->hideLabel()
->useContainer(false)
Expand Down

0 comments on commit 9974ae2

Please sign in to comment.