Skip to content

[Validator] Add an integer constraint and validator #11076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

elnur
Copy link
Contributor

@elnur elnur commented Jun 7, 2014

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets none that I'm aware of
License MIT
Doc PR will be added when this PR gets approved

Who would think it's so hard to write a validator for an integer that works with both int and string types like 1 and '1'. That's why I decided to add this validator to the core.


namespace Symfony\Component\Validator\Tests\Constraints;

use DateTime;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually in Symfony's codebase global classes used without importing them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's ugly, but okay, fixed.

@cordoval
Copy link
Contributor

cordoval commented Jun 7, 2014

if this gets into core, you need to add an entry for new feature to CHANGELOG2.5.txt and add similar entries to the CHANGELOG.txt of the component as well. All of this besides writing the documentation PR.

@elnur
Copy link
Contributor Author

elnur commented Jun 7, 2014

@cordoval, you meant 2.6, right?

@cordoval
Copy link
Contributor

cordoval commented Jun 7, 2014

yes, we are already missing several features that are due to 2.6, please create a new file. You rock @elnur like a .pro 😊

@elnur
Copy link
Contributor Author

elnur commented Jun 7, 2014

@cordoval, I've added an entry to the component's CHANGELOG, but seems like CHANGELOG-2.x.txt files are used for changes in minor versions only.

@cordoval
Copy link
Contributor

cordoval commented Jun 7, 2014

@stof i lost your message, wonder if it was because @elnur rebased. Anyway could you please repeat it? I think is good information for the tool i am building to analyze dependencies 😊 these are good practices!

@webmozart
Copy link
Contributor

Related to #6799 and #10221 (discussion as of here).

@webmozart
Copy link
Contributor

@elnur I think I understand the problem you're solving, but I'm not very fond of the solution. Let's look at the simplest case:

$validator->validate($value, new Integer());
$validator->validate($value, new Type('int'));

I personally would expect both validators to do the same. Maybe this should instead be solved by adding an option "strict" to Type?

// requires an actual integer
$validator->validate($value, new Type('int'));

// requires an integer or anything that can safely be casted to an integer
$validator->validate($value, new Type(array('type' => 'int', 'strict' => false)));

@elnur
Copy link
Contributor Author

elnur commented Sep 29, 2014

@webmozart I'd be happy with whatever other solution you come up with. I just solved a problem I had and decided to share it with others.

@DavidBadura
Copy link
Contributor

+1 for strict parameter. the same also for float, numeric etc. ;-)

@webmozart
Copy link
Contributor

Replaced by #12312.

@webmozart webmozart closed this Oct 24, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants