0

I am using date_select for two of my fields in the db -

<div class="control-group">
   <label class="control-label">
   <%= f.label :date_one %></label>
<div class="controls">
   <%= f.date_select :date_one%>
</div>
</div>

<div class="control-group">
   <label class="control-label">
   <%= f.label :date_two %></label>
<div class="controls">
   <%= f.date_select :date_two%>
</div>
</div>

Now what I am trying to do here is, no matter what date is selected for date_one, date_two shouldnt be before what has been selected for date_one. Is that possible? How do I do it? Is it possible to setup a validation of some kind?

Thanks

psharma
  • 1,279
  • 2
  • 19
  • 47

1 Answers1

2

Yes, but you should start on server to ensure valid data is going to be saved. Then you could add client side validations, see for more information this question.

Well, to add server side validation, do this in your model:

validate :date_two_after_date_one

def date_two_after_date_one
  errors.add :date_two, 'should be after date one' if date_two < date_one
end

And client side, you will have to use JavaScript for that, paste your form_for call.

Community
  • 1
  • 1
sites
  • 21,417
  • 17
  • 87
  • 146