Rails validation regex not working: invalid though input correct -
i trying validate kind of datetime field regex. regex correct - tested on rubular - rails throws error on field: invalid.
in model have:
validates :ddate, presence: true, format: { with: /\a(\d\d\.){2}\d{4} \d\d:\d\d\z/ }
in form enter: 25.12.2015 20:15
, validation error: ddate invalid
i tried break down , started with:
validates :ddate, presence: true, format: { with: /\a\d\d/ }
which worked expected, tried:
validates :ddate, presence: true, format: { with: /\a\d\d\./ }
which did not work anymore.
any hints, why not working?
i using rails 4.2.4 , ruby 2.2.1p85.
you passing string, make instead:
attr_accessor :ddate_string validates :ddate_string, presence: true, format: { with: /\d{1,2}\.\d{1,2}\.\d{4} \d{1,2}:\d{1,2}/ } before_save :set_ddate def set_ddate self.ddate = ddate_string end
you can run regex validations against strings.
Comments
Post a Comment