Comments (17)
Let's decide, is it needed for 1.0.0? I think it's indeed a cool thing to have, but can wait for the next release (after 1.0.0). Maybe vote under this post using 👍 / 👎? @yiisoft/yii3
By the way, PHP 7.4 is still supported, so this can be optional.
from validator.
Where would you put these annotations? Maybe you've meant https://github.com/yiisoft/form/blob/master/src/FormModel.php?
from validator.
Yes, it will be primarily used in FormModel
, but we have also DataSetInterface
here that can leverage using annotated validators.
from validator.
So that could be something like named constructor:
$validator = Validator::fromAnnotatedDataSet($data);
$results = $validator->validate($data);
Right?
from validator.
Yes, we will need something like that to receive the DataSetInterface
implementation and parse all attributes using reflection and do the validation.
So we will have validator attributes in this package that will be used both here and in yiisoft/forms
.
from validator.
OK. Let's wait for next beta of PHP 8. Attributes will likely be adjusted there.
from validator.
It's not applicable if rules needs dependencies in current implementation
from validator.
@BoShurik what do you mean?
from validator.
@samdark If I have custom rule:
final class Phone extends Rule
{
public function __construct(\libphonenumber\PhoneNumberUtil $phoneUtil)
{
// ...
}
}
How to inject this dependency?
from validator.
@BoShurik by putting your instance of GroupRule or Rule into container? Since the form is typically obtained by type-hinting it in the route handler action, rule dependency of it would be automatically injected along with sub-dependencies.
from validator.
@samdark do you suggest to use rule class as an attribute or create separate attribute for rule (e.g. Phone
and PhoneAttribute
)? I mean first approach is impossible since rule may have a dependency.
from validator.
I don't get it. Why would I need to put a rule class into an attribute?
class MyRule
{
private WeirdChecker $weirdChecker;
public function __construct(WeirdChecker $weirdChecker)
{
$this->weirdChecker = $weirdChecker;
}
}
class MyForm extends FormModel
{
private MyRule $myRule;
public function __construct(MyRule $myRule)
{
$this->myRule = $myRule;
}
}
public function actionIndex(MyForm $form)
{
// ...
}
from validator.
How you suggest to support php attributes? For each rule create an attribute class which must be converted to its rule?
from validator.
Ah! Finally got it :) You mean something like the following, right?
#[Attribute::TARGET_PROPERTY]
class Validate
{
public function __construct(string $rule, array $parameters = [])
{
// ..
}
public function getData(): array
{
// ...
}
}
class AttributeFormModel
{
private ValidatorFactoryInterface $validatorFactory;
public function __construct(AttributeValidatorFactory $validatorFactory)
{
$this->validatorFactory = $validatorFactory;
}
public function rules(): array
{
$class = new ReflectionClass($this);
$rules = [];
foreach ($class->getProperties() as $property) {
if ($property->isStatic()) {
continue;
}
$attributes = $property->getAttributes(Validate::class);
foreach ($attributes as $validatorAttribute) {
$rules[$property->getName()][] = $validatorAttribute->getData();
}
}
return $rules;
}
}
from validator.
Here's a good reference btw., till the manual is ready: https://stitcher.io/blog/attributes-in-php-8
from validator.
Need to dig deeper into this though. 🤔
from validator.
If depends on how it will affect public API. If it will change it then it worth considering doing it now. If not, doing it later is fine.
from validator.
Related Issues (20)
- Remove or improve `RulesDumper` HOT 5
- Increase MSI to 100%
- Add support non-public properties for dot notation in `Nested` rule
- Add methods `getFirstErrorMessagesIndexedByAttribute()` and `getFirstErrorMessagesIndexedByPath()` to `Result` class HOT 2
- Way to add error with custom value path HOT 2
- Return error code for API application HOT 6
- Can rule Integer allow only 'int'? HOT 1
- Add method to merge several validation results to one HOT 3
- Replace `mixed $skipOnEmpty` in rules constructors to `bool|callable|null $skipOnEmpty` HOT 3
- update irc link
- update docs folder link
- Allow to validate aspect ratio in `Image` rule
- Replace type rule in `RuleHandlerInterface::validate()` from `object` to `RuleInterface`
- Extract rule name and options to separate collector HOT 4
- Extract rule name and options to separate collector HOT 1
- Add `Unique` rule for validating `iterable` HOT 1
- Add `Any` rule
- `AtLeast`, `OneOf` naming - not clear that they are applied to attributes, not other rules HOT 2
- `AnyRule` - make the error message more detailed HOT 1
- `UniqueIterable` rule - add non-strict mode to support values of different types HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from validator.