Autovalidations do not respect :allow_blank #18

solnic opened this Issue May 17, 2011 · 3 comments

3 participants

DataMapper member

I needed to apply the following patch in my project to see correct behavior with, e.g., optional belongs_to relationships, where the property should be allowed to be blank.

Created by Bernerd Schaefer - 2011-01-07 09:56:10 UTC

Original Lighthouse ticket:

DataMapper member

Maybe fixed here 2ccda08?

Pretty sure this is fixed, whenever that happened...

DataMapper member

Actually, maybe not. I'm shutting up now.


I didn't realize issues had been moved over here from Lighthouse, so here's an update I just posted over there:

I'm having this same problem, and it's really infuriating.

I have the following model:

class Product
  include DataMapper::Resource
  property :id, Serial
  property :list_price, Decimal

  # more properties, not relevant to problem

I want to update that model from a form and not set a list price, so I have params that look something like this:

{ 'product' => { 'list_price' => '' } }

The auto-validations generated for the list_price property have allow_nil: true but not allow_blank: true, meaning that when I update the product with the params above, it's invalid.

Unlike most of the other auto-validations, it seems there's no way to customize numericality, meaning I have to completely disable auto-validations and manually recreate them. I would expect to be able to do something like one of the following:

property :list_price, Decimal, numericality: { allow_blank: true }
property :list_price, Decimal, allow_blank: true

Ideally, though, I shouldn't even have to do that. I think that allow_blank should default to true under the same conditions as allow_nil since otherwise the property can't be updated from a form, which seems like a pretty prime use-case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment