Form validation - keepin correct fields displayed on refresh

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

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

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

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

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.

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

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs