Conversation
@miolae any usage syntax examples? |
@samdark Usage axample $validator = new NumberValidator();
$validator->attributes = $this->getAttributesForValidator();
$validator->min(1);
return [
[['login', 'pass'], (new NumberValidator())->min(6)->max(255)],
$validator,
]; I'd prefer a more convenient way to set |
I've read the issue again and I'm confused. There is also a good variant But there could be some difficulties with equal keys (when On the other hand the right part is looking very tasty. I can add some code to implement this. Because now there is no way to set several validators to a single set of attributes (I understood it just now). |
Could be the following as well: public function rules(Validator $is)
{
return [
[['name', 'email', 'subject', 'body'], $is->required()],
['email', $is->email()],
[['login', 'pass'], $is->number()->min(6)->max(255)],
];
} |
I think built-in and custom validator using shouldn't differ too much. You're now showing only built-in variant. And how will look a custom one? |
Right. Then: public function rules()
{
return [
[['name', 'email', 'subject', 'body'], new RequiredValidator()],
['email', new EmailValidator()],
[['login', 'pass'], (new NumberValidator())->min(6)->max(255)],
['branch', new CustomBranchValidator()->primaryOnly()],
];
} |
This variant is already realized for |
I think it's good enough. Let's do other validators and docs. |
6f777ce
to
5a87eab
Compare
5a87eab
to
1768da8
Compare
[skip ci]
[skip ci]
[skip ci]
@samdark (or anybody of yiisoft) make a code review please. There are some significant changes. I've described them in the changelog. If all is ok I'll continue with other validators (now only |
@@ -466,4 +542,48 @@ public function getAttributeNames() | |||
return ltrim($attribute, '!'); | |||
}, $this->attributes); | |||
} | |||
|
|||
private function ruleEsixts(string $name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exists*
@@ -448,8 +448,10 @@ public function createValidators() | |||
foreach ($this->rules() as $rule) { | |||
if ($rule instanceof Validator) { | |||
$validators->append($rule); | |||
} elseif (is_array($rule) && isset($rule[0], $rule[1])) { // attributes, validator type | |||
$validator = Validator::createValidator($rule[1], $this, (array) $rule[0], array_slice($rule, 2)); | |||
} elseif (is_array($rule) && isset($rule[0], $rule[1]) && $rule[1] instanceof Validator) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for removing support for plain arrays with validator config? Looks like unnecessary BC break.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this configuration way is deprecated.
There was a discussion about this in Slack. Somebody said that the deprecation will be removed. So, I'll return arrays for configuration.
* @return mixed | ||
* @throws InvalidConfigException | ||
*/ | ||
public function __call($name, $params) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why rely on magic __call()
and processing some constant with configuration? It is less flexible and less readable than regular getters and setters.
```php | ||
public function rules() { | ||
return [ | ||
[['login', 'pass'], (new NumberValidator())->min(6)->integerOnly(true)], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true
could be default.
public function rules() { | ||
return [ | ||
[['login', 'pass'], (new NumberValidator())->min(6)->integerOnly(true)], | ||
['email', new RequiredValidator], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
()
should be specified.
]; | ||
} | ||
``` | ||
- Validators don't contain validation logic anymore. They are containers for _validation rules_ (see `BaseRule` class). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
- either with a given callback | ||
- or with a given regular expression | ||
- In all other cases you just need to extend `BaseRule` and change or add the needed bit of logic | ||
- Validation rules themselves are described in the `RULES` constant of your validator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That looks like validator and its rule are coupled very tightly... is there a reason for these to be separate classes?
* | ||
* @throws InvalidConfigException | ||
*/ | ||
public function setRule(string $name, $value, ?string $message = null, $validation = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason introducing such flexibility/complexity? While re-configuring rules and overriding standard ones is definitely cool, it may be easily abused to change standard predictable behavior. Are there use cases for it?
Thank you very much for your feedback. Now I have a lot of work in different directions and can't take time for this. A bit later I'll reconsider my decisions. |
Will begin from draft with all requested changes. |
@viktorprogger thank you for ideas. There's now https://github.com/yiisoft/validator |
Here is an example of my vision about how it should work. I've changed
NumberValidator
instead of abstractValidator
to simplfy this example. If all is ok, I'll change all the core validators and the relevant tests.