Skip to content

Commit

Permalink
Add Label attribute support (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Aug 24, 2024
1 parent 222581c commit b204dd3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ use Yiisoft\Validator\Rule\Required;

final class LoginForm extends FormModel
{
#[Label('Your login')]
#[Required]
#[Length(min: 4, max: 40, skipOnEmpty: true)]
#[Email(skipOnEmpty: true)]
private ?string $login = null;

#[Label('Your password')]
#[Required]
#[Length(min: 8, skipOnEmpty: true)]
private ?string $password = null;

#[Label('Remember me for 1 week')]
#[Safe]
private bool $rememberMe = false;
}
Expand Down
14 changes: 14 additions & 0 deletions src/FormModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\FormModel;

use LogicException;
use ReflectionAttribute;
use ReflectionClass;
use ReflectionException;
use Yiisoft\FormModel\Exception\PropertyNotSupportNestedValuesException;
Expand All @@ -15,6 +16,7 @@
use Yiisoft\Hydrator\Attribute\SkipHydration;
use Yiisoft\Strings\Inflector;
use Yiisoft\Strings\StringHelper;
use Yiisoft\Validator\Label;
use Yiisoft\Validator\Result;

use function array_key_exists;
Expand Down Expand Up @@ -237,6 +239,18 @@ private function readPropertyMetaValue(int $metaKey, string $path): ?string
return null;
}

/**
* Try to get label from {@see Label} PHP attribute.
*/
if ($metaKey === self::META_LABEL) {
$attributes = $property->getAttributes(Label::class, ReflectionAttribute::IS_INSTANCEOF);
foreach ($attributes as $attribute) {
/** @var Label $instance */
$instance = $attribute->newInstance();
return $instance->getLabel();
}
}

$value = $property->getValue($value);
if (!is_object($value)) {
return null;
Expand Down
17 changes: 17 additions & 0 deletions tests/FormModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Yiisoft\FormModel\Tests\Support\Form\DefaultFormNameForm;
use Yiisoft\FormModel\Tests\Support\Form\FormWithNestedProperty;
use Yiisoft\FormModel\Tests\Support\Form\FormWithNestedStructures;
use Yiisoft\FormModel\Tests\Support\Form\LabelForm;
use Yiisoft\FormModel\Tests\Support\Form\LoginForm;
use Yiisoft\FormModel\Tests\Support\Form\NestedForm;
use Yiisoft\FormModel\Tests\Support\Form\NestedMixedForm\NestedMixedForm;
Expand Down Expand Up @@ -682,4 +683,20 @@ public function testAddErrorWithoutValidation(): void
$this->expectExceptionMessage('Validation result is not set.');
$form->addError('Test message.');
}

public static function dataLabel(): iterable
{
yield ['AgeFromAttribute', 'age'];
yield ['NameFromGetter', 'name'];
}

#[DataProvider('dataLabel')]
public function testLabel(string $expected, string $property): void
{
$form = new LabelForm();

$label = $form->getPropertyLabel($property);

$this->assertSame($expected, $label);
}
}
24 changes: 24 additions & 0 deletions tests/Support/Form/LabelForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Yiisoft\FormModel\Tests\Support\Form;

use Yiisoft\FormModel\FormModel;
use Yiisoft\Validator\Label;

final class LabelForm extends FormModel
{
#[Label('AgeFromAttribute')]
public string $age;

#[Label('NameFromAttribute')]
public string $name;

public function getPropertyLabels(): array
{
return [
'name' => 'NameFromGetter',
];
}
}

0 comments on commit b204dd3

Please sign in to comment.