# Date Field Validation

**URL:** <https://www.ruby-forum.com/t/date-field-validation/138394>\
**Category:** Rails\
**Created:** [May 22, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/date-field-validation/138394 "2008-05-22T16:02:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrbless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@mrbless](https://www.ruby-forum.com/u/mrbless)\
**Post date:** [May 22, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/date-field-validation/138394/1 "2008-05-22T16:02:23Z")

</div>

I have to fields in database start\_date and end\_date. What I want to  
validate is that start\_date must be less than end\_date. I want the  
model level validation . Please help me.

---

<div class="post-metadata">

**Author:** ![mrbless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@mrbless](https://www.ruby-forum.com/u/mrbless)\
**Post date:** [May 22, 2008, 4:03pm UTC](https://www.ruby-forum.com/t/date-field-validation/138394/2 "2008-05-22T16:03:33Z")

</div>

Only way I know how to do this is to add your own validate method e.g.:

# in your model…

def validate  
errors.add(:start\_date, “must be less than the end date”) if  
start\_date \>= end\_date  
end

There might be a helper to do this, but at the time I wasn’t aware of  
one so this is how I did it 😉

Hope this helps.

- Wayne

---

<div class="post-metadata">

**Author:** ![mrbless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@mrbless](https://www.ruby-forum.com/u/mrbless)\
**Post date:** [May 23, 2008, 2:48pm UTC](https://www.ruby-forum.com/t/date-field-validation/138394/3 "2008-05-23T14:48:12Z")

</div>

Thanks for your advice but I have tried that but it doesn’t work. Let  
me clarify what I have in view file  
Join Date:  
\<%=date\_select :job\_experience, :join\_date,  
:start\_year=\>Time.now.year, :end\_year=\>1900,  
:order=\>[:month,:year],:discard\_type=\>true,:day=\>false,  
:include\_blank=\>true  
%\>

Left Date:  
\<%=date\_select :job\_experience, :left\_date,  
:start\_year=\>Time.now.year, :end\_year=\>1900,  
:order=\>[:month,:year],:discard\_type=\>true,  
:day=\>false,:include\_blank=\>true  
%\>

Then I write the following in my model

def validate  
errors.add\_to\_base("left date cannot be less than join date ") if  
left\_date\<join\_date  
end

But the validation doesn’t work. Please Rails Lover give me some  
suggestion.
