Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

validates_presence_of :allow_nil => true mistakenly allows blank too #8621

Closed
ColinDKelley opened this Issue Dec 26, 2012 · 0 comments

Comments

Projects
None yet
2 participants
Contributor

ColinDKelley commented Dec 26, 2012

These 2 "can't be blank" tests come up red in Rails 3.2.9:

  def test_allow_nil
    Topic.validates_presence_of(:title, :allow_nil => true)

    t = Topic.new(:title => "something")
    assert t.valid?, t.errors.full_messages

    t.title = ""
    assert t.invalid?                                  # fails
    assert_equal ["can't be blank"], t.errors[:title]  # fails

    t.title = "  "
    assert t.invalid?, t.errors.full_messages          # fails
    assert_equal ["can't be blank"], t.errors[:title   # fails
end

This case is green as it should be:

    t.title = nil
    assert t.valid?, t.errors.full_messages

Here is a branch with full tests of the validates_presence_of options :allow_nil and :allow_blank, and fixes for the failing cases:

https://github.com/ColinDKelley/rails/commits/validates_presence_blank_and_nil

Pull request #8622

@rafaelfranca rafaelfranca added a commit to rafaelfranca/omg-rails that referenced this issue Dec 26, 2012

@rafaelfranca rafaelfranca Revert the change at ActiveModel::Errors#add_on_blank and fix in the
right place.

The EachValidator#validate already handle :allow_blank and :allow_nil,
correctly.

Closes #8622.

Fix #8621.
1e00ab3

@rafaelfranca rafaelfranca added a commit that referenced this issue Dec 26, 2012

@rafaelfranca rafaelfranca Fix `validates_presence_of` with `:allow_nil` or `:allow_blank` options.
Fix #8621

[Colin Kelley + Rafael Mendonça França]
93366c7

@sgerrand sgerrand pushed a commit to sgerrand/rails that referenced this issue Nov 2, 2013

@ColinDKelley ColinDKelley removed TIP: :allow_nil/:allow_blank is ignored by the presence valid…
…ator

These tips were documenting an inconsistency issue rails#8621. That issue is resolved by rails#8622.
52691c3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment