# Date as textfield, virtual attribute can't be empty?

**URL:** <https://www.ruby-forum.com/t/date-as-textfield-virtual-attribute-cant-be-empty/171768>\
**Category:** Rails\
**Created:** [July 8, 2009, 10:53pm UTC](https://www.ruby-forum.com/t/date-as-textfield-virtual-attribute-cant-be-empty/171768 "2009-07-08T22:53:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pink-ink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@pink-ink](https://www.ruby-forum.com/u/pink-ink)\
**Post date:** [July 8, 2009, 10:53pm UTC](https://www.ruby-forum.com/t/date-as-textfield-virtual-attribute-cant-be-empty/171768/1 "2009-07-08T22:53:37Z")

</div>

Hi Ruby comunity!

I made a virtual date attribute to input date into a textfield.

Here is my entry from models\tbl\_data.rb:

def date\_test\_string  
date\_test.strftime(’%d.%m.%Y’)  
end

def date\_test\_string=(date\_test\_str)  
self.date\_test = Date.strptime(date\_test\_str, ‘%d.%m.%Y’)  
rescue ArgumentError  
@date\_test\_invalid = true

end

def validate  
errors.add(:date\_test, “is invalid”) if @date\_test\_invalid  
end

My Problem is, when I try to input no date (empty textfield) the  
validation throws an error --\> invalid date.

When I disable “def validate” there is no error-message but the new  
emtpty date will not be saved into the database, the old date (like  
01.01.2009) appears again in the “show” page.

Do somebody knows where I did go wrong?

Thanks ahead for every suggestion!!

Regs,

Hermann
