aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2009-03-10 21:37:09 +0100
committerXavier Noria <fxn@hashref.com>2009-03-10 21:37:49 +0100
commitd02837401b24ff472eaf9b4efd82e09530012563 (patch)
treecda4e67556bfd5e1bbcb66a474958fff91b87887 /railties
parentf8c573abc1df0d18969bf690c969f9b1cd3aac04 (diff)
downloadrails-d02837401b24ff472eaf9b4efd82e09530012563.tar.gz
rails-d02837401b24ff472eaf9b4efd82e09530012563.tar.bz2
rails-d02837401b24ff472eaf9b4efd82e09530012563.zip
revised section 8 of validations guide
Diffstat (limited to 'railties')
-rw-r--r--railties/guides/source/activerecord_validations_callbacks.textile34
1 files changed, 20 insertions, 14 deletions
diff --git a/railties/guides/source/activerecord_validations_callbacks.textile b/railties/guides/source/activerecord_validations_callbacks.textile
index 6a2208dea6..78eed8e20a 100644
--- a/railties/guides/source/activerecord_validations_callbacks.textile
+++ b/railties/guides/source/activerecord_validations_callbacks.textile
@@ -659,9 +659,9 @@ h3. Displaying Validation Errors in the View
Rails provides built-in helpers to display the error messages of your models in your view templates.
-h4. error_messages and error_messages_for
+h4. +error_messages+ and +error_messages_for+
-When creating a form with the form_for helper, you can use the error_messages method on the form builder to render all failed validation messages for the current model instance.
+When creating a form with the +form_for+ helper, you can use the +error_messages+ method on the form builder to render all failed validation messages for the current model instance.
<ruby>
class Product < ActiveRecord::Base
@@ -687,6 +687,8 @@ end
<% end %>
</erb>
+To get the idea, if you submit the form with empty fields you typically get this back, though styles are indeed missing by default:
+
!images/error_messages.png(Error messages)!
You can also use the +error_messages_for+ helper to display the error messages of a model assigned to a view template. It's very similar to the previous example and will achieve exactly the same result.
@@ -713,41 +715,45 @@ If you pass +nil+ to any of these options, it will get rid of the respective sec
h4. Customizing the Error Messages CSS
-It's also possible to change the CSS classes used by the +error_messages+ helper. These classes are automatically defined at the *scaffold.css* file, generated by the scaffold script. If you're not using scaffolding, you can still define those CSS classes at your CSS files. Here is a list of the default CSS classes.
+The selectors to customize the style of error messages are:
-* +.fieldWithErrors+ - Style for the form fields with errors.
+* +.fieldWithErrors+ - Style for the form fields and labels with errors.
* +#errorExplanation+ - Style for the +div+ element with the error messages.
* +#errorExplanation h2+ - Style for the header of the +div+ element.
* +#errorExplanation p+ - Style for the paragraph that holds the message that appears right below the header of the +div+ element.
-* +#errorExplanation ul li+ - Style for the list of error messages.
+* +#errorExplanation ul li+ - Style for the list items with individual error messages.
+
+Scaffolding for example generates +public/stylesheets/scaffold.css+, which defines the red-based style you saw above.
+
+The name of the class and the id can be changed with the +:class+ and +:id+ options, accepted by both helpers.
h4. Customizing the Error Messages HTML
-By default, form fields with errors are displayed enclosed by a +div+ element with the +fieldWithErrors+ CSS class. However, it's possible to override the way Rails treats those fields by default.
+By default, form fields with errors are displayed enclosed by a +div+ element with the +fieldWithErrors+ CSS class. However, it's possible to override that.
+
+The way form fields with errors are treated is defined by +ActionView::Base.field_error_proc+. This is a +Proc+ that receives two parameters:
+
+* A string with the HTML tag
+* An instance of +ActionView::Helpers::InstanceTag+.
Here is a simple example where we change the Rails behaviour to always display the error messages in front of each of the form fields with errors. The error messages will be enclosed by a +span+ element with a +validation-error+ CSS class. There will be no +div+ element enclosing the +input+ element, so we get rid of that red border around the text field. You can use the +validation-error+ CSS class to style it anyway you want.
<ruby>
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
if instance.error_message.kind_of?(Array)
- %(#{html_tag}<span class='validation-error'>&nbsp;
+ %(#{html_tag}<span class="validation-error">&nbsp;
#{instance.error_message.join(',')}</span>)
else
- %(#{html_tag}<span class='validation-error'>&nbsp;
+ %(#{html_tag}<span class="validation-error">&nbsp;
#{instance.error_message}</span>)
end
end
</ruby>
-This will result in something like the following content:
+This will result in something like the following:
!images/validation_error_messages.png(Validation error messages)!
-The way form fields with errors are treated is defined by the +ActionView::Base.field_error_proc+ Ruby Proc. This Proc receives two parameters:
-
-* A string with the HTML tag
-* An object of the +ActionView::Helpers::InstanceTag+ class.
-
h3. Callbacks Overview
Callbacks are methods that get called at certain moments of an object's lifecycle. With callbacks it's possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.