weixin_39782500 2020-11-22 01:58
浏览 0

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

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

Created by Bernerd Schaefer - 2011-01-07 10:00:49 UTC

Original Lighthouse ticket: http://datamapper.lighthouseapp.com/projects/20609/tickets/1470

该提问来源于开源项目:datamapper/dm-core

  • 写回答

6条回答 默认 最新

  • weixin_39782500 2020-11-22 01:58
    关注

    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...

    by Piotr Solnica (solnic)

    评论

报告相同问题?