-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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
validates length lets nil to be valid although nil is not allowed #7180
Comments
sorry, |
Well, empty string |
you're right, I think that |
Given a class that include ActiveModel::Validations with a validation like the following: validates_length_of(:title, :minimum => 0, :allow_nil => false). This makes incosistent the validation because it not raise error when title is *nil*.It should accepts only the empty string With this patch title is *nil* is handled and the validation accepts only the empty string *""* It should fix rails#7180
It is no secret that length is going to convert the value to a string if it isn't one. Writing validates_length_of(:title, :minimum => 0, :allow_nil => false) seems to be just a confusing way to write validates_presence_of(:title). |
If you think that the behavior is correct, then the documentation should be appropriately updated, because it simply doesn't make any sense. What you say to be a confusing way to write something that doesn't fit to this situation is (without the redundant How would you write a validation with More than reading that my approach is bad I'd like to read why current approach is correct and why it was changed between 3.0 and 3.1? Isn't it just masking of the fact that refactoring of the validation method introduced a bug which you think is not necessary to fix so you say it's a feature? By the way, if
I'd like to reopen this issue because I'm confident that current state is not [correct](http://en.wikipedia.org/wiki/Correctness_(computer_science\)) and something should be done to fix this state (suggestions have already been given). |
@xHire there is a simple way to get your validation with something like that: I think it's helpful for you. |
Well, maybe for me, but surely not for authlogic and others. When someone competent solves this issue in a way that makes length validation with Simply because "hey, do it rather this way" doesn't fix broken correctness. |
I Agree, it's is inconsistent behavior and at least could be raise an exception if |
@xHire if you want to fix why you don't give a try? Also, I don't think that attacking the contributors will make someone fix your issue. Try to be more respectful next time. I'm reopening but I'll not fix it now. |
If the issue doesn't exist in 3.0.3 and exist 3.1 should be easy to see why it changed using github. |
@rafaelfranca it should be decided reopen this pull request (after some refactoring) or |
@rafaelfranca Thank you for reopening this. I didn't mean to attack anyone and I'm sorry if something seemed like that. Regarding my statement about making a feature from this bug -- that was what josevalim actually did by writing "It is no secret that length is going to convert the value to a string if it isn't one." and closing this issue without any discussion. So it seemed to me like what I wrote (I admin it could have been written in a bit different way). I found that this commit introduces the bug: 157c37f Right now I have a patch which handles nil correctly based on |
@xHire are you going to turn this into a PR anytime soon? |
Length validation handles correctly nil. Fix #7180 Conflicts: activemodel/CHANGELOG.md
When nil or empty string are not allowed, they are not valid. Conflicts: activemodel/CHANGELOG.md activemodel/lib/active_model/validations/length.rb activemodel/test/cases/validations/length_validation_test.rb
When nil or empty string are not allowed, they are not valid. Conflicts: activemodel/CHANGELOG.md activemodel/lib/active_model/validations/length.rb activemodel/test/cases/validations/length_validation_test.rb Conflicts: activemodel/lib/active_model/validations/length.rb
When nil or empty string are not allowed, they are not valid. Conflicts: activemodel/CHANGELOG.md activemodel/test/cases/validations/length_validation_test.rb
When I setup length validation like this one:
then even with nil this is valid. I created a simple demonstrative application: https://github.com/xHire/ror-zerolengthvalidation
At least in 3.0.3 this issue didn't exist, but in latest 3.1 and 3.2 it does.
The text was updated successfully, but these errors were encountered: