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

validation_max bug in inputs/base/validations in v2.0.0.rc3 #606

Closed
mhenrixon opened this issue Jun 24, 2011 · 6 comments
Closed

validation_max bug in inputs/base/validations in v2.0.0.rc3 #606

mhenrixon opened this issue Jun 24, 2011 · 6 comments
Labels
Milestone

Comments

@mhenrixon
Copy link
Contributor

I have this numericality validation in my model and now it seems like formtastic chokes on it somehow when trying to follow the Proc.

validates_numericality_of :min_sphere, 
  less_than: Proc.new { |frame| frame.max_sphere }, 
  greater_than_or_equal_to: -60, 
  only_integer: false 

validates_numericality_of :max_sphere,
  less_than_or_equal_to: 60,
  greater_than: Proc.new { |frame| frame.min_sphere },
  only_integer: false 

Error message:

undefined method `-' for #<Proc:0x000001049e6c68>
formtastic (2.0.0.rc3) lib/formtastic/inputs/base/validations.rb:88:in `validation_max'

View code:

=f.input :min_sphere
@justinfrench
Copy link
Member

If you'd like to try to patch it, I would take a look at Formtastic::Inputs::Base::Validations#validation_max (and min), and take a look at this line:

return (validation.options[:less_than] - 1) if validation.options[:less_than]

This line is assuming that :less_than is a number, not a Proc. The code needs to be changed to something like this, which detects and calls the Proc.

if validation.options[:less_than]
  return (...) if validation.options[:less_than].kind_of?(Proc)
  return (validation.options[:less_than] - 1)
end

The (...) obviously needs filling in :)

I was about to suggest another work-around may be to try :less_than_or_equal_to, but I think this would avoid the - 1 error, but still result in a Proc rather than a useful number in the min and max.

If you wish to bypass this logic completely until it's sorted out, a custom version of NumberInput in app/inputs/number_input.rb should help:

 class NumberInput < Formtastic::Inputs::NumberInput
   def step_option
     return options[:step] if options.key?(:step)
     1
   end

   def min_option
     return options[:min] if options.key?(:min)
   end

   def max_option
     return options[:max] if options.key?(:max)
   end
 end

Basically, it will only use min/max/step from the options, not from validations, etc.

@mhenrixon
Copy link
Contributor Author

Spot on suggesting me to patch it. I am a ruby n00b still but thanks to your detailed description of the problem I think I'll manage to solve this one. Give me a day or so to do this and I'll send you a pull request.

If I wanted to write tests for this I suppose that I'd add this for both range_input_specs, number_input_specs?

@justinfrench
Copy link
Member

Yes, they're the right places to add coverage, but it could be a lot of data to set-up, so a quick hack around with bundle open formtastic within your app might ensure we're on the right path to start with.

@mhenrixon
Copy link
Contributor Author

Ok so I fixed the issue but I have to spend some time with the specs before I can add that. A LOT!! of setting up done in the spec_helper :)

@justinfrench
Copy link
Member

@mhenrixon, how's the specs going? Would love to get a new RC out this week!

@mhenrixon
Copy link
Contributor Author

Sorry about that, I completely forgot about it. Will try and create something tonight.

justinfrench added a commit that referenced this issue Jul 4, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants