From 0668ab67bf6b32950113995303446840ae57d5d9 Mon Sep 17 00:00:00 2001 From: CassioMarques Date: Mon, 10 Nov 2008 20:24:51 -0200 Subject: Added documentation for validates_presence_of --- .../html/activerecord_validations_callbacks.html | 29 ++++++++++++++++++++++ .../source/activerecord_validations_callbacks.txt | 21 ++++++++++++++++ 2 files changed, 50 insertions(+) (limited to 'railties/doc') diff --git a/railties/doc/guides/html/activerecord_validations_callbacks.html b/railties/doc/guides/html/activerecord_validations_callbacks.html index 6feaae68ea..2e5044eb6e 100644 --- a/railties/doc/guides/html/activerecord_validations_callbacks.html +++ b/railties/doc/guides/html/activerecord_validations_callbacks.html @@ -539,6 +539,35 @@ http://www.gnu.org/software/src-highlite -->

The default error message for validates_numericallity_of is "is not a number".

3.10. The validates_presence_of helper

+

This helper validates that the attributes are not empty. It uses the blank? method to check if the value is either nil or an empty string (if the string has only spaces, it will still be considered empty).

+
+
+
class Person < ActiveRecord::Base
+  validates_presence_of :name, :login, :email
+end
+
+
+ + + +
+Note +If you want to be sure that an association is present, you'll need to test if the foreign key used to map the association is present, and not the associated object itself.
+
+
+
+
class LineItem < ActiveRecord::Base
+  belongs_to :order
+  validates_presence_of :order_id
+end
+
+

The default error message for validates_presence_of is "can't be empty".

3.11. The validates_size_of helper

3.12. The validates_uniqueness_of+ helper

diff --git a/railties/doc/guides/source/activerecord_validations_callbacks.txt b/railties/doc/guides/source/activerecord_validations_callbacks.txt index b26ebfed41..568ab8bc23 100644 --- a/railties/doc/guides/source/activerecord_validations_callbacks.txt +++ b/railties/doc/guides/source/activerecord_validations_callbacks.txt @@ -237,6 +237,27 @@ The default error message for +validates_numericallity_of+ is "_is not a number_ === The +validates_presence_of+ helper +This helper validates that the attributes are not empty. It uses the +blank?+ method to check if the value is either +nil+ or an empty string (if the string has only spaces, it will still be considered empty). + +[source, ruby] +------------------------------------------------------------------ +class Person < ActiveRecord::Base + validates_presence_of :name, :login, :email +end +------------------------------------------------------------------ + +NOTE: If you want to be sure that an association is present, you'll need to test if the foreign key used to map the association is present, and not the associated object itself. + +[source, ruby] +------------------------------------------------------------------ +class LineItem < ActiveRecord::Base + belongs_to :order + validates_presence_of :order_id +end +------------------------------------------------------------------ + +The default error message for +validates_presence_of+ is "_can't be empty_". + === The +validates_size_of+ helper === The validates_uniqueness_of+ helper -- cgit v1.2.3