SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple form error

    As much as I love the simplified error messages and validations in rails, it seems like a huge process to get anything customized and I haven't found any resources that seem to be easy. What I want here is pretty simple:

    I have a form that validates_presence_of a few items. All I want is to change the markup of the error message displayed when there's an error. Here's my code:

    Code:
    <div><%= f.label :name, 'Full Name', :class => 'strong' %><%= f.error_message_on :name %></div>
    <div><%= f.text_field :name %></div>
    So, I just want the f.error_message_on to be in a span instead of a div, so it's display directly beside the label, not underneath.

    Reading through form builders and error handling and such seems extremely complicated. Is there no simple way to just display this error in it's own span so I can style it appropriately?

  2. #2
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As an addition to this, it looks like even if I could change the error markup easily, my label is still wrapped in it's own <div class="fieldWithErrors"> so it displays underneath anyway.

    So, does anyone have a simple example of a custom form error?

  3. #3
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found this article:
    http://ramblingsonrails.com/how-to-m...ilder-in-rails

    Does anyone have a good resource for custom form builders? I don't understand this really, but I'm assuming his label definition overwrites the default label definition in the FormBuilder class. So presumably I could do this for any form item?

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tizzod View Post
    I think the key to understanding what that custom builder is doing comes when you realise how errors are passed from the model to the view/helper. The object created from a model will carry any errors in an errors method. So:

    Say you have a model Thing. In your controller you have the code:
    Code:
    @thing = Thing.find(params[:id])
    Which means the instance of Thing you are using is passed to the view via @thing. This @thing will have a method 'errors' that contains all the current error messages for this object. So you can do this:
    Code:
    all_thing_errors = @thing.errors
    The object returned by errors has it own set of methods including 'on' which allows you to grab the subset of errors that just apply to a particular method. So you can do this:
    Code:
    errors_on_thing_name = @thing.errors.on(:name) if @thing.errors
    If there is only one error, errors will contain just that error but if there are more, errors will contain an array.

    So you could just do:
    Code:
    <% errors_on_thing_name = @thing.errors.on(:name) if @thing.errors %>
    <%= f.text_field :name %>
    <%= content_tag('span', 
      (errors_on_thing_name.is_a?(Array) ? errors_on_thing_name.first : errors_on_thing_name)
      ) if errors_on_thing_name %>
    That's fairly close to what Andrew Timberlake is doing, except he is doing it in a far neater and more flexible way. Rather than actually specifying the object being passed to the view (in my example @thing) he uses objects available to the form to grab the object. See this line:
    Code:
    object = @template.instance_variable_get("@#{@object_name}")
    In my example I could use much of his code by simple replacing this line with:
    Code:
    object = @thing
    but the result would be code that only worked for @thing.

    By creating a new class ErrorFormBuilder based on the standard FormBuilder he allows a fairly simple way of adding his functionality to any form by simply adding:
    Code:
    :builder => ErrorFormBuilder
    This means he does the job in a way that can be used in any form. Very nice.

    I think this google search should give you more information on Formbuilder.

  5. #5
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the detailed response, that clears things up well. I had no idea about the errors method.

    This then kind of relates to another topic I posted
    http://www.sitepoint.com/forums/show...75#post4149775

    I tried accessing @image.errors, assuming that the validations set by attachment_fu would set an error, It was empty though is .errors set upon instantiation, or saving to the db? Because it's the saving to the DB messes up, image doesn't save, throws no error, photo saves and i'm left with a photo linking to a nil image.

  6. #6
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On save - that's when the validation is done.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •