I have the start and end dates of the event, I need validation, which will not allow the end date to be entered less than the start date. How to do it?
- If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦
|
1 answer
There are no such validators out of the box, but in general everything that you can write a random Ruby check for can also be verified in validation.
This is usually done by putting this validation into a method and calling it through validate . The method is even described in the guides . The result will look something like this:
validate :timespan_valid? def timespan_valid? unless start_date <= end_date errors.add(:start_date, 'is past the end of the event') end end Just make sure that the method does not throw an exception in you in a regular situation. For example, in a regular situation, some of the dates can easily be nil 'om. How to react, decide for yourself.
|