I have validator, that validates one of fields on form. Field return value with class ActiveSupport::TimeWithZone
validate :begins_at_not_in_past
def begins_at_not_in_past
return unless self.state == 'Scheduled'
if self.begins_at != nil && self.begins_at < (Time.now - 1.hour)
errors.add(:begins_at, 'Action cannot begin in past')
false
end
end
If validation fails it show me my error, however it does create new record in database even though data in form is not valid.
Where did i make mistake?
You should have following line. Otherwise it won’t add error to object. So add following line with errors.add
In controller: #Validation will run only when you call valid? method