- Published
- Author
- Puneeth kumarSystem Analyst
Validating date and time field in Rails
1. Check if a date is valid (e.g., "2025-02-30" is not a valid date).
2. Make sure a date is before or after a certain time as needed.
3. Restrict a field to only accept dates, times or datetimes.
4. Works well with user input in different formats.
For example :
This makes sure start_time is not in the past.
#CU6U0R822 #date_time_validation
validates_timeliness gem helps us to check if a date or time field is valid and meets certain conditions — like being in the past, in the future, or within a specific range. It can do following things.1. Check if a date is valid (e.g., "2025-02-30" is not a valid date).
2. Make sure a date is before or after a certain time as needed.
3. Restrict a field to only accept dates, times or datetimes.
4. Works well with user input in different formats.
For example :
Code
class Event < ApplicationRecord
validates_timeliness :start_time, on_or_after: :now, type: :datetime
endThis makes sure start_time is not in the past.
#CU6U0R822 #date_time_validation