|
1 | 1 | module ActiveModel
|
2 |
| - |
3 | 2 | # == Active Model Format Validator
|
4 | 3 | module Validations
|
5 | 4 | class FormatValidator < EachValidator
|
@@ -42,50 +41,62 @@ def check_options_validity(options, name)
|
42 | 41 | end
|
43 | 42 |
|
44 | 43 | module HelperMethods
|
45 |
| - # Validates whether the value of the specified attribute is of the correct form, going by the regular expression provided. |
46 |
| - # You can require that the attribute matches the regular expression: |
| 44 | + # Validates whether the value of the specified attribute is of the correct form, |
| 45 | + # going by the regular expression provided. You can require that the attribute |
| 46 | + # matches the regular expression: |
47 | 47 | #
|
48 | 48 | # class Person < ActiveRecord::Base
|
49 | 49 | # validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
|
50 | 50 | # end
|
51 | 51 | #
|
52 |
| - # Alternatively, you can require that the specified attribute does _not_ match the regular expression: |
| 52 | + # Alternatively, you can require that the specified attribute does _not_ match |
| 53 | + # the regular expression: |
53 | 54 | #
|
54 | 55 | # class Person < ActiveRecord::Base
|
55 | 56 | # validates_format_of :email, :without => /NOSPAM/
|
56 | 57 | # end
|
57 | 58 | #
|
58 |
| - # You can also provide a proc or lambda which will determine the regular expression that will be used to validate the attribute |
| 59 | + # You can also provide a proc or lambda which will determine the regular |
| 60 | + # expression that will be used to validate the attribute. |
59 | 61 | #
|
60 | 62 | # class Person < ActiveRecord::Base
|
61 | 63 | # # Admin can have number as a first letter in their screen name
|
62 |
| - # validates_format_of :screen_name, :with => lambda{ |person| person.admin? ? /\A[a-z0-9][a-z0-9_\-]*\Z/i : /\A[a-z][a-z0-9_\-]*\Z/i } |
| 64 | + # validates_format_of :screen_name, |
| 65 | + # :with => lambda{ |person| person.admin? ? /\A[a-z0-9][a-z0-9_\-]*\Z/i : /\A[a-z][a-z0-9_\-]*\Z/i } |
63 | 66 | # end
|
64 | 67 | #
|
65 |
| - # Note: use <tt>\A</tt> and <tt>\Z</tt> to match the start and end of the string, <tt>^</tt> and <tt>$</tt> match the start/end of a line. |
| 68 | + # Note: use <tt>\A</tt> and <tt>\Z</tt> to match the start and end of the string, |
| 69 | + # <tt>^</tt> and <tt>$</tt> match the start/end of a line. |
66 | 70 | #
|
67 |
| - # You must pass either <tt>:with</tt> or <tt>:without</tt> as an option. In addition, both must be a regular expression |
68 |
| - # or a proc or lambda, or else an exception will be raised. |
| 71 | + # You must pass either <tt>:with</tt> or <tt>:without</tt> as an option. In |
| 72 | + # addition, both must be a regular expression or a proc or lambda, or else an |
| 73 | + # exception will be raised. |
69 | 74 | #
|
70 | 75 | # Configuration options:
|
71 | 76 | # * <tt>:message</tt> - A custom error message (default is: "is invalid").
|
72 |
| - # * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+). |
73 |
| - # * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+). |
74 |
| - # * <tt>:with</tt> - Regular expression that if the attribute matches will result in a successful validation. |
75 |
| - # This can be provided as a proc or lambda returning regular expression which will be called at runtime. |
76 |
| - # * <tt>:without</tt> - Regular expression that if the attribute does not match will result in a successful validation. |
77 |
| - # This can be provided as a proc or lambda returning regular expression which will be called at runtime. |
| 77 | + # * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute |
| 78 | + # is +nil+ (default is +false+). |
| 79 | + # * <tt>:allow_blank</tt> - If set to true, skips this validation if the |
| 80 | + # attribute is blank (default is +false+). |
| 81 | + # * <tt>:with</tt> - Regular expression that if the attribute matches will |
| 82 | + # result in a successful validation. This can be provided as a proc or lambda |
| 83 | + # returning regular expression which will be called at runtime. |
| 84 | + # * <tt>:without</tt> - Regular expression that if the attribute does not match |
| 85 | + # will result in a successful validation. This can be provided as a proc or |
| 86 | + # lambda returning regular expression which will be called at runtime. |
78 | 87 | # * <tt>:on</tt> - Specifies when this validation is active. Runs in all
|
79 | 88 | # validation contexts by default (+nil+), other options are <tt>:create</tt>
|
80 | 89 | # and <tt>:update</tt>.
|
81 |
| - # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should |
82 |
| - # occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The |
83 |
| - # method, proc or string should return or evaluate to a true or false value. |
84 |
| - # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should |
85 |
| - # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The |
86 |
| - # method, proc or string should return or evaluate to a true or false value. |
| 90 | + # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the |
| 91 | + # validation should occur (e.g. <tt>:if => :allow_validation</tt>, or |
| 92 | + # <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The method, proc |
| 93 | + # or string should return or evaluate to a true or false value. |
| 94 | + # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if |
| 95 | + # the validation should not occur (e.g. <tt>:unless => :skip_validation</tt>, |
| 96 | + # or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The method, |
| 97 | + # proc or string should return or evaluate to a true or false value. |
87 | 98 | # * <tt>:strict</tt> - Specifies whether validation should be strict.
|
88 |
| - # See <tt>ActiveModel::Validation#validates!</tt> for more information |
| 99 | + # See <tt>ActiveModel::Validation#validates!</tt> for more information. |
89 | 100 | def validates_format_of(*attr_names)
|
90 | 101 | validates_with FormatValidator, _merge_attributes(attr_names)
|
91 | 102 | end
|
|
0 commit comments