# Advice, why value\_before\_type\_cast in FormHelper?

**URL:** <https://www.ruby-forum.com/t/advice-why-value-before-type-cast-in-formhelper/62349>\
**Category:** Rails\
**Created:** [June 2, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/advice-why-value-before-type-cast-in-formhelper/62349 "2006-06-02T23:03:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [June 2, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/advice-why-value-before-type-cast-in-formhelper/62349/1 "2006-06-02T23:03:37Z")

</div>

I am wondering why is in creating text fields with form\_helper used  
value\_before\_type\_cast instead of attribute value for field value?

I set rails enviroment so date fields are in european format  
(dd.mm.yyyy) and would like to have same format in forms, but because of  
value\_before\_type\_cast is called, form fields are filled with  
‘yyyy-mm-dd’ or timestamp integer, etc.

Thoughts / comments / sugestions?  
thanks in advance,

–  
Bojan M.  
Informatika Mihelac, Bojan M. s.p. | [www.informatikamihelac.com](http://www.informatikamihelac.com)  
 → tools, scripts, tricks from our code lab: [http://source.mihelac.org](http://source.mihelac.org)

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [June 3, 2006, 9:49am UTC](https://www.ruby-forum.com/t/advice-why-value-before-type-cast-in-formhelper/62349/2 "2006-06-03T09:49:53Z")

</div>

Bojan M. wrote:

> 

or is there any tutorial how to override fields produced with  
FormHelper?

thanks,  
Bojan

–  
Bojan M.  
Informatika Mihelac, Bojan M. s.p. | [www.informatikamihelac.com](http://www.informatikamihelac.com)  
 → tools, scripts, tricks from our code lab: [http://source.mihelac.org](http://source.mihelac.org)

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [June 4, 2006, 9:12am UTC](https://www.ruby-forum.com/t/advice-why-value-before-type-cast-in-formhelper/62349/3 "2006-06-04T09:12:42Z")

</div>

I’ve asked this many times before and have never gotten an answer.

However, form fields will look at the attribute on the model if it’s  
not in the database.

Here’s a simplified (non-working) example to modify the formatting of  
the created\_at field

class YourModel \< ActiveRecord::base

def formatted\_date=(date)  
self.created\_at = date  
end

def formatted\_date  
# apply your special formatting here  
end

end

Then, in your form, just do

\<%= text\_field “your\_model”, “formatted\_date” %\>

That’s how I manage to do the special formatting that I need and still  
make use of the helpers. I did write a plugin that allowed you to pass  
a formatted string into the text\_field helper, but I threw it away  
because it was too difficult to make it work for select tags,  
options\_for\_select, and others. I found the above approach to be a  
decent solution.

If anyone from the core can comment on this, that would be great.

Hope that helps you out.

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [June 5, 2006, 10:07am UTC](https://www.ruby-forum.com/t/advice-why-value-before-type-cast-in-formhelper/62349/4 "2006-06-05T10:07:29Z")

</div>

Thanks Brian,  
in the middtime I succeed to create custom date select field that works  
with custom date format (dd.mm.yyyy). There were few new problems I run  
into while solving these so I would post all the code and explanation on  
my site asap.

best regards,  
Bojan

Brian H. wrote:

> def formatted\_date=(date)  
> Then, in your form, just do  
> If anyone from the core can comment on this, that would be great.
> 
> > > (dd.mm.yyyy) and would like to have same format in forms, but  
> > > thanks,
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Bojan M.  
Informatika Mihelac, Bojan M. s.p. | [www.informatikamihelac.com](http://www.informatikamihelac.com)  
 → tools, scripts, tricks from our code lab: [http://source.mihelac.org](http://source.mihelac.org)
