#1469 unconfirmed
Bernerd Schaefer

Autovalidations do not respect :allow_blank

Reported by Bernerd Schaefer | January 7th, 2011 @ 03:56 AM

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.


Comments and changes to this ticket

  • Piotr Solnica (solnic)

    Piotr Solnica (solnic) May 17th, 2011 @ 02:18 AM

    • Tag changed from dm-core, dm-validations to dm-validations
  • Emily

    Emily March 2nd, 2012 @ 10:06 AM

    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.

  • Jason Seney

    Jason Seney December 10th, 2012 @ 04:07 PM

    I can confirm this issue also - I don't see how it hasn't come up more often. Since Serials are numerical, this basically means I can't have optional associations. Is there anyway to get around this besides turning off auto validations?

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »