yiisoft / validator

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

Cannot change defaultSkipOnEmpty #567

Closed tomaszkane closed 1 year ago

tomaszkane commented 1 year ago

What steps will reproduce the problem?

Add DI definition:

Validator::class => [
    'class' => Validator::class,
    '__construct()' => [
        // some other config here...
        'defaultSkipOnEmpty' => false,
    ],
],

What is the expected result?

I want to Container::get(Validator::class) and optionally change defaultSkipOnEmpty to validate different context.

What do you get instead?

defaultSkipOnEmpty cannot be changed, so I must add two definitions to DI, like:

Validator::class => [
    'class' => Validator::class,
    '__construct()' => [
        // some other config here...
        'defaultSkipOnEmpty' => false,
    ],
],
'LazyValidator' => [
    'class' => Validator::class,
    '__construct()' => [
        // some other config here...
        'defaultSkipOnEmpty' => true,
    ],
],

with is ugly.

Additional info

Q A
PHP version 8.0
samdark commented 1 year ago

Do you need two validator instances in the same request?

tomaszkane commented 1 year ago

Hm, no its two requests.

First is api /validate to validate given data (all or part) - "validate what user send".

Second is /create - "validate all data needs to save record" so empty or missing values is not acceptable.

samdark commented 1 year ago

I see. So, an immutable method such as withDefaultSkipOnEmpty() would do.