yiisoft / request-model

https://www.yiiframework.com/
BSD 3-Clause "New" or "Revised" License
15 stars 9 forks source link

Not required fields #15

Open vjik opened 3 years ago

vjik commented 3 years ago

It is difficult to work with optional fields. Has two algorithm. For example, field sort optional and maybe "asc" or "desc".

Strict algorithm

If data has field (even empty), then we validate him.

['sort' => 'asc'] // asc
['sort' => 'desc'] // desc
['sort' => 'up'] // error
['sort' => ''] // error
[], // null

Non-strict algorithm

If data not have field or field is empty, then we believe that the value was not passed.

['sort' => 'asc'] // asc
['sort' => 'desc'] // desc
['sort' => 'up'] // error
['sort' => ''] // null
[], // null

I'm created #14 with test for both algorithms. Tests pass, but implementation of models very difficult. Need find more beautiful solution.

vjik commented 3 years ago

@see https://github.com/yiisoft/validator/pull/102