Skip to content
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

Add allow_nil qualifier to validate_presence_of #1097

Closed
cjmao opened this issue Apr 12, 2018 · 5 comments
Closed

Add allow_nil qualifier to validate_presence_of #1097

cjmao opened this issue Apr 12, 2018 · 5 comments

Comments

@cjmao
Copy link

cjmao commented Apr 12, 2018

Rails supports validates :some_attribute, presence: { allow_nil: true } on models, but currently the validate_presence_of matcher doesn't have the allow_nil qualifier.

It will be much simpler to write specs with this added.
Please let me know if it is not added for some reasons.

@mcmire
Copy link
Collaborator

mcmire commented Apr 13, 2018

Hey @cjmao,

There is an existing issue for this in #683. We have not seen much of a need for this -- after all, usually when you are using validates_presence_of you want to prevent an attribute from being nil or an empty string. So that's why it hasn't been added, but if there is a legitimate use case, then we would gladly take a PR. There was a PR that someone started in #695 which you may use as inspiration if you wish; you may want to read the comments for that particular use case however before starting.

@cjmao
Copy link
Author

cjmao commented Apr 13, 2018

Thanks @mcmire , I've read the comments under the issue and the PR you mentioned. Seems this is a duplicate issue, and my use case is mostly the same with those in the comments.

Currently i'm having a model which has a description attribute. A user can optionally choose to write some description in the description field of a form, or leave the field blank. When the form is posted, and the user choose not to write any description, the controller will receive an empty string as the parameter for description.

Since this field is optional, I think it's best to convert (only) empty string to nil (a user cannot type an empty string, so it's reasonable to consider empty strings to be nil to indicate that the user typed nothing).
With this, the app can distinguish between whether the user has typed nothing, or has typed something which is considered blank. If the user did type something blank (not empty), the app should warn that the user should provide something meaningful (not blank).

So in this case, it indeed needs to validate the presence of this attribute, but only when it is not nil, which comes to validates :description, presence: { allow_nil: true }.

@mcmire
Copy link
Collaborator

mcmire commented Apr 13, 2018

@cjmao Okay, thanks for that. Well, like I said, if you want to start a new PR based on the one that exists that would be great, and we can slip it into the next release.

@cjmao
Copy link
Author

cjmao commented Apr 13, 2018

@mcmire thanks for the advice!
I don't have much experience with Ruby though, I'll see what i can do. 😂

@cjmao cjmao changed the title Add an allow_nil qualifier to validate_presence_of Add allow_nil qualifier to validate_presence_of Apr 17, 2018
@mcmire
Copy link
Collaborator

mcmire commented Sep 13, 2018

Just looking over old issues. I'm going to close this since we have a PR for this.

@mcmire mcmire closed this as completed Sep 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants