Wes Gamble
2006-Jul-14 16:04 UTC
[Rails] 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 -- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Jul-14 16:30 UTC
[Rails] Re: Form validation - keepin correct fields displayed on ref
Wes Gamble 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, > WesI 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 -- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Jul-14 17:15 UTC
[Rails] Re: Form validation - keepin correct fields displayed on ref
Wes Gamble wrote:> Wes Gamble 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, > WesMy 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 -- Posted via http://www.ruby-forum.com/.
mat@pobblebonk.net
2006-Jul-14 17:49 UTC
[Rails] Re: Form validation - keepin correct fields displayed on ref
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 Gamble wrote: >> Wes Gamble 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Brian Hogan
2006-Jul-14 18:08 UTC
[Rails] Re: Form validation - keepin correct fields displayed on ref
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. On 7/14/06, Wes Gamble <weyus@att.net> wrote:> > Wes Gamble wrote: > > Wes Gamble 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060714/715849f7/attachment-0001.html
Wes Gamble
2006-Jul-14 18:10 UTC
[Rails] Re: Re: Form validation - keepin correct fields displayed o
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 NewtonMathew, 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" <email address> 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: <TD class="list-label"><LABEL for="email_from">From (name):</LABEL></TD> <TD><%= text_field(:current_job, ''from_name'') %></TD> <TD class="list-label"><LABEL for="email_from_email">From (email address):</LABEL></TD> <TD><%= text_field(:current_job, ''from_email'') %></TD> 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 -- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Jul-14 18:18 UTC
[Rails] Re: Re: Form validation - keepin correct fields displayed on
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 -- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Jul-14 18:43 UTC
[Rails] Re: Re: Form validation - keepin correct fields displayed on
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 -- Posted via http://www.ruby-forum.com/.