Comments (8)
zend-filter is not a validation library. It's purpose is to transform data and/or normalize data.
The proper place to do checks like this is within a validator.
If you are using this particular filter with a data set used within zend-inputfilter, what I'd do instead is use the validator Zend\Validator\Digits
to ensure every value within the string is a numeric digit.
Originally posted by @weierophinney at zendframework/zend-filter#63 (comment)
from laminas-filter.
Thank you @weierophinney ,
We do want data normalization. If we have a string which contains "00", we do want to get a null int. What we do not want, however, is that even strings which are not a valid way to represent 0, such as "four", end up quietly converted to 0. If filter() returns 0 for such cases, we expect the call to trigger a warning.
Note that while I requested a warning to be triggered, I would find it a proper solution too to just refuse to guess a result and - for example - throw an exception. To phrase this differently, "don't quietly take a guess".
Originally posted by @Chealer at zendframework/zend-filter#63 (comment)
from laminas-filter.
@Chealer
The filter uses PHP's normal behaviour for conversion from string to numbers:
If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero).
http://php.net/manual/en/language.types.string.php#language.types.string.conversion
If your use case doesn't allow this conversion, than add a validator in front.
Originally posted by @froschdesign at zendframework/zend-filter#63 (comment)
from laminas-filter.
@Chealer Another route is to create a custom filter, if the current one does not implement the behavior you require.
Typically, we do not want our filters to raise exceptions. If they cannot filter the value, they should return the original value verbatim. So, in this case, I'd write a filter that checks to ensure the value is not malformed, and, if not, filters it, but otherwise returns it verbatim. Then use a validator to see if the value is valid for the domain.
Alternately, if you really want the exception and/or error, trigger it.
But we do not plan to implement such a feature in zend-filter, for the reasons stated above.
Originally posted by @weierophinney at zendframework/zend-filter#63 (comment)
from laminas-filter.
@froschdesign , I read ToInt's code and understand why it happens. If your point is that PHP itself already has a problematic behavior, I do not disagree.
It's not really that a specific use case doesn't allow such conversion. We use Zend filters to normalize inputs throughout the application. The designers must not have realized this problem, but this "filtering" is often quietly altering input and complicating debugging.
Originally posted by @Chealer at zendframework/zend-filter#63 (comment)
from laminas-filter.
Thank you @weierophinney ,
I understand that we can work around this, but this requires some effort, and our philosophy leads us to contribute to our upstreams.
If there is no real solution in sight, I recommend mitigating through documentation.
Originally posted by @Chealer at zendframework/zend-filter#63 (comment)
from laminas-filter.
@Chealer We'd gladly accept documentation to cover this scenario. Docs are in the repository itself.
Originally posted by @weierophinney at zendframework/zend-filter#63 (comment)
from laminas-filter.
If your point is that PHP itself already has a problematic behavior…
No, but we should document this behavior for the filter.
Originally posted by @froschdesign at zendframework/zend-filter#63 (comment)
from laminas-filter.
Related Issues (20)
- References to Zend Version 2.3.8p2 HOT 12
- Filter for arithmetic operations HOT 2
- Test failure with OpenSSL 3.0 HOT 1
- RenameUpload filter breaks UploadFile validator HOT 1
- PHP 8.1 support HOT 3
- Non-string scalar types produce a fatal error with StripNewlines HOT 6
- Hydration of object attached to form with unfiltered values on submission HOT 1
- Is `DateTimeFormatter` filter missed in documentation? HOT 1
- Removal of `container-interop/container-interop`
- Problem with FileInput HOT 1
- Dependency Dashboard
- Declaration of Laminas\Filter\FilterPluginManager::get($name, ?array $options = NULL) must be compatible with Laminas\ServiceManager\AbstractPluginManager::get($name, $options = Array, $usePeeringServiceManagers = true) HOT 10
- Action Required: Fix Renovate Configuration HOT 3
- [RFC]: Deprecate Compression Adapters Lzf, Rar and Snappy HOT 1
- Remove Deprecated Compression Adapters HOT 1
- Remove Deprecated Encryption & Decryption Filters HOT 1
- Mark filters as attributes HOT 7
- `Boolean` filter should also accept `int-mask<self::CONSTANTS>` via `__construct`
- Check for an instance of the `DateTimeInterface` instead of `DateTime` in `DateTimeFormatter` HOT 4
- ToString filter missing? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laminas-filter.