yiisoft / validator

Yii validator library
https://www.yiiframework.com/
BSD 3-Clause "New" or "Revised" License
113 stars 39 forks source link

Update method rules() in next versions. #1

Closed Auramel closed 4 years ago

Auramel commented 6 years ago

Hello! So, I offer to change syntax in \yii\base\Model::rules()

For example, now we have

public function rules()
{
    return [
        [['login', 'pass'], 'required']
    ];
}

I offer try to code something else, for example:

public function rules()
{
    return [
        $this->login => Validator()->required()->string()->min(255)->max(1000)
    ];
}

I like https://github.com/Respect/Validation approach. What do you think? UPD: I don't know why tabs and spaces are not working... UPD2: Someone fixed it. Thank you 👍

Russian: хотелось бы писать правила, как в ссылке выше. :) UPD: Хз почему все переносы и табы слетели... Писал в Notepad++...

UPD2: Кто-то пофиксил. Спасибо 👍

machour commented 5 years ago

@viktorprogger overall, this looks to be heading in the good direction, but without more visible use cases to see, I tend to agree with what @rob006 said.

Yii 3 brings DI closer to the developer, and some will want to use it even if they are not experts and do not fully grasp the consequences of their acts.

In real life, if I need to change unique validation to work in a certain way, I would explicitly use my validator in my code, where I need it.

Allowing to replace a validator through configuration may have a lot of unexpected consequences (i.e., core depend on unique validation to work in a certain way, I change it to another way because of my app requirements => trouble).

rob006 commented 5 years ago

IMHO if somebody uses DI he understands why does he use it.

IMO it is exactly opposite - people does not understand how DI works and what are the consequences of it. Even in your implementation there is fundamental flaw which ruins 2 main goals of DI - implicit dependencies and lack of global state (Factory will crate dependencies from nothing using some global static container).

Also note that if you want, nobody will forbid you to use DI for creating rules - you can still do this in rules() directly. It will work the same, except that it will be explicit and framework will not promote such approach.

kids-return commented 5 years ago

my point of view.

should focus on the yii3 base build. This will allow for faster release. I have been trying to run the demo for a year and now I can't run it.

kids-return commented 5 years ago

Should I use 2.X for new projects? Refactored above after the release of 3.0

machour commented 5 years ago

@kids-return let's keep this conversation about the new rules implementation please. And yes, you should use 2.x for new projects, as we're all doing currently, I'd hate to see 3.x rushed just for the sake of being published.

viktorprogger commented 5 years ago

Also note that if you want, nobody will forbid you to use DI for creating rules - you can still do this in rules() directly. It will work the same, except that it will be explicit and framework will not promote such approach.

This is a good argument. I don't know why didn't I think about it before. I'm going to change this, but I can't give any dates, because I've changed my employer this week, and I have to join the work ASAP.

samdark commented 5 years ago

No worries. What you did is a great starting point anyway.

samdark commented 4 years ago

This one is now implemented.