# Form validation - keepin correct fields displayed on refresh

**URL:** <https://www.ruby-forum.com/t/form-validation-keepin-correct-fields-displayed-on-refresh/98425>\
**Category:** Rails\
**Created:** [July 14, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/form-validation-keepin-correct-fields-displayed-on-refresh/98425 "2006-07-14T18:03:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 14, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/form-validation-keepin-correct-fields-displayed-on-refresh/98425/1 "2006-07-14T18:03:56Z")

</div>

All,

I’m finally doing my first real form in Rails - the model object that  
I’m entering information for has 8 validations so far.

If I type in good values for all the fields but one, I get the pretty  
validation, and the nice field highlighting, but all of the fields are  
cleared, forcing me to retype all of that info. That is a big drag.

Is there a standard way to get the equivalent of what we call a “form  
bean” in the Java/Struts world?

Why is the object that’s backing the form being cleared out every time I  
re-render the form after validation fails?

Has anyone implemented another object - we might call it the “view  
model,” which keeps track of the state of the form until it’s  
successfully submitted?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 14, 2006, 6:29pm UTC](https://www.ruby-forum.com/t/form-validation-keepin-correct-fields-displayed-on-refresh/98425/2 "2006-07-14T18:29:59Z")

</div>

Wes G. wrote:

> All,
> 
> I’m finally doing my first real form in Rails - the model object that  
> I’m entering information for has 8 validations so far.
> 
> If I type in good values for all the fields but one, I get the pretty  
> validation, and the nice field highlighting, but all of the fields are  
> cleared, forcing me to retype all of that info. That is a big drag.
> 
> Is there a standard way to get the equivalent of what we call a “form  
> bean” in the Java/Struts world?
> 
> Why is the object that’s backing the form being cleared out every time I  
> re-render the form after validation fails?
> 
> Has anyone implemented another object - we might call it the “view  
> model,” which keeps track of the state of the form until it’s  
> successfully submitted?
> 
> Thanks,  
> Wes

I think I realize what I have done wrong here. I don’t think I’m using  
read\_attribute and write\_attribute correctly in some of my display-only  
getters and setters and therefore, the changes are not making it into  
the attribute hash.

I will confirm.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 14, 2006, 7:15pm UTC](https://www.ruby-forum.com/t/form-validation-keepin-correct-fields-displayed-on-refresh/98425/3 "2006-07-14T19:15:07Z")

</div>

Wes G. wrote:

> Wes G. wrote:
> 
> > All,
> > 
> > I’m finally doing my first real form in Rails - the model object that  
> > I’m entering information for has 8 validations so far.
> > 
> > If I type in good values for all the fields but one, I get the pretty  
> > validation, and the nice field highlighting, but all of the fields are  
> > cleared, forcing me to retype all of that info. That is a big drag.
> > 
> > Is there a standard way to get the equivalent of what we call a “form  
> > bean” in the Java/Struts world?
> > 
> > Why is the object that’s backing the form being cleared out every time I  
> > re-render the form after validation fails?
> > 
> > Has anyone implemented another object - we might call it the “view  
> > model,” which keeps track of the state of the form until it’s  
> > successfully submitted?
> > 
> > Thanks,  
> > Wes
> 
> I think I realize what I have done wrong here. I don’t think I’m using  
> read\_attribute and write\_attribute correctly in some of my display-only  
> getters and setters and therefore, the changes are not making it into  
> the attribute hash.
> 
> I will confirm.
> 
> Thanks,  
> Wes

My problem was not using write\_attribute() and read\_attribute()  
correctly in my object. Hopefully, the importance of using these two  
methods will be emphasized as the need of facade columns in your model  
object is often necessary.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 14, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/form-validation-keepin-correct-fields-displayed-on-refresh/98425/4 "2006-07-14T19:50:50Z")

</div>

Hi Wes,  
I am a newbie to AJAX style web development and was hoping you  
or  
someone else could explain what facade columns are and  
why/how/where are they used.

Thank you in advance,  
Mathew Newton

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 14, 2006, 8:11pm UTC](https://www.ruby-forum.com/t/form-validation-keepin-correct-fields-displayed-on-refresh/98425/5 "2006-07-14T20:11:24Z")

</div>

Wes:

Please share an example of one of your facade columns if you have the  
time.  
I’m sure there are many that would be interested.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 14, 2006, 8:18pm UTC](https://www.ruby-forum.com/t/form-validation-keepin-correct-fields-displayed-on-refresh/98425/6 "2006-07-14T20:18:36Z")

</div>

The thing that tripped me up was that because I couldn’t see any data, I  
thought that my custom getters weren’t being called when I refreshed the  
page.

But in fact, my custom getters were being called, but the data that they  
were referencing was empty, because they were being called on a _brand  
new_ object.

You can see that the object in your view is different every time by  
putting:

\<%= "Object id: " + @your\_object\_name.object\_id.to\_s %\>

into your page and POSTing with errors. You will see a different object  
id every time.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 14, 2006, 8:10pm UTC](https://www.ruby-forum.com/t/form-validation-keepin-correct-fields-displayed-on-refresh/98425/7 "2006-07-14T20:10:20Z")

</div>

unknown wrote:

> Hi Wes,  
> I am a newbie to AJAX style web development and was hoping you  
> or  
> someone else could explain what facade columns are and  
> why/how/where are they used.
> 
> Thank you in advance,  
> Mathew Newton

Mathew,

Basically, any time that you have to customize the look and feel of a  
column from the DB, you will have to do something like this.

Here’s the real live example I just figured out (although I’m still  
having some issues with the validation :]).

I have a legacy DB schema that contains one field for an email called  
“fromLine”.  
The fromLine field is formatted as:  
“Name of person”

But I want to display this information and allow for data entry with two  
fields - one for name and one for email address. So in order to do  
that, I need to split up (to read) and merge together (to write) the  
name and email address in order to interact correctly with my DB column.

Here’s what you have in the view:

From (name): \<%= text\_field(:current\_job, 'from\_name') %\> From (email address): \<%= text\_field(:current\_job, 'from\_email') %\>

Here’s what you have to do in the model to make this work - notice the  
use of read\_attribute and write\_attribute - you have to do this because  
the form helpers interact with the actual DB attributes, not the custom  
ones that you create in the model. The attributes hash persists no  
matter what happens. If you have errors, my understanding is that the  
object that you get in your view is a brand new object when you  
redisplay the page, so it isn’t enough to just reference your custom  
attributes (because they would be empty since it’s a new object).

Basically, you have to use the attribute hash to “hold” your custom  
stuff. Attributes that aren’t modified just go directly to the  
attribute hash.

public  
def from\_name  
line = read\_attribute(:fromLine)  
if (! line.nil?) && (md = line.match(/"(.\*)"/))  
md[1]  
else  
‘’  
end  
end

public  
def from\_name=(name)  
line = “”#{name}" \<#{self.from\_email}\>"  
self.fromLine = line  
end

public  
def from\_email  
line = read\_attribute(:fromLine)  
if (! line.nil?) && (md = line.match(/\<(.\*)\>/))  
md[1]  
else  
‘’  
end  
end

public  
def from\_email=(email)  
line = “”#{self.from\_name}" \<#{email}\>"  
self.fromLine = line  
end

public  
def fromLine=(line)  
write\_attribute(:fromLine, line)  
end

HTH,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 14, 2006, 8:43pm UTC](https://www.ruby-forum.com/t/form-validation-keepin-correct-fields-displayed-on-refresh/98425/8 "2006-07-14T20:43:11Z")

</div>

On the other hand, you may not want to listen to me since now NONE of my  
validations appear to be running.

Sigh. I still love Rails, but why does it have to be such a rocky road  
for me :)?

WG
