#1470 accepted
Bernerd Schaefer

Setting Integer belongs_to property to empty string should not fail property validation

Reported by Bernerd Schaefer | January 7th, 2011 @ 04:00 AM | in 1.1.1

Scenario:

  • Model defines an optional belongs_to relationship
  • User submits form with <option value="" name="user_id"></option>

Expected behavior:

  • user_id is cleared

Actual behavior:

  • user_id is set to "", which results in a "User must be an integer" error

The following patch solved this problem for me (typecasting blank to nil for Integer fields): https://gist.github.com/769304

Comments and changes to this ticket

  • Piotr Solnica (solnic)

    Piotr Solnica (solnic) January 7th, 2011 @ 05:06 AM

    • State changed from “unconfirmed” to “suggestion”
    • Assigned user set to “Piotr Solnica (solnic)”
    • Tag changed from dm-core bug to dm-core, suggestion, validations

    It's a known issue and we decided it's not DataMapper's responsibility to typecast empty strings to nils. In a web app's world it is obviously a convenient behavior, but we cannot just assume it would be good in other cases. There's an idea to create a rack middleware which would sanitize params. We could also think about a DM plugin which would do the same thing as in your gist.

    I'm marking this as "suggestion" so we can have another discussion about it. I did have a suggestion that we could add a new option to Property called something like "blank_as_nil" which, when set to true, would make properties to automatically typecast blank values to nils. After some discussions on IRC we dropped that idea. I know it's a pain and it hits many people so maybe we should reconsider...

  • Piotr Solnica (solnic)

    Piotr Solnica (solnic) January 7th, 2011 @ 05:06 AM

    Adding Dan and Martin to that ticket...

  • Bernerd Schaefer

    Bernerd Schaefer January 7th, 2011 @ 05:34 AM

    Perhaps it could find home, at least, in dm-rails. Note that I think this would be required for my ticket about allow_blank and autovalidations (#1469).

  • Piotr Solnica (solnic)

    Piotr Solnica (solnic) January 7th, 2011 @ 05:39 AM

    Yeah allow_blank doesn't make sense without auto-typecasting blank to nil, right?

    I would add it to dm-rails right away. Martin, what do you think?

  • Piotr Solnica (solnic)

    Piotr Solnica (solnic) January 8th, 2011 @ 01:08 PM

    • Milestone set to 1.1
    • State changed from “suggestion” to “accepted”
    • Milestone order changed from “196325” to “0”

    alright, we've decided with Martin to fix it now in dm-rails.

  • Piotr Solnica (solnic)

    Piotr Solnica (solnic) February 9th, 2011 @ 06:43 AM

    • Milestone changed from 1.1 to 1.1.1
    • Milestone order changed from “7” to “0”
  • Dan Kubb (dkubb)

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 »