validates_timeliness icon indicating copy to clipboard operation
validates_timeliness copied to clipboard

Format with Regular Expression

Open khanhhd opened this issue 6 years ago • 10 comments

I have an issue with format option: my case is the field accepts 2 types

yyyy/mm/dd hh:nn:ss

or

yyyy-mm-dd hh:nn:ss

My code was:

  validates_datetime :column_name,
   timeliness: {type: :datetime, format: /yyyy[\/-]mm[\/-]dd\shh\:nn\:ss/, allow_blank: true}

The default value in migration is 2000-01-01 00:00:00 But It did not accept the value 2017/11/11 11:11:11

khanhhd avatar Sep 18 '17 02:09 khanhhd

Couple of things. The validation should be written as

validates_datetime :column_name, format: /yyyy[\/-]mm[\/-]dd\shh\:nn\:ss/, allow_blank: true

But specifically, the format option is not meant to be a regexp but a Timeliness format string. The yyyy/mm/dd hh:nn:ss is not a default format but you can add it as a custom format globally.

Try Timeliness.add_formats(:datetime, 'yyyy/mm/dd hh:nn:ss') see how that goes

adzap avatar Sep 18 '17 02:09 adzap

put the Timeliness line in an initializer.

adzap avatar Sep 18 '17 02:09 adzap

Hello @adzap , Thanks for your reply. I had tried with your code. But It seem does not work

#config/initializers/validates_timeliness.rb
ValidatesTimeliness.setup do |config|
  config.extend_orms = [ :active_record ]
  config.use_plugin_parser = true
  Timeliness.add_formats(:datetime, 'yyyy/mm/dd hh:nn:ss')
end
#model.rb
  validates_datetime :column_name, format: /yyyy[\/-]mm[\/-]dd\shh\:nn\:ss/, allow_blank: true

It does not accept anything even the value is 2011/11/11 11:11:11 or 2011-11-11 11:11:11

khanhhd avatar Sep 18 '17 02:09 khanhhd

Ah sorry I should have been more explicit. After adding that format the validation becomes simply this

validates_datetime :column_name, allow_blank: true

adzap avatar Sep 18 '17 02:09 adzap

Also for safety move the Timeliness.add_formats call outside the setup do block.

adzap avatar Sep 18 '17 02:09 adzap

@adzap I just want It accepts 2 formats yyyy-mm-dd hh:nn:ss or yyyy/mm/dd hh:nn:ss If we remove format option in model. It also accepts other types. Such as 11-11-2011 11:11:11 , 11/11/2011 11:11:11

khanhhd avatar Sep 18 '17 02:09 khanhhd

That is not really supported directly in the validation. It is really necessary to be so restrictive on the format? How is the datetime being captured, web form?

adzap avatar Sep 18 '17 03:09 adzap

It is read from CSV file, If it is web form then we can validate in client side :) Thanks for your help (y)

khanhhd avatar Sep 18 '17 03:09 khanhhd

ah ok. you can never rely on client side validation alone, but restricting the format would be easier.

adzap avatar Sep 18 '17 03:09 adzap

i wrote the following codes in the User class at models/user.rb.

validates_date :movedin_date, :allow_blank, :on_or_before => lambda { Date.current }, :format => %r@\d{4}/\d{2}/\d{2}@m

I want the format to be 'yyyy/mm/dd'. But, this is kind of difficult for me, so is it okay to add the following lilne in the initilizer?

Timeliness.add_formats(:date, 'yyyy/mm/dd')
ValidatesTimeliness.setup do |config|
:
end

Humi

humipine avatar Mar 05 '18 09:03 humipine

yes that should be fine to add to the initializer

adzap avatar Nov 22 '22 03:11 adzap