Git Product home page Git Product logo

Comments (17)

arogachev avatar arogachev commented on August 22, 2024 2

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.

samdark avatar samdark commented on August 22, 2024

Where would you put these annotations? Maybe you've meant https://github.com/yiisoft/form/blob/master/src/FormModel.php?

from validator.

armpogart avatar armpogart commented on August 22, 2024

Yes, it will be primarily used in FormModel, but we have also DataSetInterface here that can leverage using annotated validators.

from validator.

samdark avatar samdark commented on August 22, 2024

So that could be something like named constructor:

$validator = Validator::fromAnnotatedDataSet($data);
$results = $validator->validate($data);

Right?

from validator.

armpogart avatar armpogart commented on August 22, 2024

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.

samdark avatar samdark commented on August 22, 2024

OK. Let's wait for next beta of PHP 8. Attributes will likely be adjusted there.

from validator.

BoShurik avatar BoShurik commented on August 22, 2024

It's not applicable if rules needs dependencies in current implementation

from validator.

samdark avatar samdark commented on August 22, 2024

@BoShurik what do you mean?

from validator.

BoShurik avatar BoShurik commented on August 22, 2024

@samdark If I have custom rule:

final class Phone extends Rule
{
    public function __construct(\libphonenumber\PhoneNumberUtil $phoneUtil)
    {
        // ...
    }
}

How to inject this dependency?

from validator.

samdark avatar samdark commented on August 22, 2024

@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.

BoShurik avatar BoShurik commented on August 22, 2024

@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.

samdark avatar samdark commented on August 22, 2024

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.

BoShurik avatar BoShurik commented on August 22, 2024

How you suggest to support php attributes? For each rule create an attribute class which must be converted to its rule?

from validator.

samdark avatar samdark commented on August 22, 2024

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.

samdark avatar samdark commented on August 22, 2024

Here's a good reference btw., till the manual is ready: https://stitcher.io/blog/attributes-in-php-8

from validator.

arogachev avatar arogachev commented on August 22, 2024

Need to dig deeper into this though. 🤔

from validator.

samdark avatar samdark commented on August 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.