Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 4 and date format validation #11506

Closed
glaucocustodio opened this issue Jul 19, 2013 · 2 comments
Closed

Rails 4 and date format validation #11506

glaucocustodio opened this issue Jul 19, 2013 · 2 comments

Comments

@glaucocustodio
Copy link
Contributor

Hi guys, I am on Rails 4 (and ruby 2.0.0p195) and I had a curious experience.

I tried many ways to check date validity in my model.

For me, the better way to do it is using regex. My form (client side) just accepts date in this format 'dd/mm/yyyy', and in my table migration I have a date type field (my db is postgres).

My date always was returning as invalid, so I checked that Rails was converting it to psql date format ('yyyy-mm-dd') before to perform regex match.

Weird that the same code works in Rails 3.

See the gist: https://gist.github.com/glaucocustodio/6039161

Note: as well as in Rails 3 project, in my Rails 4 app I have a locales/pt-BR.yml and language set to it in application.rb.

If it is my own mistake, please, how I must to proceed?

Thank you.

@ehtb
Copy link
Contributor

ehtb commented Jul 25, 2013

Glauco, which queries are you testing against?

Testing it on 3.2.14 and 4 yields the same result: a failure at the second assertion

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  setup do
    @user = User.new
  end

  test 'valid date' do
    User.validates(:date_birth, format: { with: /[0-3]{1}[0-9]{1}\/[0-1]{1}[0-9]{1}\/[1-2]{1}[0-9]{3}/, message: "is bad data" })

    puts Rails.version

    @user.date_birth = '20/12/2012'
    assert @user.valid?, '20/12/2012'

    @user.date_birth = '2012/12/20'
    assert @user.valid?, '2012/12/20'
  end
end

ps. adding this as validations on the model makes no difference.

@rafaelfranca
Copy link
Member

This doesn't seems an issue to me. It is more like a question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants