-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #633: Add PHP attribute that sets property label for usage in err…
…or messages
- Loading branch information
Showing
15 changed files
with
267 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Validator; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY)] | ||
final class Label | ||
{ | ||
public function __construct( | ||
private string $label, | ||
) { | ||
} | ||
|
||
public function getLabel(): string | ||
{ | ||
return $this->label; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Validator; | ||
|
||
/** | ||
* Provides data attribute labels. | ||
*/ | ||
interface LabelsProviderInterface | ||
{ | ||
/** | ||
* @return array<string, string> A set of attribute labels. | ||
*/ | ||
public function getValidationPropertyLabels(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Validator\Tests\Support\Data; | ||
|
||
use Yiisoft\Validator\Label; | ||
use Yiisoft\Validator\LabelsProviderInterface; | ||
use Yiisoft\Validator\Rule\Number; | ||
use Yiisoft\Validator\Rule\Required; | ||
|
||
final class ObjectWithLabelsProvider implements LabelsProviderInterface | ||
{ | ||
#[Required(message: '{attribute} cannot be blank.')] | ||
public string $name = ''; | ||
|
||
#[Number(min: 21, lessThanMinMessage: '{attribute} must be no less than {min}.')] | ||
#[Label('test age')] | ||
protected int $age = 17; | ||
|
||
#[Number(max: 100)] | ||
#[Label('test')] | ||
private int $number = 42; | ||
|
||
public function getValidationPropertyLabels(): array | ||
{ | ||
return [ | ||
'name' => 'Имя', | ||
'age' => 'Возраст', | ||
]; | ||
} | ||
} |
Oops, something went wrong.