Please describe how to change the way that Rails renders validation failures in a web form. For example, let''s say that your model includes the following validations: - validates_presence_of :name - validates_length_of :name, :maximum=>40 The result of violating one of the above statements is that a list of errors are displayed on the new/edit action -- and the corresponding textbox is surrounded with a red box. Instead, I would prefer to place a red star adjacent to the textbox in violation. Thank you for your insights, JG
On 16.6.2005, at 10:14, John Greek wrote:> Please describe how to change the way that Rails > renders validation failures in a web form. > > For example, let''s say that your model includes the > following validations: > > - validates_presence_of :name > - validates_length_of :name, :maximum=>40 > > The result of violating one of the above statements is > that a list of errors are displayed on the new/edit > action -- and the corresponding textbox is surrounded > with a red box. > > Instead, I would prefer to place a red star adjacent > to the textbox in violation.All that rails does to the field is that it changes its css class to "fieldWithErrors". You can edit public/stylesheets/scaffold.css to show the star instead of using red background. You can take the list of errors away from the template if you don''t need it. It normally uses the error_messages_for [1] helper method to show the errors so you can search for it to find it in your templates. //jarkko [1] http://rails.rubyonrails.com/classes/ActionView/Helpers/ ActiveRecordHelper.html#M000377> > Thank you for your insights, JG > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thank you for your response-- Rails wraps invalid form fields with a div tag as follows: <div class="fieldWithErrors"> <input id="user_firstname" name="user[firstname]" size="30" type="text" value="" /> </div> How can I modify either rails or the fieldWithErrors css to produce a red star rather than using a red background around the whole field? I''m not clear that this can be done with only a css change. Please elaborate. Thanks, JG --- Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> > On 16.6.2005, at 10:14, John Greek wrote: > > > Please describe how to change the way that Rails > > renders validation failures in a web form. > > > > For example, let''s say that your model includes > the > > following validations: > > > > - validates_presence_of :name > > - validates_length_of :name, :maximum=>40 > > > > The result of violating one of the above > statements is > > that a list of errors are displayed on the > new/edit > > action -- and the corresponding textbox is > surrounded > > with a red box. > > > > Instead, I would prefer to place a red star > adjacent > > to the textbox in violation. > > All that rails does to the field is that it changes > its css class to > "fieldWithErrors". You can edit > public/stylesheets/scaffold.css to show > the star instead of using red background. > > You can take the list of errors away from the > template if you don''t > need it. It normally uses the error_messages_for [1] > helper method to > show the errors so you can search for it to find it > in your templates. > > //jarkko > > [1] >http://rails.rubyonrails.com/classes/ActionView/Helpers/> > ActiveRecordHelper.html#M000377 > > > > > Thank you for your insights, JG > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > -- > Jarkko Laine > http://jlaine.net > http://odesign.fi > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Jun 16, 2005, at 1:14 AM, John Greek wrote:><snip>> The result of violating one of the above statements is > that a list of errors are displayed on the new/edit > action -- and the corresponding textbox is surrounded > with a red box. > > Instead, I would prefer to place a red star adjacent > to the textbox in violation.In our application, we decided to use the same helper, but just over- rode it like this: module ActionView::Helpers::ActiveRecordHelper def error_messages_for(object_name, options = {}) options = options.symbolize_keys object = instance_eval "@#{object_name}" unless object.errors.empty? content_tag("div", content_tag(options[:header_tag] || "h2", "Please correct the following:") + content_tag("ul", object.errors.full_messages.collect { | msg| content_tag("li", msg) }), "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation" ) end end #def error_messages_for end #class ActionView::Helpers::ActiveRecordHelper This code was then placed inside our application''s lib folder, and ''require''d at the bottom of config/environment.rb. You''d still have to do what Jarkko suggested with the css to make a red star appear (or, you could similarly override the tag helper methods, but I''m not sure it''s worth the trouble). Duane Johnson (canadaduane) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
A while ago I saw this snippet on the mailinglist: # override the default error_wrapping provided in # action_view/helpers/active_record_helper.rb module ActionView module Helpers class InstanceTag alias_method :old_error_wrappinng, :error_wrapping def error_wrapping(html_tag, has_error) html_tag end end end end With modifying the error_wrapping method you could do something like this (this is untested!): def error_wrapping(html_tag, has_error) html_tag + "*" end Flurin John Greek wrote:>Thank you for your response-- > >Rails wraps invalid form fields with a div tag as >follows: > ><div class="fieldWithErrors"> > ><input id="user_firstname" name="user[firstname]" >size="30" type="text" value="" /> > ></div> > >How can I modify either rails or the fieldWithErrors >css to produce a red star rather than using a red >background around the whole field? > >I''m not clear that this can be done with only a css >change. Please elaborate. > >Thanks, JG > >--- Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote: > > > >>On 16.6.2005, at 10:14, John Greek wrote: >> >> >> >>>Please describe how to change the way that Rails >>>renders validation failures in a web form. >>> >>>For example, let''s say that your model includes >>> >>> >>the >> >> >>>following validations: >>> >>>- validates_presence_of :name >>>- validates_length_of :name, :maximum=>40 >>> >>>The result of violating one of the above >>> >>> >>statements is >> >> >>>that a list of errors are displayed on the >>> >>> >>new/edit >> >> >>>action -- and the corresponding textbox is >>> >>> >>surrounded >> >> >>>with a red box. >>> >>>Instead, I would prefer to place a red star >>> >>> >>adjacent >> >> >>>to the textbox in violation. >>> >>> >>All that rails does to the field is that it changes >>its css class to >>"fieldWithErrors". You can edit >>public/stylesheets/scaffold.css to show >>the star instead of using red background. >> >>You can take the list of errors away from the >>template if you don''t >>need it. It normally uses the error_messages_for [1] >>helper method to >>show the errors so you can search for it to find it >>in your templates. >> >>//jarkko >> >>[1] >> >> >> >http://rails.rubyonrails.com/classes/ActionView/Helpers/ > > >>ActiveRecordHelper.html#M000377 >> >> >> >>>Thank you for your insights, JG >>>_______________________________________________ >>>Rails mailing list >>>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> >>> >>> >>http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >>-- >>Jarkko Laine >>http://jlaine.net >>http://odesign.fi >> >> >>>_______________________________________________ >>> >>> >>Rails mailing list >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > >