text:
text.html.markdown meta.paragraph.markdown: [0, 26], {Active Record Validations
}
text.html.markdown meta.paragraph.markdown markup.heading.1.markdown punctuation.definition.heading.markdown: [26, 51], {=========================}
text.html.markdown meta.paragraph.markdown: [51, 52], {
}
text.html.markdown: [52, 53], {
}
text.html.markdown meta.paragraph.markdown: [53, 133], {This guide teaches you how to validate the state of objects before they go into
}
text.html.markdown meta.paragraph.markdown: [133, 189], {the database using Active Record's validations feature.
}
text.html.markdown meta.paragraph.markdown: [189, 190], {
}
text.html.markdown meta.paragraph.markdown: [190, 231], {After reading this guide, you will know:
}
text.html.markdown meta.paragraph.markdown: [231, 232], {
}
text.html.markdown markup.list.unnumbered.markdown punctuation.definition.list_item.markdown: [232, 233], {*}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [233, 234], { }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [234, 292], {How to use the built-in Active Record validation helpers.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [292, 344], {* How to create your own custom validation methods.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [344, 419], {* How to work with the error messages generated by the validation process.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [419, 420], {
}
text.html.markdown: [420, 501], {--------------------------------------------------------------------------------
}
text.html.markdown: [501, 502], {
}
text.html.markdown meta.paragraph.markdown: [502, 523], {Validations Overview
}
text.html.markdown meta.paragraph.markdown markup.heading.2.markdown punctuation.definition.heading.markdown: [523, 543], {--------------------}
text.html.markdown meta.paragraph.markdown: [543, 544], {
}
text.html.markdown: [544, 545], {
}
text.html.markdown meta.paragraph.markdown: [545, 592], {Here's an example of a very simple validation:
}
text.html.markdown meta.paragraph.markdown: [592, 593], {
}
text.html.markdown meta.paragraph.markdown: [593, 601], {```ruby
}
text.html.markdown meta.paragraph.markdown: [601, 614], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [614, 615], {<}
text.html.markdown meta.paragraph.markdown: [615, 635], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [635, 669], {  validates :name, presence: true
}
text.html.markdown meta.paragraph.markdown: [669, 673], {end
}
text.html.markdown meta.paragraph.markdown: [673, 674], {
}
text.html.markdown meta.paragraph.markdown: [674, 723], {Person.create(name: "John Doe").valid? # => true
}
text.html.markdown meta.paragraph.markdown: [723, 766], {Person.create(name: nil).valid? # => false
}
text.html.markdown meta.paragraph.markdown: [766, 770], {```
}
text.html.markdown meta.paragraph.markdown: [770, 771], {
}
text.html.markdown meta.paragraph.markdown: [771, 824], {As you can see, our validation lets us know that our }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [824, 825], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [825, 831], {Person}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [831, 832], {`}
text.html.markdown meta.paragraph.markdown: [832, 846], { is not valid
}
text.html.markdown meta.paragraph.markdown: [846, 856], {without a }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [856, 857], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [857, 861], {name}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [861, 862], {`}
text.html.markdown meta.paragraph.markdown: [862, 885], { attribute. The second }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [885, 886], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [886, 892], {Person}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [892, 893], {`}
text.html.markdown meta.paragraph.markdown: [893, 923], { will not be persisted to the
}
text.html.markdown meta.paragraph.markdown: [923, 933], {database.
}
text.html.markdown meta.paragraph.markdown: [933, 934], {
}
text.html.markdown meta.paragraph.markdown: [934, 1013], {Before we dig into more details, let's talk about how validations fit into the
}
text.html.markdown meta.paragraph.markdown: [1013, 1046], {big picture of your application.
}
text.html.markdown meta.paragraph.markdown: [1046, 1047], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [1047, 1050], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [1050, 1051], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown: [1051, 1071], {Why Use Validations?}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [1071, 1072], {
}
text.html.markdown: [1072, 1073], {
}
text.html.markdown meta.paragraph.markdown: [1073, 1144], {Validations are used to ensure that only valid data is saved into your
}
text.html.markdown meta.paragraph.markdown: [1144, 1222], {database. For example, it may be important to your application to ensure that
}
text.html.markdown meta.paragraph.markdown: [1222, 1297], {every user provides a valid email address and mailing address. Model-level
}
text.html.markdown meta.paragraph.markdown: [1297, 1376], {validations are the best way to ensure that only valid data is saved into your
}
text.html.markdown meta.paragraph.markdown: [1376, 1455], {database. They are database agnostic, cannot be bypassed by end users, and are
}
text.html.markdown meta.paragraph.markdown: [1455, 1527], {convenient to test and maintain. Rails makes them easy to use, provides
}
text.html.markdown meta.paragraph.markdown: [1527, 1607], {built-in helpers for common needs, and allows you to create your own validation
}
text.html.markdown meta.paragraph.markdown: [1607, 1624], {methods as well.
}
text.html.markdown meta.paragraph.markdown: [1624, 1625], {
}
text.html.markdown meta.paragraph.markdown: [1625, 1700], {There are several other ways to validate data before it is saved into your
}
text.html.markdown meta.paragraph.markdown: [1700, 1774], {database, including native database constraints, client-side validations,
}
text.html.markdown meta.paragraph.markdown: [1774, 1843], {controller-level validations. Here's a summary of the pros and cons:
}
text.html.markdown meta.paragraph.markdown: [1843, 1844], {
}
text.html.markdown markup.list.unnumbered.markdown punctuation.definition.list_item.markdown: [1844, 1845], {*}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [1845, 1846], { }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [1846, 1923], {Database constraints and/or stored procedures make the validation mechanisms
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [1923, 1997], {  database-dependent and can make testing and maintenance more difficult.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [1997, 2073], {  However, if your database is used by other applications, it may be a good
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [2073, 2141], {  idea to use some constraints at the database level. Additionally,
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [2141, 2220], {  database-level validations can safely handle some things (such as uniqueness
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [2220, 2292], {  in heavily-used tables) that can be difficult to implement otherwise.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [2292, 2370], {* Client-side validations can be useful, but are generally unreliable if used
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [2370, 2445], {  alone. If they are implemented using JavaScript, they may be bypassed if
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [2445, 2521], {  JavaScript is turned off in the user's browser. However, if combined with
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [2521, 2599], {  other techniques, client-side validation can be a convenient way to provide
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [2599, 2654], {  users with immediate feedback as they use your site.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [2654, 2726], {* Controller-level validations can be tempting to use, but often become
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [2726, 2804], {  unwieldy and difficult to test and maintain. Whenever possible, it's a good
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [2804, 2879], {  idea to keep your controllers skinny, as it will make your application a
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [2879, 2920], {  pleasure to work with in the long run.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [2920, 2921], {
}
text.html.markdown: [2921, 2997], {Choose these in certain, specific cases. It's the opinion of the Rails team
}
text.html.markdown meta.paragraph.markdown: [2997, 3074], {that model-level validations are the most appropriate in most circumstances.
}
text.html.markdown meta.paragraph.markdown: [3074, 3075], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [3075, 3078], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [3078, 3079], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown: [3079, 3107], {When Does Validation Happen?}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [3107, 3108], {
}
text.html.markdown: [3108, 3109], {
}
text.html.markdown meta.paragraph.markdown: [3109, 3186], {There are two kinds of Active Record objects: those that correspond to a row
}
text.html.markdown meta.paragraph.markdown: [3186, 3266], {inside your database and those that do not. When you create a fresh object, for
}
text.html.markdown meta.paragraph.markdown: [3266, 3284], {example using the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [3284, 3285], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [3285, 3288], {new}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [3288, 3289], {`}
text.html.markdown meta.paragraph.markdown: [3289, 3342], { method, that object does not belong to the database
}
text.html.markdown meta.paragraph.markdown: [3342, 3361], {yet. Once you call }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [3361, 3362], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [3362, 3366], {save}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [3366, 3367], {`}
text.html.markdown meta.paragraph.markdown: [3367, 3411], { upon that object it will be saved into the
}
text.html.markdown meta.paragraph.markdown: [3411, 3462], {appropriate database table. Active Record uses the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [3462, 3463], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [3463, 3474], {new_record?}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [3474, 3475], {`}
text.html.markdown meta.paragraph.markdown: [3475, 3485], { instance
}
text.html.markdown meta.paragraph.markdown: [3485, 3558], {method to determine whether an object is already in the database or not.
}
text.html.markdown meta.paragraph.markdown: [3558, 3609], {Consider the following simple Active Record class:
}
text.html.markdown meta.paragraph.markdown: [3609, 3610], {
}
text.html.markdown meta.paragraph.markdown: [3610, 3618], {```ruby
}
text.html.markdown meta.paragraph.markdown: [3618, 3631], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [3631, 3632], {<}
text.html.markdown meta.paragraph.markdown: [3632, 3652], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [3652, 3656], {end
}
text.html.markdown meta.paragraph.markdown: [3656, 3660], {```
}
text.html.markdown meta.paragraph.markdown: [3660, 3661], {
}
text.html.markdown meta.paragraph.markdown: [3661, 3704], {We can see how it works by looking at some }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [3704, 3705], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [3705, 3718], {rails console}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [3718, 3719], {`}
text.html.markdown meta.paragraph.markdown: [3719, 3728], { output:
}
text.html.markdown meta.paragraph.markdown: [3728, 3729], {
}
text.html.markdown meta.paragraph.markdown: [3729, 3737], {```ruby
}
text.html.markdown meta.paragraph.markdown: [3737, 3753], {$ rails console
}
text.html.markdown: [3753, 3789], {>> p = Person.new(name: "John Doe")
}
text.html.markdown meta.paragraph.markdown: [3789, 3793], {=> #}
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [3793, 3794], {<}
text.html.markdown meta.paragraph.markdown: [3794, 3835], {Person id: nil, name: "John Doe", created}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [3835, 3836], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [3836, 3852], {at: nil, updated}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [3852, 3853], {_}
text.html.markdown meta.paragraph.markdown: [3853, 3862], {at: nil>
}
text.html.markdown: [3862, 3879], {>> p.new_record?
}
text.html.markdown meta.paragraph.markdown: [3879, 3887], {=> true
}
text.html.markdown: [3887, 3897], {>> p.save
}
text.html.markdown meta.paragraph.markdown: [3897, 3905], {=> true
}
text.html.markdown: [3905, 3922], {>> p.new_record?
}
text.html.markdown meta.paragraph.markdown: [3922, 3931], {=> false
}
text.html.markdown meta.paragraph.markdown: [3931, 3935], {```
}
text.html.markdown meta.paragraph.markdown: [3935, 3936], {
}
text.html.markdown meta.paragraph.markdown: [3936, 3986], {Creating and saving a new record will send an SQL }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [3986, 3987], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [3987, 3993], {INSERT}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [3993, 3994], {`}
text.html.markdown meta.paragraph.markdown: [3994, 4012], { operation to the
}
text.html.markdown meta.paragraph.markdown: [4012, 4067], {database. Updating an existing record will send an SQL }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4067, 4068], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [4068, 4074], {UPDATE}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4074, 4075], {`}
text.html.markdown meta.paragraph.markdown: [4075, 4086], { operation
}
text.html.markdown meta.paragraph.markdown: [4086, 4163], {instead. Validations are typically run before these commands are sent to the
}
text.html.markdown meta.paragraph.markdown: [4163, 4239], {database. If any validations fail, the object will be marked as invalid and
}
text.html.markdown meta.paragraph.markdown: [4239, 4274], {Active Record will not perform the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4274, 4275], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [4275, 4281], {INSERT}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4281, 4282], {`}
text.html.markdown meta.paragraph.markdown: [4282, 4286], { or }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4286, 4287], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [4287, 4293], {UPDATE}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4293, 4294], {`}
text.html.markdown meta.paragraph.markdown: [4294, 4318], { operation. This avoids
}
text.html.markdown meta.paragraph.markdown: [4318, 4393], {storing an invalid object in the database. You can choose to have specific
}
text.html.markdown meta.paragraph.markdown: [4393, 4455], {validations run when an object is created, saved, or updated.
}
text.html.markdown meta.paragraph.markdown: [4455, 4456], {
}
text.html.markdown meta.paragraph.markdown: [4456, 4535], {CAUTION: There are many ways to change the state of an object in the database.
}
text.html.markdown meta.paragraph.markdown: [4535, 4614], {Some methods will trigger validations, but some will not. This means that it's
}
text.html.markdown meta.paragraph.markdown: [4614, 4691], {possible to save an object in the database in an invalid state if you aren't
}
text.html.markdown meta.paragraph.markdown: [4691, 4700], {careful.
}
text.html.markdown meta.paragraph.markdown: [4700, 4701], {
}
text.html.markdown meta.paragraph.markdown: [4701, 4776], {The following methods trigger validations, and will save the object to the
}
text.html.markdown meta.paragraph.markdown: [4776, 4814], {database only if the object is valid:
}
text.html.markdown meta.paragraph.markdown: [4814, 4815], {
}
text.html.markdown markup.list.unnumbered.markdown punctuation.definition.list_item.markdown: [4815, 4816], {*}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [4816, 4817], { }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4817, 4818], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [4818, 4824], {create}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4824, 4825], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [4825, 4826], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [4826, 4828], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4828, 4829], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [4829, 4836], {create!}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4836, 4837], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [4837, 4838], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [4838, 4840], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4840, 4841], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [4841, 4845], {save}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4845, 4846], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [4846, 4847], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [4847, 4849], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4849, 4850], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [4850, 4855], {save!}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4855, 4856], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [4856, 4857], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [4857, 4859], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4859, 4860], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [4860, 4866], {update}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4866, 4867], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [4867, 4868], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [4868, 4870], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4870, 4871], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [4871, 4878], {update!}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4878, 4879], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [4879, 4880], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [4880, 4881], {
}
text.html.markdown: [4881, 4959], {The bang versions (e.g. `save!`) raise an exception if the record is invalid.
}
text.html.markdown meta.paragraph.markdown: [4959, 4988], {The non-bang versions don't: }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4988, 4989], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [4989, 4993], {save}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4993, 4994], {`}
text.html.markdown meta.paragraph.markdown: [4994, 4999], { and }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [4999, 5000], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [5000, 5006], {update}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5006, 5007], {`}
text.html.markdown meta.paragraph.markdown: [5007, 5015], { return }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5015, 5016], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [5016, 5021], {false}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5021, 5022], {`}
text.html.markdown meta.paragraph.markdown: [5022, 5024], {,
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5024, 5025], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [5025, 5031], {create}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5031, 5032], {`}
text.html.markdown meta.paragraph.markdown: [5032, 5037], { and }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5037, 5038], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [5038, 5044], {update}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5044, 5045], {`}
text.html.markdown meta.paragraph.markdown: [5045, 5071], { just return the objects.
}
text.html.markdown meta.paragraph.markdown: [5071, 5072], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [5072, 5075], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [5075, 5076], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown: [5076, 5096], {Skipping Validations}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [5096, 5097], {
}
text.html.markdown: [5097, 5098], {
}
text.html.markdown meta.paragraph.markdown: [5098, 5170], {The following methods skip validations, and will save the object to the
}
text.html.markdown meta.paragraph.markdown: [5170, 5241], {database regardless of its validity. They should be used with caution.
}
text.html.markdown meta.paragraph.markdown: [5241, 5242], {
}
text.html.markdown markup.list.unnumbered.markdown punctuation.definition.list_item.markdown: [5242, 5243], {*}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5243, 5244], { }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5244, 5245], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [5245, 5255], {decrement!}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5255, 5256], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5256, 5257], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5257, 5259], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5259, 5260], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [5260, 5277], {decrement_counter}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5277, 5278], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5278, 5279], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5279, 5281], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5281, 5282], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [5282, 5292], {increment!}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5292, 5293], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5293, 5294], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5294, 5296], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5296, 5297], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [5297, 5314], {increment_counter}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5314, 5315], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5315, 5316], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5316, 5318], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5318, 5319], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [5319, 5326], {toggle!}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5326, 5327], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5327, 5328], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5328, 5330], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5330, 5331], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [5331, 5336], {touch}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5336, 5337], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5337, 5338], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5338, 5340], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5340, 5341], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [5341, 5351], {update_all}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5351, 5352], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5352, 5353], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5353, 5355], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5355, 5356], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [5356, 5372], {update_attribute}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5372, 5373], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5373, 5374], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5374, 5376], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5376, 5377], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [5377, 5390], {update_column}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5390, 5391], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5391, 5392], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5392, 5394], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5394, 5395], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [5395, 5409], {update_columns}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5409, 5410], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5410, 5411], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5411, 5413], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5413, 5414], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [5414, 5429], {update_counters}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5429, 5430], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5430, 5431], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5431, 5432], {
}
text.html.markdown: [5432, 5511], {Note that `save` also has the ability to skip validations if passed `validate:
}
text.html.markdown meta.paragraph.markdown: [5511, 5575], {false` as argument. This technique should be used with caution.
}
text.html.markdown meta.paragraph.markdown: [5575, 5576], {
}
text.html.markdown markup.list.unnumbered.markdown punctuation.definition.list_item.markdown: [5576, 5577], {*}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5577, 5578], { }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5578, 5579], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [5579, 5600], {save(validate: false)}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5600, 5601], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5601, 5602], {
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [5602, 5603], {
}
text.html.markdown: [5603, 5631], {### `valid?` and `invalid?`
}
text.html.markdown: [5631, 5632], {
}
text.html.markdown meta.paragraph.markdown: [5632, 5692], {To verify whether or not an object is valid, Rails uses the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5692, 5693], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [5693, 5699], {valid?}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5699, 5700], {`}
text.html.markdown meta.paragraph.markdown: [5700, 5709], { method.
}
text.html.markdown meta.paragraph.markdown: [5709, 5751], {You can also use this method on your own. }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5751, 5752], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [5752, 5758], {valid?}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [5758, 5759], {`}
text.html.markdown meta.paragraph.markdown: [5759, 5786], { triggers your validations
}
text.html.markdown meta.paragraph.markdown: [5786, 5863], {and returns true if no errors were found in the object, and false otherwise.
}
text.html.markdown meta.paragraph.markdown: [5863, 5881], {As you saw above:
}
text.html.markdown meta.paragraph.markdown: [5881, 5882], {
}
text.html.markdown meta.paragraph.markdown: [5882, 5890], {```ruby
}
text.html.markdown meta.paragraph.markdown: [5890, 5903], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [5903, 5904], {<}
text.html.markdown meta.paragraph.markdown: [5904, 5924], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [5924, 5958], {  validates :name, presence: true
}
text.html.markdown meta.paragraph.markdown: [5958, 5962], {end
}
text.html.markdown meta.paragraph.markdown: [5962, 5963], {
}
text.html.markdown meta.paragraph.markdown: [5963, 6012], {Person.create(name: "John Doe").valid? # => true
}
text.html.markdown meta.paragraph.markdown: [6012, 6055], {Person.create(name: nil).valid? # => false
}
text.html.markdown meta.paragraph.markdown: [6055, 6059], {```
}
text.html.markdown meta.paragraph.markdown: [6059, 6060], {
}
text.html.markdown meta.paragraph.markdown: [6060, 6140], {After Active Record has performed validations, any errors found can be accessed
}
text.html.markdown meta.paragraph.markdown: [6140, 6152], {through the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [6152, 6153], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [6153, 6159], {errors}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [6159, 6160], {`}
text.html.markdown meta.paragraph.markdown: [6160, 6219], { instance method, which returns a collection of errors. By
}
text.html.markdown meta.paragraph.markdown: [6219, 6292], {definition, an object is valid if this collection is empty after running
}
text.html.markdown meta.paragraph.markdown: [6292, 6305], {validations.
}
text.html.markdown meta.paragraph.markdown: [6305, 6306], {
}
text.html.markdown meta.paragraph.markdown: [6306, 6344], {Note that an object instantiated with }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [6344, 6345], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [6345, 6348], {new}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [6348, 6349], {`}
text.html.markdown meta.paragraph.markdown: [6349, 6386], { will not report errors even if it's
}
text.html.markdown meta.paragraph.markdown: [6386, 6450], {technically invalid, because validations are not run when using }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [6450, 6451], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [6451, 6454], {new}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [6454, 6455], {`}
text.html.markdown meta.paragraph.markdown: [6455, 6457], {.
}
text.html.markdown meta.paragraph.markdown: [6457, 6458], {
}
text.html.markdown meta.paragraph.markdown: [6458, 6466], {```ruby
}
text.html.markdown meta.paragraph.markdown: [6466, 6479], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [6479, 6480], {<}
text.html.markdown meta.paragraph.markdown: [6480, 6500], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [6500, 6534], {  validates :name, presence: true
}
text.html.markdown meta.paragraph.markdown: [6534, 6538], {end
}
text.html.markdown meta.paragraph.markdown: [6538, 6539], {
}
text.html.markdown meta.block-level.markdown markup.quote.markdown punctuation.definition.blockquote.markdown: [6539, 6540], {>}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown punctuation.definition.blockquote.markdown: [6540, 6541], {>}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6541, 6542], { }
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6542, 6557], {p = Person.new
}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6557, 6590], {#=> #<Person id: nil, name: nil>
}
text.html.markdown meta.block-level.markdown: [6590, 6602], {>> p.errors
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [6602, 6603], {#}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown: [6603, 6608], {=> {}}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [6608, 6609], {
}
text.html.markdown: [6609, 6610], {
}
text.html.markdown meta.block-level.markdown markup.quote.markdown punctuation.definition.blockquote.markdown: [6610, 6611], {>}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown punctuation.definition.blockquote.markdown: [6611, 6612], {>}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6612, 6613], { }
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6613, 6622], {p.valid?
}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6622, 6632], {#=> false
}
text.html.markdown meta.block-level.markdown: [6632, 6644], {>> p.errors
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [6644, 6645], {#}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown: [6645, 6673], {=> {name:["can't be blank"]}}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [6673, 6674], {
}
text.html.markdown: [6674, 6675], {
}
text.html.markdown meta.block-level.markdown markup.quote.markdown punctuation.definition.blockquote.markdown: [6675, 6676], {>}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown punctuation.definition.blockquote.markdown: [6676, 6677], {>}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6677, 6678], { }
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6678, 6696], {p = Person.create
}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6696, 6729], {#=> #<Person id: nil, name: nil>
}
text.html.markdown meta.block-level.markdown: [6729, 6741], {>> p.errors
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [6741, 6742], {#}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown: [6742, 6770], {=> {name:["can't be blank"]}}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [6770, 6771], {
}
text.html.markdown: [6771, 6772], {
}
text.html.markdown meta.block-level.markdown markup.quote.markdown punctuation.definition.blockquote.markdown: [6772, 6773], {>}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown punctuation.definition.blockquote.markdown: [6773, 6774], {>}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6774, 6775], { }
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6775, 6782], {p.save
}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6782, 6792], {#=> false
}
text.html.markdown meta.block-level.markdown: [6792, 6793], {
}
text.html.markdown meta.block-level.markdown markup.quote.markdown punctuation.definition.blockquote.markdown: [6793, 6794], {>}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown punctuation.definition.blockquote.markdown: [6794, 6795], {>}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6795, 6796], { }
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6796, 6804], {p.save!
}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6804, 6876], {#=> ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
}
text.html.markdown meta.block-level.markdown: [6876, 6877], {
}
text.html.markdown meta.block-level.markdown markup.quote.markdown punctuation.definition.blockquote.markdown: [6877, 6878], {>}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown punctuation.definition.blockquote.markdown: [6878, 6879], {>}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6879, 6880], { }
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6880, 6895], {Person.create!
}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6895, 6967], {#=> ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [6967, 6971], {```
}
text.html.markdown meta.block-level.markdown: [6971, 6972], {
}
text.html.markdown: [6972, 7048], {`invalid?` is simply the inverse of `valid?`. It triggers your validations,
}
text.html.markdown meta.paragraph.markdown: [7048, 7124], {returning true if any errors were found in the object, and false otherwise.
}
text.html.markdown meta.paragraph.markdown: [7124, 7125], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [7125, 7128], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [7128, 7129], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [7129, 7130], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [7130, 7138], {errors[]}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [7138, 7139], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [7139, 7140], {
}
text.html.markdown: [7140, 7141], {
}
text.html.markdown meta.paragraph.markdown: [7141, 7220], {To verify whether or not a particular attribute of an object is valid, you can
}
text.html.markdown meta.paragraph.markdown: [7220, 7224], {use }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [7224, 7225], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [7225, 7243], {errors[:attribute]}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [7243, 7244], {`}
text.html.markdown meta.paragraph.markdown: [7244, 7288], {. It returns an array of all the errors for
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [7288, 7289], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [7289, 7299], {:attribute}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [7299, 7300], {`}
text.html.markdown meta.paragraph.markdown: [7300, 7368], {. If there are no errors on the specified attribute, an empty array
}
text.html.markdown meta.paragraph.markdown: [7368, 7381], {is returned.
}
text.html.markdown meta.paragraph.markdown: [7381, 7382], {
}
text.html.markdown meta.paragraph.markdown: [7382, 7409], {This method is only useful }
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [7409, 7410], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [7410, 7415], {after}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [7415, 7416], {_}
text.html.markdown meta.paragraph.markdown: [7416, 7460], { validations have been run, because it only
}
text.html.markdown meta.paragraph.markdown: [7460, 7537], {inspects the errors collection and does not trigger validations itself. It's
}
text.html.markdown meta.paragraph.markdown: [7537, 7556], {different from the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [7556, 7557], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [7557, 7584], {ActiveRecord::Base#invalid?}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [7584, 7585], {`}
text.html.markdown meta.paragraph.markdown: [7585, 7617], { method explained above because
}
text.html.markdown meta.paragraph.markdown: [7617, 7696], {it doesn't verify the validity of the object as a whole. It only checks to see
}
text.html.markdown meta.paragraph.markdown: [7696, 7769], {whether there are errors found on an individual attribute of the object.
}
text.html.markdown meta.paragraph.markdown: [7769, 7770], {
}
text.html.markdown meta.paragraph.markdown: [7770, 7778], {```ruby
}
text.html.markdown meta.paragraph.markdown: [7778, 7791], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [7791, 7792], {<}
text.html.markdown meta.paragraph.markdown: [7792, 7812], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [7812, 7846], {  validates :name, presence: true
}
text.html.markdown meta.paragraph.markdown: [7846, 7850], {end
}
text.html.markdown meta.paragraph.markdown: [7850, 7851], {
}
text.html.markdown meta.block-level.markdown markup.quote.markdown punctuation.definition.blockquote.markdown: [7851, 7852], {>}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown punctuation.definition.blockquote.markdown: [7852, 7853], {>}
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [7853, 7854], { }
text.html.markdown meta.block-level.markdown markup.quote.markdown markup.quote.markdown: [7854, 7895], {Person.new.errors[:name].any? # => false
}
text.html.markdown meta.block-level.markdown: [7895, 7941], {>> Person.create.errors[:name].any? # => true
}
text.html.markdown: [7941, 7945], {```
}
text.html.markdown: [7945, 7946], {
}
text.html.markdown meta.paragraph.markdown: [7946, 8025], {We'll cover validation errors in greater depth in the [Working with Validation
}
text.html.markdown meta.paragraph.markdown: [8025, 8102], {Errors](#working-with-validation-errors) section. For now, let's turn to the
}
text.html.markdown meta.paragraph.markdown: [8102, 8162], {built-in validation helpers that Rails provides by default.
}
text.html.markdown meta.paragraph.markdown: [8162, 8163], {
}
text.html.markdown meta.paragraph.markdown: [8163, 8182], {Validation Helpers
}
text.html.markdown meta.paragraph.markdown markup.heading.2.markdown punctuation.definition.heading.markdown: [8182, 8200], {------------------}
text.html.markdown meta.paragraph.markdown: [8200, 8201], {
}
text.html.markdown: [8201, 8202], {
}
text.html.markdown meta.paragraph.markdown: [8202, 8276], {Active Record offers many pre-defined validation helpers that you can use
}
text.html.markdown meta.paragraph.markdown: [8276, 8356], {directly inside your class definitions. These helpers provide common validation
}
text.html.markdown meta.paragraph.markdown: [8356, 8436], {rules. Every time a validation fails, an error message is added to the object's
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [8436, 8437], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [8437, 8443], {errors}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [8443, 8444], {`}
text.html.markdown meta.paragraph.markdown: [8444, 8513], { collection, and this message is associated with the attribute being
}
text.html.markdown meta.paragraph.markdown: [8513, 8524], {validated.
}
text.html.markdown meta.paragraph.markdown: [8524, 8525], {
}
text.html.markdown meta.paragraph.markdown: [8525, 8602], {Each helper accepts an arbitrary number of attribute names, so with a single
}
text.html.markdown meta.paragraph.markdown: [8602, 8678], {line of code you can add the same kind of validation to several attributes.
}
text.html.markdown meta.paragraph.markdown: [8678, 8679], {
}
text.html.markdown meta.paragraph.markdown: [8679, 8702], {All of them accept the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [8702, 8703], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [8703, 8706], {:on}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [8706, 8707], {`}
text.html.markdown meta.paragraph.markdown: [8707, 8712], { and }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [8712, 8713], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [8713, 8721], {:message}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [8721, 8722], {`}
text.html.markdown meta.paragraph.markdown: [8722, 8754], { options, which define when the
}
text.html.markdown meta.paragraph.markdown: [8754, 8819], {validation should be run and what message should be added to the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [8819, 8820], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [8820, 8826], {errors}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [8826, 8827], {`}
text.html.markdown meta.paragraph.markdown: [8827, 8828], {
}
text.html.markdown meta.paragraph.markdown: [8828, 8870], {collection if it fails, respectively. The }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [8870, 8871], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [8871, 8874], {:on}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [8874, 8875], {`}
text.html.markdown meta.paragraph.markdown: [8875, 8907], { option takes one of the values
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [8907, 8908], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [8908, 8913], {:save}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [8913, 8914], {`}
text.html.markdown meta.paragraph.markdown: [8914, 8930], { (the default), }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [8930, 8931], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [8931, 8938], {:create}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [8938, 8939], {`}
text.html.markdown meta.paragraph.markdown: [8939, 8944], {  or }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [8944, 8945], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [8945, 8952], {:update}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [8952, 8953], {`}
text.html.markdown meta.paragraph.markdown: [8953, 8980], {. There is a default error
}
text.html.markdown meta.paragraph.markdown: [8980, 9057], {message for each one of the validation helpers. These messages are used when
}
text.html.markdown meta.paragraph.markdown: [9057, 9061], {the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [9061, 9062], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [9062, 9070], {:message}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [9070, 9071], {`}
text.html.markdown meta.paragraph.markdown: [9071, 9133], { option isn't specified. Let's take a look at each one of the
}
text.html.markdown meta.paragraph.markdown: [9133, 9152], {available helpers.
}
text.html.markdown meta.paragraph.markdown: [9152, 9153], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [9153, 9156], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [9156, 9157], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [9157, 9158], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [9158, 9168], {acceptance}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [9168, 9169], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [9169, 9170], {
}
text.html.markdown: [9170, 9171], {
}
text.html.markdown meta.paragraph.markdown: [9171, 9250], {This method validates that a checkbox on the user interface was checked when a
}
text.html.markdown meta.paragraph.markdown: [9250, 9330], {form was submitted. This is typically used when the user needs to agree to your
}
text.html.markdown meta.paragraph.markdown: [9330, 9404], {application's terms of service, confirm reading some text, or any similar
}
text.html.markdown meta.paragraph.markdown: [9404, 9475], {concept. This validation is very specific to web applications and this
}
text.html.markdown meta.paragraph.markdown: [9475, 9551], {'acceptance' does not need to be recorded anywhere in your database (if you
}
text.html.markdown meta.paragraph.markdown: [9551, 9628], {don't have a field for it, the helper will just create a virtual attribute).
}
text.html.markdown meta.paragraph.markdown: [9628, 9629], {
}
text.html.markdown meta.paragraph.markdown: [9629, 9637], {```ruby
}
text.html.markdown meta.paragraph.markdown: [9637, 9650], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [9650, 9651], {<}
text.html.markdown meta.paragraph.markdown: [9651, 9671], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [9671, 9689], {  validates :terms}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [9689, 9690], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [9690, 9692], {of}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [9692, 9693], {_}
text.html.markdown meta.paragraph.markdown: [9693, 9719], {service, acceptance: true
}
text.html.markdown meta.paragraph.markdown: [9719, 9723], {end
}
text.html.markdown meta.paragraph.markdown: [9723, 9727], {```
}
text.html.markdown meta.paragraph.markdown: [9727, 9728], {
}
text.html.markdown meta.paragraph.markdown: [9728, 9773], {The default error message for this helper is }
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [9773, 9774], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [9774, 9792], {"must be accepted"}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [9792, 9793], {_}
text.html.markdown meta.paragraph.markdown: [9793, 9795], {.
}
text.html.markdown meta.paragraph.markdown: [9795, 9796], {
}
text.html.markdown meta.paragraph.markdown: [9796, 9814], {It can receive an }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [9814, 9815], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [9815, 9822], {:accept}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [9822, 9823], {`}
text.html.markdown meta.paragraph.markdown: [9823, 9872], { option, which determines the value that will be
}
text.html.markdown meta.paragraph.markdown: [9872, 9941], {considered acceptance. It defaults to "1" and can be easily changed.
}
text.html.markdown meta.paragraph.markdown: [9941, 9942], {
}
text.html.markdown meta.paragraph.markdown: [9942, 9950], {```ruby
}
text.html.markdown meta.paragraph.markdown: [9950, 9963], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [9963, 9964], {<}
text.html.markdown meta.paragraph.markdown: [9964, 9984], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [9984, 10002], {  validates :terms}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [10002, 10003], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [10003, 10005], {of}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [10005, 10006], {_}
text.html.markdown meta.paragraph.markdown: [10006, 10045], {service, acceptance: { accept: 'yes' }
}
text.html.markdown meta.paragraph.markdown: [10045, 10049], {end
}
text.html.markdown meta.paragraph.markdown: [10049, 10053], {```
}
text.html.markdown meta.paragraph.markdown: [10053, 10054], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [10054, 10057], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [10057, 10058], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [10058, 10059], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [10059, 10079], {validates_associated}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [10079, 10080], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [10080, 10081], {
}
text.html.markdown: [10081, 10082], {
}
text.html.markdown meta.paragraph.markdown: [10082, 10160], {You should use this helper when your model has associations with other models
}
text.html.markdown meta.paragraph.markdown: [10160, 10230], {and they also need to be validated. When you try to save your object, }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [10230, 10231], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [10231, 10237], {valid?}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [10237, 10238], {`}
text.html.markdown meta.paragraph.markdown: [10238, 10239], {
}
text.html.markdown meta.paragraph.markdown: [10239, 10295], {will be called upon each one of the associated objects.
}
text.html.markdown meta.paragraph.markdown: [10295, 10296], {
}
text.html.markdown meta.paragraph.markdown: [10296, 10304], {```ruby
}
text.html.markdown meta.paragraph.markdown: [10304, 10318], {class Library }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [10318, 10319], {<}
text.html.markdown meta.paragraph.markdown: [10319, 10339], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [10339, 10357], {  has_many :books
}
text.html.markdown meta.paragraph.markdown: [10357, 10387], {  validates_associated :books
}
text.html.markdown meta.paragraph.markdown: [10387, 10391], {end
}
text.html.markdown meta.paragraph.markdown: [10391, 10395], {```
}
text.html.markdown meta.paragraph.markdown: [10395, 10396], {
}
text.html.markdown meta.paragraph.markdown: [10396, 10457], {This validation will work with all of the association types.
}
text.html.markdown meta.paragraph.markdown: [10457, 10458], {
}
text.html.markdown meta.paragraph.markdown: [10458, 10477], {CAUTION: Don't use }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [10477, 10478], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [10478, 10498], {validates_associated}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [10498, 10499], {`}
text.html.markdown meta.paragraph.markdown: [10499, 10535], { on both ends of your associations.
}
text.html.markdown meta.paragraph.markdown: [10535, 10583], {They would call each other in an infinite loop.
}
text.html.markdown meta.paragraph.markdown: [10583, 10584], {
}
text.html.markdown meta.paragraph.markdown: [10584, 10614], {The default error message for }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [10614, 10615], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [10615, 10635], {validates_associated}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [10635, 10636], {`}
text.html.markdown meta.paragraph.markdown: [10636, 10640], { is }
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [10640, 10641], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [10641, 10653], {"is invalid"}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [10653, 10654], {_}
text.html.markdown meta.paragraph.markdown: [10654, 10661], {. Note
}
text.html.markdown meta.paragraph.markdown: [10661, 10710], {that each associated object will contain its own }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [10710, 10711], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [10711, 10717], {errors}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [10717, 10718], {`}
text.html.markdown meta.paragraph.markdown: [10718, 10741], { collection; errors do
}
text.html.markdown meta.paragraph.markdown: [10741, 10777], {not bubble up to the calling model.
}
text.html.markdown meta.paragraph.markdown: [10777, 10778], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [10778, 10781], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [10781, 10782], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [10782, 10783], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [10783, 10795], {confirmation}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [10795, 10796], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [10796, 10797], {
}
text.html.markdown: [10797, 10798], {
}
text.html.markdown meta.paragraph.markdown: [10798, 10875], {You should use this helper when you have two text fields that should receive
}
text.html.markdown meta.paragraph.markdown: [10875, 10955], {exactly the same content. For example, you may want to confirm an email address
}
text.html.markdown meta.paragraph.markdown: [10955, 11032], {or a password. This validation creates a virtual attribute whose name is the
}
text.html.markdown meta.paragraph.markdown: [11032, 11106], {name of the field that has to be confirmed with "_confirmation" appended.
}
text.html.markdown meta.paragraph.markdown: [11106, 11107], {
}
text.html.markdown meta.paragraph.markdown: [11107, 11115], {```ruby
}
text.html.markdown meta.paragraph.markdown: [11115, 11128], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [11128, 11129], {<}
text.html.markdown meta.paragraph.markdown: [11129, 11149], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [11149, 11188], {  validates :email, confirmation: true
}
text.html.markdown meta.paragraph.markdown: [11188, 11192], {end
}
text.html.markdown meta.paragraph.markdown: [11192, 11196], {```
}
text.html.markdown meta.paragraph.markdown: [11196, 11197], {
}
text.html.markdown meta.paragraph.markdown: [11197, 11248], {In your view template you could use something like
}
text.html.markdown meta.paragraph.markdown: [11248, 11249], {
}
text.html.markdown meta.paragraph.markdown: [11249, 11256], {```erb
}
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [11256, 11257], {<}
text.html.markdown meta.paragraph.markdown: [11257, 11290], {%= text_field :person, :email %>
}
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [11290, 11291], {<}
text.html.markdown meta.paragraph.markdown: [11291, 11298], {%= text}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [11298, 11299], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [11299, 11320], {field :person, :email}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [11320, 11321], {_}
text.html.markdown meta.paragraph.markdown: [11321, 11337], {confirmation %>
}
text.html.markdown meta.paragraph.markdown: [11337, 11341], {```
}
text.html.markdown meta.paragraph.markdown: [11341, 11342], {
}
text.html.markdown meta.paragraph.markdown: [11342, 11374], {This check is performed only if }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [11374, 11375], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [11375, 11393], {email_confirmation}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [11393, 11394], {`}
text.html.markdown meta.paragraph.markdown: [11394, 11402], { is not }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [11402, 11403], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [11403, 11406], {nil}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [11406, 11407], {`}
text.html.markdown meta.paragraph.markdown: [11407, 11420], {. To require
}
text.html.markdown meta.paragraph.markdown: [11420, 11499], {confirmation, make sure to add a presence check for the confirmation attribute
}
text.html.markdown meta.paragraph.markdown: [11499, 11521], {(we'll take a look at }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [11521, 11522], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [11522, 11530], {presence}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [11530, 11531], {`}
text.html.markdown meta.paragraph.markdown: [11531, 11554], { later on this guide):
}
text.html.markdown meta.paragraph.markdown: [11554, 11555], {
}
text.html.markdown meta.paragraph.markdown: [11555, 11563], {```ruby
}
text.html.markdown meta.paragraph.markdown: [11563, 11576], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [11576, 11577], {<}
text.html.markdown meta.paragraph.markdown: [11577, 11597], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [11597, 11636], {  validates :email, confirmation: true
}
text.html.markdown meta.paragraph.markdown: [11636, 11684], {  validates :email_confirmation, presence: true
}
text.html.markdown meta.paragraph.markdown: [11684, 11688], {end
}
text.html.markdown meta.paragraph.markdown: [11688, 11692], {```
}
text.html.markdown meta.paragraph.markdown: [11692, 11693], {
}
text.html.markdown meta.paragraph.markdown: [11693, 11738], {The default error message for this helper is }
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [11738, 11739], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [11739, 11767], {"doesn't match confirmation"}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [11767, 11768], {_}
text.html.markdown meta.paragraph.markdown: [11768, 11770], {.
}
text.html.markdown meta.paragraph.markdown: [11770, 11771], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [11771, 11774], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [11774, 11775], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [11775, 11776], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [11776, 11785], {exclusion}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [11785, 11786], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [11786, 11787], {
}
text.html.markdown: [11787, 11788], {
}
text.html.markdown meta.paragraph.markdown: [11788, 11866], {This helper validates that the attributes' values are not included in a given
}
text.html.markdown meta.paragraph.markdown: [11866, 11919], {set. In fact, this set can be any enumerable object.
}
text.html.markdown meta.paragraph.markdown: [11919, 11920], {
}
text.html.markdown meta.paragraph.markdown: [11920, 11928], {```ruby
}
text.html.markdown meta.paragraph.markdown: [11928, 11942], {class Account }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [11942, 11943], {<}
text.html.markdown meta.paragraph.markdown: [11943, 11963], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [11963, 12022], {  validates :subdomain, exclusion: { in: %w(www us ca jp),
}
text.html.markdown meta.paragraph.markdown: [12022, 12071], {    message: "Subdomain %{value} is reserved." }
}
text.html.markdown meta.paragraph.markdown: [12071, 12075], {end
}
text.html.markdown meta.paragraph.markdown: [12075, 12079], {```
}
text.html.markdown meta.paragraph.markdown: [12079, 12080], {
}
text.html.markdown meta.paragraph.markdown: [12080, 12084], {The }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [12084, 12085], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [12085, 12094], {exclusion}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [12094, 12095], {`}
text.html.markdown meta.paragraph.markdown: [12095, 12117], { helper has an option }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [12117, 12118], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [12118, 12121], {:in}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [12121, 12122], {`}
text.html.markdown meta.paragraph.markdown: [12122, 12160], { that receives the set of values that
}
text.html.markdown meta.paragraph.markdown: [12160, 12215], {will not be accepted for the validated attributes. The }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [12215, 12216], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [12216, 12219], {:in}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [12219, 12220], {`}
text.html.markdown meta.paragraph.markdown: [12220, 12235], { option has an
}
text.html.markdown meta.paragraph.markdown: [12235, 12248], {alias called }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [12248, 12249], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [12249, 12256], {:within}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [12256, 12257], {`}
text.html.markdown meta.paragraph.markdown: [12257, 12315], { that you can use for the same purpose, if you'd like to.
}
text.html.markdown meta.paragraph.markdown: [12315, 12337], {This example uses the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [12337, 12338], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [12338, 12346], {:message}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [12346, 12347], {`}
text.html.markdown meta.paragraph.markdown: [12347, 12387], { option to show how you can include the
}
text.html.markdown meta.paragraph.markdown: [12387, 12406], {attribute's value.
}
text.html.markdown meta.paragraph.markdown: [12406, 12407], {
}
text.html.markdown meta.paragraph.markdown: [12407, 12436], {The default error message is }
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [12436, 12437], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [12437, 12450], {"is reserved"}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [12450, 12451], {_}
text.html.markdown meta.paragraph.markdown: [12451, 12453], {.
}
text.html.markdown meta.paragraph.markdown: [12453, 12454], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [12454, 12457], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [12457, 12458], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [12458, 12459], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [12459, 12465], {format}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [12465, 12466], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [12466, 12467], {
}
text.html.markdown: [12467, 12468], {
}
text.html.markdown meta.paragraph.markdown: [12468, 12545], {This helper validates the attributes' values by testing whether they match a
}
text.html.markdown meta.paragraph.markdown: [12545, 12600], {given regular expression, which is specified using the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [12600, 12601], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [12601, 12606], {:with}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [12606, 12607], {`}
text.html.markdown meta.paragraph.markdown: [12607, 12616], { option.
}
text.html.markdown meta.paragraph.markdown: [12616, 12617], {
}
text.html.markdown meta.paragraph.markdown: [12617, 12625], {```ruby
}
text.html.markdown meta.paragraph.markdown: [12625, 12639], {class Product }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [12639, 12640], {<}
text.html.markdown meta.paragraph.markdown: [12640, 12660], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [12660, 12719], {  validates :legacy_code, format: { with: /\A[a-zA-Z]+\z/,
}
text.html.markdown meta.paragraph.markdown: [12719, 12757], {    message: "Only letters allowed" }
}
text.html.markdown meta.paragraph.markdown: [12757, 12761], {end
}
text.html.markdown meta.paragraph.markdown: [12761, 12765], {```
}
text.html.markdown meta.paragraph.markdown: [12765, 12766], {
}
text.html.markdown meta.paragraph.markdown: [12766, 12795], {The default error message is }
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [12795, 12796], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [12796, 12808], {"is invalid"}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [12808, 12809], {_}
text.html.markdown meta.paragraph.markdown: [12809, 12811], {.
}
text.html.markdown meta.paragraph.markdown: [12811, 12812], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [12812, 12815], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [12815, 12816], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [12816, 12817], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [12817, 12826], {inclusion}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [12826, 12827], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [12827, 12828], {
}
text.html.markdown: [12828, 12829], {
}
text.html.markdown meta.paragraph.markdown: [12829, 12908], {This helper validates that the attributes' values are included in a given set.
}
text.html.markdown meta.paragraph.markdown: [12908, 12956], {In fact, this set can be any enumerable object.
}
text.html.markdown meta.paragraph.markdown: [12956, 12957], {
}
text.html.markdown meta.paragraph.markdown: [12957, 12965], {```ruby
}
text.html.markdown meta.paragraph.markdown: [12965, 12978], {class Coffee }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [12978, 12979], {<}
text.html.markdown meta.paragraph.markdown: [12979, 12999], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [12999, 13059], {  validates :size, inclusion: { in: %w(small medium large),
}
text.html.markdown meta.paragraph.markdown: [13059, 13105], {    message: "%{value} is not a valid size" }
}
text.html.markdown meta.paragraph.markdown: [13105, 13109], {end
}
text.html.markdown meta.paragraph.markdown: [13109, 13113], {```
}
text.html.markdown meta.paragraph.markdown: [13113, 13114], {
}
text.html.markdown meta.paragraph.markdown: [13114, 13118], {The }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [13118, 13119], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [13119, 13128], {inclusion}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [13128, 13129], {`}
text.html.markdown meta.paragraph.markdown: [13129, 13151], { helper has an option }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [13151, 13152], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [13152, 13155], {:in}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [13155, 13156], {`}
text.html.markdown meta.paragraph.markdown: [13156, 13194], { that receives the set of values that
}
text.html.markdown meta.paragraph.markdown: [13194, 13216], {will be accepted. The }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [13216, 13217], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [13217, 13220], {:in}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [13220, 13221], {`}
text.html.markdown meta.paragraph.markdown: [13221, 13249], { option has an alias called }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [13249, 13250], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [13250, 13257], {:within}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [13257, 13258], {`}
text.html.markdown meta.paragraph.markdown: [13258, 13272], { that you can
}
text.html.markdown meta.paragraph.markdown: [13272, 13346], {use for the same purpose, if you'd like to. The previous example uses the
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [13346, 13347], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [13347, 13355], {:message}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [13355, 13356], {`}
text.html.markdown meta.paragraph.markdown: [13356, 13415], { option to show how you can include the attribute's value.
}
text.html.markdown meta.paragraph.markdown: [13415, 13416], {
}
text.html.markdown meta.paragraph.markdown: [13416, 13461], {The default error message for this helper is }
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [13461, 13462], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [13462, 13491], {"is not included in the list"}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [13491, 13492], {_}
text.html.markdown meta.paragraph.markdown: [13492, 13494], {.
}
text.html.markdown meta.paragraph.markdown: [13494, 13495], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [13495, 13498], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [13498, 13499], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [13499, 13500], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [13500, 13506], {length}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [13506, 13507], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [13507, 13508], {
}
text.html.markdown: [13508, 13509], {
}
text.html.markdown meta.paragraph.markdown: [13509, 13583], {This helper validates the length of the attributes' values. It provides a
}
text.html.markdown meta.paragraph.markdown: [13583, 13660], {variety of options, so you can specify length constraints in different ways:
}
text.html.markdown meta.paragraph.markdown: [13660, 13661], {
}
text.html.markdown meta.paragraph.markdown: [13661, 13669], {```ruby
}
text.html.markdown meta.paragraph.markdown: [13669, 13682], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [13682, 13683], {<}
text.html.markdown meta.paragraph.markdown: [13683, 13703], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [13703, 13745], {  validates :name, length: { minimum: 2 }
}
text.html.markdown meta.paragraph.markdown: [13745, 13788], {  validates :bio, length: { maximum: 500 }
}
text.html.markdown meta.paragraph.markdown: [13788, 13833], {  validates :password, length: { in: 6..20 }
}
text.html.markdown meta.paragraph.markdown: [13833, 13885], {  validates :registration_number, length: { is: 6 }
}
text.html.markdown meta.paragraph.markdown: [13885, 13889], {end
}
text.html.markdown meta.paragraph.markdown: [13889, 13893], {```
}
text.html.markdown meta.paragraph.markdown: [13893, 13894], {
}
text.html.markdown meta.paragraph.markdown: [13894, 13938], {The possible length constraint options are:
}
text.html.markdown meta.paragraph.markdown: [13938, 13939], {
}
text.html.markdown markup.list.unnumbered.markdown punctuation.definition.list_item.markdown: [13939, 13940], {*}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [13940, 13941], { }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [13941, 13942], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [13942, 13950], {:minimum}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [13950, 13951], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [13951, 14012], { - The attribute cannot have less than the specified length.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [14012, 14014], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14014, 14015], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [14015, 14023], {:maximum}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14023, 14024], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [14024, 14085], { - The attribute cannot have more than the specified length.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [14085, 14087], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14087, 14088], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [14088, 14091], {:in}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14091, 14092], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [14092, 14097], { (or }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14097, 14098], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [14098, 14105], {:within}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14105, 14106], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [14106, 14159], {) - The attribute length must be included in a given
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [14159, 14214], {  interval. The value for this option must be a range.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [14214, 14216], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14216, 14217], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [14217, 14220], {:is}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14220, 14221], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [14221, 14279], { - The attribute length must be equal to the given value.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [14279, 14280], {
}
text.html.markdown: [14280, 14353], {The default error messages depend on the type of length validation being
}
text.html.markdown meta.paragraph.markdown: [14353, 14409], {performed. You can personalize these messages using the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14409, 14410], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [14410, 14423], {:wrong_length}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14423, 14424], {`}
text.html.markdown meta.paragraph.markdown: [14424, 14426], {,
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14426, 14427], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [14427, 14436], {:too_long}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14436, 14437], {`}
text.html.markdown meta.paragraph.markdown: [14437, 14443], {, and }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14443, 14444], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [14444, 14454], {:too_short}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14454, 14455], {`}
text.html.markdown meta.paragraph.markdown: [14455, 14468], { options and }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14468, 14469], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [14469, 14477], {%{count}}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14477, 14478], {`}
text.html.markdown meta.paragraph.markdown: [14478, 14504], { as a placeholder for the
}
text.html.markdown meta.paragraph.markdown: [14504, 14584], {number corresponding to the length constraint being used. You can still use the
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14584, 14585], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [14585, 14593], {:message}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14593, 14594], {`}
text.html.markdown meta.paragraph.markdown: [14594, 14631], { option to specify an error message.
}
text.html.markdown meta.paragraph.markdown: [14631, 14632], {
}
text.html.markdown meta.paragraph.markdown: [14632, 14640], {```ruby
}
text.html.markdown meta.paragraph.markdown: [14640, 14653], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [14653, 14654], {<}
text.html.markdown meta.paragraph.markdown: [14654, 14674], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [14674, 14717], {  validates :bio, length: { maximum: 1000,
}
text.html.markdown meta.paragraph.markdown: [14717, 14778], {    too_long: "%{count} characters is the maximum allowed" }
}
text.html.markdown meta.paragraph.markdown: [14778, 14782], {end
}
text.html.markdown meta.paragraph.markdown: [14782, 14786], {```
}
text.html.markdown meta.paragraph.markdown: [14786, 14787], {
}
text.html.markdown meta.paragraph.markdown: [14787, 14862], {This helper counts characters by default, but you can split the value in a
}
text.html.markdown meta.paragraph.markdown: [14862, 14886], {different way using the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14886, 14887], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [14887, 14897], {:tokenizer}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [14897, 14898], {`}
text.html.markdown meta.paragraph.markdown: [14898, 14907], { option:
}
text.html.markdown meta.paragraph.markdown: [14907, 14908], {
}
text.html.markdown meta.paragraph.markdown: [14908, 14916], {```ruby
}
text.html.markdown meta.paragraph.markdown: [14916, 14928], {class Essay }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [14928, 14929], {<}
text.html.markdown meta.paragraph.markdown: [14929, 14949], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [14949, 14981], {  validates :content, length: {
}
text.html.markdown meta.paragraph.markdown: [14981, 14999], {    minimum: 300,
}
text.html.markdown meta.paragraph.markdown: [14999, 15017], {    maximum: 400,
}
text.html.markdown meta.paragraph.markdown: [15017, 15066], {    tokenizer: lambda { |str| str.scan(/\w+/) },
}
text.html.markdown meta.paragraph.markdown: [15066, 15118], {    too_short: "must have at least %{count} words",
}
text.html.markdown meta.paragraph.markdown: [15118, 15167], {    too_long: "must have at most %{count} words"
}
text.html.markdown meta.paragraph.markdown: [15167, 15171], {  }
}
text.html.markdown meta.paragraph.markdown: [15171, 15175], {end
}
text.html.markdown meta.paragraph.markdown: [15175, 15179], {```
}
text.html.markdown meta.paragraph.markdown: [15179, 15180], {
}
text.html.markdown meta.paragraph.markdown: [15180, 15258], {Note that the default error messages are plural (e.g., "is too short (minimum
}
text.html.markdown meta.paragraph.markdown: [15258, 15307], {is %{count} characters)"). For this reason, when }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15307, 15308], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [15308, 15316], {:minimum}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15316, 15317], {`}
text.html.markdown meta.paragraph.markdown: [15317, 15334], { is 1 you should
}
text.html.markdown meta.paragraph.markdown: [15334, 15372], {provide a personalized message or use }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15372, 15373], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [15373, 15394], {validates_presence_of}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15394, 15395], {`}
text.html.markdown meta.paragraph.markdown: [15395, 15410], { instead. When
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15410, 15411], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [15411, 15414], {:in}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15414, 15415], {`}
text.html.markdown meta.paragraph.markdown: [15415, 15419], { or }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15419, 15420], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [15420, 15427], {:within}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15427, 15428], {`}
text.html.markdown meta.paragraph.markdown: [15428, 15482], { have a lower limit of 1, you should either provide a
}
text.html.markdown meta.paragraph.markdown: [15482, 15511], {personalized message or call }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15511, 15512], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [15512, 15520], {presence}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15520, 15521], {`}
text.html.markdown meta.paragraph.markdown: [15521, 15531], { prior to }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15531, 15532], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [15532, 15538], {length}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15538, 15539], {`}
text.html.markdown meta.paragraph.markdown: [15539, 15541], {.
}
text.html.markdown meta.paragraph.markdown: [15541, 15542], {
}
text.html.markdown meta.paragraph.markdown: [15542, 15546], {The }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15546, 15547], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [15547, 15551], {size}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15551, 15552], {`}
text.html.markdown meta.paragraph.markdown: [15552, 15576], { helper is an alias for }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15576, 15577], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [15577, 15583], {length}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15583, 15584], {`}
text.html.markdown meta.paragraph.markdown: [15584, 15586], {.
}
text.html.markdown meta.paragraph.markdown: [15586, 15587], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [15587, 15590], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [15590, 15591], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15591, 15592], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [15592, 15604], {numericality}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15604, 15605], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [15605, 15606], {
}
text.html.markdown: [15606, 15607], {
}
text.html.markdown meta.paragraph.markdown: [15607, 15679], {This helper validates that your attributes have only numeric values. By
}
text.html.markdown meta.paragraph.markdown: [15679, 15755], {default, it will match an optional sign followed by an integral or floating
}
text.html.markdown meta.paragraph.markdown: [15755, 15823], {point number. To specify that only integral numbers are allowed set
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15823, 15824], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [15824, 15837], {:only_integer}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15837, 15838], {`}
text.html.markdown meta.paragraph.markdown: [15838, 15848], { to true.
}
text.html.markdown meta.paragraph.markdown: [15848, 15849], {
}
text.html.markdown meta.paragraph.markdown: [15849, 15860], {If you set }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15860, 15861], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [15861, 15874], {:only_integer}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15874, 15875], {`}
text.html.markdown meta.paragraph.markdown: [15875, 15879], { to }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15879, 15880], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [15880, 15884], {true}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [15884, 15885], {`}
text.html.markdown meta.paragraph.markdown: [15885, 15908], {, then it will use the
}
text.html.markdown meta.paragraph.markdown: [15908, 15909], {
}
text.html.markdown meta.paragraph.markdown: [15909, 15917], {```ruby
}
text.html.markdown meta.paragraph.markdown: [15917, 15932], {/\A[+-]?\d+\Z/
}
text.html.markdown meta.paragraph.markdown: [15932, 15936], {```
}
text.html.markdown meta.paragraph.markdown: [15936, 15937], {
}
text.html.markdown meta.paragraph.markdown: [15937, 16017], {regular expression to validate the attribute's value. Otherwise, it will try to
}
text.html.markdown meta.paragraph.markdown: [16017, 16053], {convert the value to a number using }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [16053, 16054], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [16054, 16059], {Float}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [16059, 16060], {`}
text.html.markdown meta.paragraph.markdown: [16060, 16062], {.
}
text.html.markdown meta.paragraph.markdown: [16062, 16063], {
}
text.html.markdown meta.paragraph.markdown: [16063, 16137], {WARNING. Note that the regular expression above allows a trailing newline
}
text.html.markdown meta.paragraph.markdown: [16137, 16148], {character.
}
text.html.markdown meta.paragraph.markdown: [16148, 16149], {
}
text.html.markdown meta.paragraph.markdown: [16149, 16157], {```ruby
}
text.html.markdown meta.paragraph.markdown: [16157, 16170], {class Player }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [16170, 16171], {<}
text.html.markdown meta.paragraph.markdown: [16171, 16191], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [16191, 16231], {  validates :points, numericality: true
}
text.html.markdown meta.paragraph.markdown: [16231, 16249], {  validates :games}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [16249, 16250], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [16250, 16278], {played, numericality: { only}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [16278, 16279], {_}
text.html.markdown meta.paragraph.markdown: [16279, 16295], {integer: true }
}
text.html.markdown meta.paragraph.markdown: [16295, 16299], {end
}
text.html.markdown meta.paragraph.markdown: [16299, 16303], {```
}
text.html.markdown meta.paragraph.markdown: [16303, 16304], {
}
text.html.markdown meta.paragraph.markdown: [16304, 16312], {Besides }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [16312, 16313], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [16313, 16326], {:only_integer}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [16326, 16327], {`}
text.html.markdown meta.paragraph.markdown: [16327, 16383], {, this helper also accepts the following options to add
}
text.html.markdown meta.paragraph.markdown: [16383, 16417], {constraints to acceptable values:
}
text.html.markdown meta.paragraph.markdown: [16417, 16418], {
}
text.html.markdown markup.list.unnumbered.markdown punctuation.definition.list_item.markdown: [16418, 16419], {*}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [16419, 16420], { }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [16420, 16421], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [16421, 16434], {:greater_than}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [16434, 16435], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [16435, 16492], { - Specifies the value must be greater than the supplied
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [16492, 16569], {  value. The default error message for this option is _"must be greater than
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [16569, 16583], {  %{count}"_.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [16583, 16585], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [16585, 16586], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [16586, 16611], {:greater_than_or_equal_to}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [16611, 16612], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [16612, 16659], { - Specifies the value must be greater than or
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [16659, 16735], {  equal to the supplied value. The default error message for this option is
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [16735, 16737], {  }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.italic.markdown punctuation.definition.italic.markdown: [16737, 16738], {_}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.italic.markdown: [16738, 16781], {"must be greater than or equal to %{count}"}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.italic.markdown punctuation.definition.italic.markdown: [16781, 16782], {_}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [16782, 16784], {.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [16784, 16786], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [16786, 16787], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [16787, 16796], {:equal_to}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [16796, 16797], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [16797, 16861], { - Specifies the value must be equal to the supplied value. The
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [16861, 16904], {  default error message for this option is }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.italic.markdown punctuation.definition.italic.markdown: [16904, 16905], {_}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.italic.markdown: [16905, 16932], {"must be equal to %{count}"}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.italic.markdown punctuation.definition.italic.markdown: [16932, 16933], {_}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [16933, 16935], {.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [16935, 16937], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [16937, 16938], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [16938, 16948], {:less_than}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [16948, 16949], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [16949, 17014], { - Specifies the value must be less than the supplied value. The
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [17014, 17057], {  default error message for this option is }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.italic.markdown punctuation.definition.italic.markdown: [17057, 17058], {_}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.italic.markdown: [17058, 17086], {"must be less than %{count}"}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.italic.markdown punctuation.definition.italic.markdown: [17086, 17087], {_}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [17087, 17089], {.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [17089, 17091], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [17091, 17092], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [17092, 17114], {:less_than_or_equal_to}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [17114, 17115], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [17115, 17169], { - Specifies the value must be less than or equal the
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [17169, 17247], {  supplied value. The default error message for this option is _"must be less
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [17247, 17278], {  than or equal to %{count}"_.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [17278, 17280], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [17280, 17281], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [17281, 17285], {:odd}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [17285, 17286], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [17286, 17351], { - Specifies the value must be an odd number if set to true. The
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [17351, 17394], {  default error message for this option is }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.italic.markdown punctuation.definition.italic.markdown: [17394, 17395], {_}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.italic.markdown: [17395, 17408], {"must be odd"}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.italic.markdown punctuation.definition.italic.markdown: [17408, 17409], {_}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [17409, 17411], {.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [17411, 17413], {* }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [17413, 17414], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown: [17414, 17419], {:even}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [17419, 17420], {`}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [17420, 17486], { - Specifies the value must be an even number if set to true. The
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [17486, 17529], {  default error message for this option is }
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.italic.markdown punctuation.definition.italic.markdown: [17529, 17530], {_}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.italic.markdown: [17530, 17544], {"must be even"}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown markup.italic.markdown punctuation.definition.italic.markdown: [17544, 17545], {_}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [17545, 17547], {.
}
text.html.markdown markup.list.unnumbered.markdown meta.paragraph.list.markdown: [17547, 17548], {
}
text.html.markdown: [17548, 17598], {The default error message is _"is not a number"_.
}
text.html.markdown: [17598, 17599], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [17599, 17602], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [17602, 17603], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [17603, 17604], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [17604, 17612], {presence}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [17612, 17613], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [17613, 17614], {
}
text.html.markdown: [17614, 17615], {
}
text.html.markdown meta.paragraph.markdown: [17615, 17694], {This helper validates that the specified attributes are not empty. It uses the
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [17694, 17695], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [17695, 17701], {blank?}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [17701, 17702], {`}
text.html.markdown meta.paragraph.markdown: [17702, 17742], { method to check if the value is either }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [17742, 17743], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [17743, 17746], {nil}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [17746, 17747], {`}
text.html.markdown meta.paragraph.markdown: [17747, 17772], { or a blank string, that
}
text.html.markdown meta.paragraph.markdown: [17772, 17833], {is, a string that is either empty or consists of whitespace.
}
text.html.markdown meta.paragraph.markdown: [17833, 17834], {
}
text.html.markdown meta.paragraph.markdown: [17834, 17842], {```ruby
}
text.html.markdown meta.paragraph.markdown: [17842, 17855], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [17855, 17856], {<}
text.html.markdown meta.paragraph.markdown: [17856, 17876], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [17876, 17926], {  validates :name, :login, :email, presence: true
}
text.html.markdown meta.paragraph.markdown: [17926, 17930], {end
}
text.html.markdown meta.paragraph.markdown: [17930, 17934], {```
}
text.html.markdown meta.paragraph.markdown: [17934, 17935], {
}
text.html.markdown meta.paragraph.markdown: [17935, 18010], {If you want to be sure that an association is present, you'll need to test
}
text.html.markdown meta.paragraph.markdown: [18010, 18088], {whether the associated object itself is present, and not the foreign key used
}
text.html.markdown meta.paragraph.markdown: [18088, 18112], {to map the association.
}
text.html.markdown meta.paragraph.markdown: [18112, 18113], {
}
text.html.markdown meta.paragraph.markdown: [18113, 18121], {```ruby
}
text.html.markdown meta.paragraph.markdown: [18121, 18136], {class LineItem }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [18136, 18137], {<}
text.html.markdown meta.paragraph.markdown: [18137, 18157], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [18157, 18177], {  belongs_to :order
}
text.html.markdown meta.paragraph.markdown: [18177, 18212], {  validates :order, presence: true
}
text.html.markdown meta.paragraph.markdown: [18212, 18216], {end
}
text.html.markdown meta.paragraph.markdown: [18216, 18220], {```
}
text.html.markdown meta.paragraph.markdown: [18220, 18221], {
}
text.html.markdown meta.paragraph.markdown: [18221, 18298], {In order to validate associated records whose presence is required, you must
}
text.html.markdown meta.paragraph.markdown: [18298, 18310], {specify the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [18310, 18311], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [18311, 18322], {:inverse_of}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [18322, 18323], {`}
text.html.markdown meta.paragraph.markdown: [18323, 18352], { option for the association:
}
text.html.markdown meta.paragraph.markdown: [18352, 18353], {
}
text.html.markdown meta.paragraph.markdown: [18353, 18361], {```ruby
}
text.html.markdown meta.paragraph.markdown: [18361, 18373], {class Order }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [18373, 18374], {<}
text.html.markdown meta.paragraph.markdown: [18374, 18394], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [18394, 18399], {  has}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [18399, 18400], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [18400, 18410], {many :line}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [18410, 18411], {_}
text.html.markdown meta.paragraph.markdown: [18411, 18437], {items, inverse_of: :order
}
text.html.markdown meta.paragraph.markdown: [18437, 18441], {end
}
text.html.markdown meta.paragraph.markdown: [18441, 18445], {```
}
text.html.markdown meta.paragraph.markdown: [18445, 18446], {
}
text.html.markdown meta.paragraph.markdown: [18446, 18505], {If you validate the presence of an object associated via a }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [18505, 18506], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [18506, 18513], {has_one}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [18513, 18514], {`}
text.html.markdown meta.paragraph.markdown: [18514, 18518], { or
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [18518, 18519], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [18519, 18527], {has_many}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [18527, 18528], {`}
text.html.markdown meta.paragraph.markdown: [18528, 18584], { relationship, it will check that the object is neither }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [18584, 18585], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [18585, 18591], {blank?}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [18591, 18592], {`}
text.html.markdown meta.paragraph.markdown: [18592, 18597], { nor
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [18597, 18598], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [18598, 18621], {marked_for_destruction?}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [18621, 18622], {`}
text.html.markdown meta.paragraph.markdown: [18622, 18624], {.
}
text.html.markdown meta.paragraph.markdown: [18624, 18625], {
}
text.html.markdown meta.paragraph.markdown: [18625, 18631], {Since }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [18631, 18632], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [18632, 18644], {false.blank?}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [18644, 18645], {`}
text.html.markdown meta.paragraph.markdown: [18645, 18705], { is true, if you want to validate the presence of a boolean
}
text.html.markdown meta.paragraph.markdown: [18705, 18726], {field you should use }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [18726, 18727], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [18727, 18782], {validates :field_name, inclusion: { in: [true, false] }}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [18782, 18783], {`}
text.html.markdown meta.paragraph.markdown: [18783, 18785], {.
}
text.html.markdown meta.paragraph.markdown: [18785, 18786], {
}
text.html.markdown meta.paragraph.markdown: [18786, 18815], {The default error message is }
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [18815, 18816], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [18816, 18832], {"can't be empty"}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [18832, 18833], {_}
text.html.markdown meta.paragraph.markdown: [18833, 18835], {.
}
text.html.markdown meta.paragraph.markdown: [18835, 18836], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [18836, 18839], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [18839, 18840], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [18840, 18841], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [18841, 18851], {uniqueness}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [18851, 18852], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [18852, 18853], {
}
text.html.markdown: [18853, 18854], {
}
text.html.markdown meta.paragraph.markdown: [18854, 18930], {This helper validates that the attribute's value is unique right before the
}
text.html.markdown meta.paragraph.markdown: [18930, 19009], {object gets saved. It does not create a uniqueness constraint in the database,
}
text.html.markdown meta.paragraph.markdown: [19009, 19085], {so it may happen that two different database connections create two records
}
text.html.markdown meta.paragraph.markdown: [19085, 19163], {with the same value for a column that you intend to be unique. To avoid that,
}
text.html.markdown meta.paragraph.markdown: [19163, 19212], {you must create a unique index in your database.
}
text.html.markdown meta.paragraph.markdown: [19212, 19213], {
}
text.html.markdown meta.paragraph.markdown: [19213, 19221], {```ruby
}
text.html.markdown meta.paragraph.markdown: [19221, 19235], {class Account }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [19235, 19236], {<}
text.html.markdown meta.paragraph.markdown: [19236, 19256], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [19256, 19293], {  validates :email, uniqueness: true
}
text.html.markdown meta.paragraph.markdown: [19293, 19297], {end
}
text.html.markdown meta.paragraph.markdown: [19297, 19301], {```
}
text.html.markdown meta.paragraph.markdown: [19301, 19302], {
}
text.html.markdown meta.paragraph.markdown: [19302, 19376], {The validation happens by performing an SQL query into the model's table,
}
text.html.markdown meta.paragraph.markdown: [19376, 19448], {searching for an existing record with the same value in that attribute.
}
text.html.markdown meta.paragraph.markdown: [19448, 19449], {
}
text.html.markdown meta.paragraph.markdown: [19449, 19460], {There is a }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [19460, 19461], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [19461, 19467], {:scope}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [19467, 19468], {`}
text.html.markdown meta.paragraph.markdown: [19468, 19526], { option that you can use to specify other attributes that
}
text.html.markdown meta.paragraph.markdown: [19526, 19566], {are used to limit the uniqueness check:
}
text.html.markdown meta.paragraph.markdown: [19566, 19567], {
}
text.html.markdown meta.paragraph.markdown: [19567, 19575], {```ruby
}
text.html.markdown meta.paragraph.markdown: [19575, 19589], {class Holiday }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [19589, 19590], {<}
text.html.markdown meta.paragraph.markdown: [19590, 19610], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [19610, 19657], {  validates :name, uniqueness: { scope: :year,
}
text.html.markdown meta.paragraph.markdown: [19657, 19702], {    message: "should happen once per year" }
}
text.html.markdown meta.paragraph.markdown: [19702, 19706], {end
}
text.html.markdown meta.paragraph.markdown: [19706, 19710], {```
}
text.html.markdown meta.paragraph.markdown: [19710, 19711], {
}
text.html.markdown meta.paragraph.markdown: [19711, 19727], {There is also a }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [19727, 19728], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [19728, 19743], {:case_sensitive}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [19743, 19744], {`}
text.html.markdown meta.paragraph.markdown: [19744, 19791], { option that you can use to define whether the
}
text.html.markdown meta.paragraph.markdown: [19791, 19868], {uniqueness constraint will be case sensitive or not. This option defaults to
}
text.html.markdown meta.paragraph.markdown: [19868, 19874], {true.
}
text.html.markdown meta.paragraph.markdown: [19874, 19875], {
}
text.html.markdown meta.paragraph.markdown: [19875, 19883], {```ruby
}
text.html.markdown meta.paragraph.markdown: [19883, 19896], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [19896, 19897], {<}
text.html.markdown meta.paragraph.markdown: [19897, 19917], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [19917, 19974], {  validates :name, uniqueness: { case_sensitive: false }
}
text.html.markdown meta.paragraph.markdown: [19974, 19978], {end
}
text.html.markdown meta.paragraph.markdown: [19978, 19982], {```
}
text.html.markdown meta.paragraph.markdown: [19982, 19983], {
}
text.html.markdown meta.paragraph.markdown: [19983, 20060], {WARNING. Note that some databases are configured to perform case-insensitive
}
text.html.markdown meta.paragraph.markdown: [20060, 20077], {searches anyway.
}
text.html.markdown meta.paragraph.markdown: [20077, 20078], {
}
text.html.markdown meta.paragraph.markdown: [20078, 20107], {The default error message is }
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [20107, 20108], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [20108, 20132], {"has already been taken"}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [20132, 20133], {_}
text.html.markdown meta.paragraph.markdown: [20133, 20135], {.
}
text.html.markdown meta.paragraph.markdown: [20135, 20136], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [20136, 20139], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [20139, 20140], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [20140, 20141], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [20141, 20155], {validates_with}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [20155, 20156], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [20156, 20157], {
}
text.html.markdown: [20157, 20158], {
}
text.html.markdown meta.paragraph.markdown: [20158, 20224], {This helper passes the record to a separate class for validation.
}
text.html.markdown meta.paragraph.markdown: [20224, 20225], {
}
text.html.markdown meta.paragraph.markdown: [20225, 20233], {```ruby
}
text.html.markdown meta.paragraph.markdown: [20233, 20246], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [20246, 20247], {<}
text.html.markdown meta.paragraph.markdown: [20247, 20267], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [20267, 20302], {  validates_with GoodnessValidator
}
text.html.markdown meta.paragraph.markdown: [20302, 20306], {end
}
text.html.markdown meta.paragraph.markdown: [20306, 20307], {
}
text.html.markdown meta.paragraph.markdown: [20307, 20331], {class GoodnessValidator }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [20331, 20332], {<}
text.html.markdown meta.paragraph.markdown: [20332, 20356], { ActiveModel::Validator
}
text.html.markdown meta.paragraph.markdown: [20356, 20379], {  def validate(record)
}
text.html.markdown meta.paragraph.markdown: [20379, 20414], {    if record.first_name == "Evil"
}
text.html.markdown meta.paragraph.markdown: [20414, 20441], {      record.errors[:base] }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [20441, 20442], {<}
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [20442, 20443], {<}
text.html.markdown meta.paragraph.markdown: [20443, 20466], { "This person is evil"
}
text.html.markdown meta.paragraph.markdown: [20466, 20474], {    end
}
text.html.markdown meta.paragraph.markdown: [20474, 20480], {  end
}
text.html.markdown meta.paragraph.markdown: [20480, 20484], {end
}
text.html.markdown meta.paragraph.markdown: [20484, 20488], {```
}
text.html.markdown meta.paragraph.markdown: [20488, 20489], {
}
text.html.markdown meta.paragraph.markdown: [20489, 20511], {NOTE: Errors added to }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [20511, 20512], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [20512, 20532], {record.errors[:base]}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [20532, 20533], {`}
text.html.markdown meta.paragraph.markdown: [20533, 20568], { relate to the state of the record
}
text.html.markdown meta.paragraph.markdown: [20568, 20613], {as a whole, and not to a specific attribute.
}
text.html.markdown meta.paragraph.markdown: [20613, 20614], {
}
text.html.markdown meta.paragraph.markdown: [20614, 20618], {The }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [20618, 20619], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [20619, 20633], {validates_with}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [20633, 20634], {`}
text.html.markdown meta.paragraph.markdown: [20634, 20689], { helper takes a class, or a list of classes to use for
}
text.html.markdown meta.paragraph.markdown: [20689, 20739], {validation. There is no default error message for }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [20739, 20740], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [20740, 20754], {validates_with}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [20754, 20755], {`}
text.html.markdown meta.paragraph.markdown: [20755, 20766], {. You must
}
text.html.markdown meta.paragraph.markdown: [20766, 20844], {manually add errors to the record's errors collection in the validator class.
}
text.html.markdown meta.paragraph.markdown: [20844, 20845], {
}
text.html.markdown meta.paragraph.markdown: [20845, 20895], {To implement the validate method, you must have a }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [20895, 20896], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [20896, 20902], {record}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [20902, 20903], {`}
text.html.markdown meta.paragraph.markdown: [20903, 20923], { parameter defined,
}
text.html.markdown meta.paragraph.markdown: [20923, 20960], {which is the record to be validated.
}
text.html.markdown meta.paragraph.markdown: [20960, 20961], {
}
text.html.markdown meta.paragraph.markdown: [20961, 20989], {Like all other validations, }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [20989, 20990], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [20990, 21004], {validates_with}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [21004, 21005], {`}
text.html.markdown meta.paragraph.markdown: [21005, 21016], { takes the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [21016, 21017], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [21017, 21020], {:if}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [21020, 21021], {`}
text.html.markdown meta.paragraph.markdown: [21021, 21023], {, }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [21023, 21024], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [21024, 21031], {:unless}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [21031, 21032], {`}
text.html.markdown meta.paragraph.markdown: [21032, 21037], { and
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [21037, 21038], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [21038, 21041], {:on}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [21041, 21042], {`}
text.html.markdown meta.paragraph.markdown: [21042, 21117], { options. If you pass any other options, it will send those options to the
}
text.html.markdown meta.paragraph.markdown: [21117, 21136], {validator class as }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [21136, 21137], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [21137, 21144], {options}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [21144, 21145], {`}
text.html.markdown meta.paragraph.markdown: [21145, 21147], {:
}
text.html.markdown meta.paragraph.markdown: [21147, 21148], {
}
text.html.markdown meta.paragraph.markdown: [21148, 21156], {```ruby
}
text.html.markdown meta.paragraph.markdown: [21156, 21169], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [21169, 21170], {<}
text.html.markdown meta.paragraph.markdown: [21170, 21190], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [21190, 21201], {  validates}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [21201, 21202], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [21202, 21241], {with GoodnessValidator, fields: [:first}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [21241, 21242], {_}
text.html.markdown meta.paragraph.markdown: [21242, 21260], {name, :last_name]
}
text.html.markdown meta.paragraph.markdown: [21260, 21264], {end
}
text.html.markdown meta.paragraph.markdown: [21264, 21265], {
}
text.html.markdown meta.paragraph.markdown: [21265, 21289], {class GoodnessValidator }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [21289, 21290], {<}
text.html.markdown meta.paragraph.markdown: [21290, 21314], { ActiveModel::Validator
}
text.html.markdown meta.paragraph.markdown: [21314, 21337], {  def validate(record)
}
text.html.markdown meta.paragraph.markdown: [21337, 21405], {    if options[:fields].any?{|field| record.send(field) == "Evil" }
}
text.html.markdown meta.paragraph.markdown: [21405, 21432], {      record.errors[:base] }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [21432, 21433], {<}
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [21433, 21434], {<}
text.html.markdown meta.paragraph.markdown: [21434, 21457], { "This person is evil"
}
text.html.markdown meta.paragraph.markdown: [21457, 21465], {    end
}
text.html.markdown meta.paragraph.markdown: [21465, 21471], {  end
}
text.html.markdown meta.paragraph.markdown: [21471, 21475], {end
}
text.html.markdown meta.paragraph.markdown: [21475, 21479], {```
}
text.html.markdown meta.paragraph.markdown: [21479, 21480], {
}
text.html.markdown meta.paragraph.markdown: [21480, 21524], {Note that the validator will be initialized }
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [21524, 21525], {*}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [21525, 21534], {only once}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [21534, 21535], {*}
text.html.markdown meta.paragraph.markdown: [21535, 21562], { for the whole application
}
text.html.markdown meta.paragraph.markdown: [21562, 21641], {life cycle, and not on each validation run, so be careful about using instance
}
text.html.markdown meta.paragraph.markdown: [21641, 21662], {variables inside it.
}
text.html.markdown meta.paragraph.markdown: [21662, 21663], {
}
text.html.markdown meta.paragraph.markdown: [21663, 21741], {If your validator is complex enough that you want instance variables, you can
}
text.html.markdown meta.paragraph.markdown: [21741, 21785], {easily use a plain old Ruby object instead:
}
text.html.markdown meta.paragraph.markdown: [21785, 21786], {
}
text.html.markdown meta.paragraph.markdown: [21786, 21794], {```ruby
}
text.html.markdown meta.paragraph.markdown: [21794, 21807], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [21807, 21808], {<}
text.html.markdown meta.paragraph.markdown: [21808, 21828], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [21828, 21851], {  validate do |person|
}
text.html.markdown meta.paragraph.markdown: [21851, 21894], {    GoodnessValidator.new(person).validate
}
text.html.markdown meta.paragraph.markdown: [21894, 21900], {  end
}
text.html.markdown meta.paragraph.markdown: [21900, 21904], {end
}
text.html.markdown meta.paragraph.markdown: [21904, 21905], {
}
text.html.markdown meta.paragraph.markdown: [21905, 21929], {class GoodnessValidator
}
text.html.markdown meta.paragraph.markdown: [21929, 21954], {  def initialize(person)
}
text.html.markdown meta.paragraph.markdown: [21954, 21975], {    @person = person
}
text.html.markdown meta.paragraph.markdown: [21975, 21981], {  end
}
text.html.markdown meta.paragraph.markdown: [21981, 21982], {
}
text.html.markdown: [21982, 21997], {  def validate
}
text.html.markdown meta.block-level.markdown markup.raw.block.markdown: [21997, 22064], {    if some_complex_condition_involving_ivars_and_private_methods?
}
text.html.markdown meta.block-level.markdown markup.raw.block.markdown: [22064, 22117], {      @person.errors[:base] << "This person is evil"
}
text.html.markdown meta.block-level.markdown markup.raw.block.markdown: [22117, 22125], {    end
}
text.html.markdown: [22125, 22131], {  end
}
text.html.markdown: [22131, 22132], {
}
text.html.markdown: [22132, 22138], {  # …
}
text.html.markdown meta.paragraph.markdown: [22138, 22142], {end
}
text.html.markdown meta.paragraph.markdown: [22142, 22146], {```
}
text.html.markdown meta.paragraph.markdown: [22146, 22147], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [22147, 22150], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [22150, 22151], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [22151, 22152], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [22152, 22166], {validates_each}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [22166, 22167], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [22167, 22168], {
}
text.html.markdown: [22168, 22169], {
}
text.html.markdown meta.paragraph.markdown: [22169, 22248], {This helper validates attributes against a block. It doesn't have a predefined
}
text.html.markdown meta.paragraph.markdown: [22248, 22326], {validation function. You should create one using a block, and every attribute
}
text.html.markdown meta.paragraph.markdown: [22326, 22336], {passed to }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [22336, 22337], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [22337, 22351], {validates_each}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [22351, 22352], {`}
text.html.markdown meta.paragraph.markdown: [22352, 22406], { will be tested against it. In the following example,
}
text.html.markdown meta.paragraph.markdown: [22406, 22465], {we don't want names and surnames to begin with lower case.
}
text.html.markdown meta.paragraph.markdown: [22465, 22466], {
}
text.html.markdown meta.paragraph.markdown: [22466, 22474], {```ruby
}
text.html.markdown meta.paragraph.markdown: [22474, 22487], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [22487, 22488], {<}
text.html.markdown meta.paragraph.markdown: [22488, 22508], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [22508, 22566], {  validates_each :name, :surname do |record, attr, value|
}
text.html.markdown meta.paragraph.markdown: [22566, 22646], {    record.errors.add(attr, 'must start with upper case') if value =~ /\A[a-z]/
}
text.html.markdown meta.paragraph.markdown: [22646, 22652], {  end
}
text.html.markdown meta.paragraph.markdown: [22652, 22656], {end
}
text.html.markdown meta.paragraph.markdown: [22656, 22660], {```
}
text.html.markdown meta.paragraph.markdown: [22660, 22661], {
}
text.html.markdown meta.paragraph.markdown: [22661, 22740], {The block receives the record, the attribute's name and the attribute's value.
}
text.html.markdown meta.paragraph.markdown: [22740, 22819], {You can do anything you like to check for valid data within the block. If your
}
text.html.markdown meta.paragraph.markdown: [22819, 22893], {validation fails, you should add an error message to the model, therefore
}
text.html.markdown meta.paragraph.markdown: [22893, 22912], {making it invalid.
}
text.html.markdown meta.paragraph.markdown: [22912, 22913], {
}
text.html.markdown meta.paragraph.markdown: [22913, 22939], {Common Validation Options
}
text.html.markdown meta.paragraph.markdown markup.heading.2.markdown punctuation.definition.heading.markdown: [22939, 22964], {-------------------------}
text.html.markdown meta.paragraph.markdown: [22964, 22965], {
}
text.html.markdown: [22965, 22966], {
}
text.html.markdown meta.paragraph.markdown: [22966, 23003], {These are common validation options:
}
text.html.markdown meta.paragraph.markdown: [23003, 23004], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [23004, 23007], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [23007, 23008], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23008, 23009], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [23009, 23019], {:allow_nil}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23019, 23020], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [23020, 23021], {
}
text.html.markdown: [23021, 23022], {
}
text.html.markdown meta.paragraph.markdown: [23022, 23026], {The }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23026, 23027], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [23027, 23037], {:allow_nil}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23037, 23038], {`}
text.html.markdown meta.paragraph.markdown: [23038, 23101], { option skips the validation when the value being validated is
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23101, 23102], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [23102, 23105], {nil}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23105, 23106], {`}
text.html.markdown meta.paragraph.markdown: [23106, 23108], {.
}
text.html.markdown meta.paragraph.markdown: [23108, 23109], {
}
text.html.markdown meta.paragraph.markdown: [23109, 23117], {```ruby
}
text.html.markdown meta.paragraph.markdown: [23117, 23130], {class Coffee }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [23130, 23131], {<}
text.html.markdown meta.paragraph.markdown: [23131, 23151], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [23151, 23211], {  validates :size, inclusion: { in: %w(small medium large),
}
text.html.markdown meta.paragraph.markdown: [23211, 23274], {    message: "%{value} is not a valid size" }, allow_nil: true
}
text.html.markdown meta.paragraph.markdown: [23274, 23278], {end
}
text.html.markdown meta.paragraph.markdown: [23278, 23282], {```
}
text.html.markdown meta.paragraph.markdown: [23282, 23283], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [23283, 23286], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [23286, 23287], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23287, 23288], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [23288, 23300], {:allow_blank}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23300, 23301], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [23301, 23302], {
}
text.html.markdown: [23302, 23303], {
}
text.html.markdown meta.paragraph.markdown: [23303, 23307], {The }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23307, 23308], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [23308, 23320], {:allow_blank}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23320, 23321], {`}
text.html.markdown meta.paragraph.markdown: [23321, 23347], { option is similar to the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23347, 23348], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [23348, 23358], {:allow_nil}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23358, 23359], {`}
text.html.markdown meta.paragraph.markdown: [23359, 23380], { option. This option
}
text.html.markdown meta.paragraph.markdown: [23380, 23433], {will let validation pass if the attribute's value is }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23433, 23434], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [23434, 23440], {blank?}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23440, 23441], {`}
text.html.markdown meta.paragraph.markdown: [23441, 23448], {, like }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23448, 23449], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [23449, 23452], {nil}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23452, 23453], {`}
text.html.markdown meta.paragraph.markdown: [23453, 23460], { or an
}
text.html.markdown meta.paragraph.markdown: [23460, 23486], {empty string for example.
}
text.html.markdown meta.paragraph.markdown: [23486, 23487], {
}
text.html.markdown meta.paragraph.markdown: [23487, 23495], {```ruby
}
text.html.markdown meta.paragraph.markdown: [23495, 23507], {class Topic }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [23507, 23508], {<}
text.html.markdown meta.paragraph.markdown: [23508, 23528], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [23528, 23585], {  validates :title, length: { is: 5 }, allow_blank: true
}
text.html.markdown meta.paragraph.markdown: [23585, 23589], {end
}
text.html.markdown meta.paragraph.markdown: [23589, 23590], {
}
text.html.markdown meta.paragraph.markdown: [23590, 23636], {Topic.create("title" => "").valid?  # => true
}
text.html.markdown meta.paragraph.markdown: [23636, 23682], {Topic.create("title" => nil).valid? # => true
}
text.html.markdown meta.paragraph.markdown: [23682, 23686], {```
}
text.html.markdown meta.paragraph.markdown: [23686, 23687], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [23687, 23690], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [23690, 23691], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23691, 23692], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [23692, 23700], {:message}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23700, 23701], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [23701, 23702], {
}
text.html.markdown: [23702, 23703], {
}
text.html.markdown meta.paragraph.markdown: [23703, 23731], {As you've already seen, the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23731, 23732], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [23732, 23740], {:message}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23740, 23741], {`}
text.html.markdown meta.paragraph.markdown: [23741, 23783], { option lets you specify the message that
}
text.html.markdown meta.paragraph.markdown: [23783, 23804], {will be added to the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23804, 23805], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [23805, 23811], {errors}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23811, 23812], {`}
text.html.markdown meta.paragraph.markdown: [23812, 23857], { collection when validation fails. When this
}
text.html.markdown meta.paragraph.markdown: [23857, 23937], {option is not used, Active Record will use the respective default error message
}
text.html.markdown meta.paragraph.markdown: [23937, 23965], {for each validation helper.
}
text.html.markdown meta.paragraph.markdown: [23965, 23966], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [23966, 23969], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [23969, 23970], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23970, 23971], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [23971, 23974], {:on}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23974, 23975], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [23975, 23976], {
}
text.html.markdown: [23976, 23977], {
}
text.html.markdown meta.paragraph.markdown: [23977, 23981], {The }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23981, 23982], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [23982, 23985], {:on}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [23985, 23986], {`}
text.html.markdown meta.paragraph.markdown: [23986, 24050], { option lets you specify when the validation should happen. The
}
text.html.markdown meta.paragraph.markdown: [24050, 24128], {default behavior for all the built-in validation helpers is to be run on save
}
text.html.markdown meta.paragraph.markdown: [24128, 24205], {(both when you're creating a new record and when you're updating it). If you
}
text.html.markdown meta.paragraph.markdown: [24205, 24236], {want to change it, you can use }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [24236, 24237], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [24237, 24248], {on: :create}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [24248, 24249], {`}
text.html.markdown meta.paragraph.markdown: [24249, 24284], { to run the validation only when a
}
text.html.markdown meta.paragraph.markdown: [24284, 24309], {new record is created or }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [24309, 24310], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [24310, 24321], {on: :update}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [24321, 24322], {`}
text.html.markdown meta.paragraph.markdown: [24322, 24364], { to run the validation only when a record
}
text.html.markdown meta.paragraph.markdown: [24364, 24376], {is updated.
}
text.html.markdown meta.paragraph.markdown: [24376, 24377], {
}
text.html.markdown meta.paragraph.markdown: [24377, 24385], {```ruby
}
text.html.markdown meta.paragraph.markdown: [24385, 24398], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [24398, 24399], {<}
text.html.markdown meta.paragraph.markdown: [24399, 24419], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [24419, 24483], {  # it will be possible to update email with a duplicated value
}
text.html.markdown meta.paragraph.markdown: [24483, 24533], {  validates :email, uniqueness: true, on: :create
}
text.html.markdown meta.paragraph.markdown: [24533, 24534], {
}
text.html.markdown: [24534, 24604], {  # it will be possible to create the record with a non-numerical age
}
text.html.markdown: [24604, 24654], {  validates :age, numericality: true, on: :update
}
text.html.markdown: [24654, 24655], {
}
text.html.markdown: [24655, 24709], {  # the default (validates on both create and update)
}
text.html.markdown: [24709, 24754], {  validates :name, presence: true, on: :save
}
text.html.markdown meta.paragraph.markdown: [24754, 24758], {end
}
text.html.markdown meta.paragraph.markdown: [24758, 24762], {```
}
text.html.markdown meta.paragraph.markdown: [24762, 24763], {
}
text.html.markdown meta.paragraph.markdown: [24763, 24782], {Strict Validations
}
text.html.markdown meta.paragraph.markdown markup.heading.2.markdown punctuation.definition.heading.markdown: [24782, 24800], {------------------}
text.html.markdown meta.paragraph.markdown: [24800, 24801], {
}
text.html.markdown: [24801, 24802], {
}
text.html.markdown meta.paragraph.markdown: [24802, 24858], {You can also specify validations to be strict and raise
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [24858, 24859], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [24859, 24894], {ActiveModel::StrictValidationFailed}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [24894, 24895], {`}
text.html.markdown meta.paragraph.markdown: [24895, 24924], { when the object is invalid.
}
text.html.markdown meta.paragraph.markdown: [24924, 24925], {
}
text.html.markdown meta.paragraph.markdown: [24925, 24933], {```ruby
}
text.html.markdown meta.paragraph.markdown: [24933, 24946], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [24946, 24947], {<}
text.html.markdown meta.paragraph.markdown: [24947, 24967], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [24967, 25013], {  validates :name, presence: { strict: true }
}
text.html.markdown meta.paragraph.markdown: [25013, 25017], {end
}
text.html.markdown meta.paragraph.markdown: [25017, 25018], {
}
text.html.markdown meta.paragraph.markdown: [25018, 25099], {Person.new.valid?  # => ActiveModel::StrictValidationFailed: Name can't be blank
}
text.html.markdown meta.paragraph.markdown: [25099, 25103], {```
}
text.html.markdown meta.paragraph.markdown: [25103, 25104], {
}
text.html.markdown meta.paragraph.markdown: [25104, 25157], {There is also an ability to pass custom exception to }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25157, 25158], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [25158, 25165], {:strict}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25165, 25166], {`}
text.html.markdown meta.paragraph.markdown: [25166, 25174], { option
}
text.html.markdown meta.paragraph.markdown: [25174, 25175], {
}
text.html.markdown meta.paragraph.markdown: [25175, 25183], {```ruby
}
text.html.markdown meta.paragraph.markdown: [25183, 25196], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [25196, 25197], {<}
text.html.markdown meta.paragraph.markdown: [25197, 25217], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [25217, 25304], {  validates :token, presence: true, uniqueness: true, strict: TokenGenerationException
}
text.html.markdown meta.paragraph.markdown: [25304, 25308], {end
}
text.html.markdown meta.paragraph.markdown: [25308, 25309], {
}
text.html.markdown meta.paragraph.markdown: [25309, 25380], {Person.new.valid?  # => TokenGenerationException: Token can't be blank
}
text.html.markdown meta.paragraph.markdown: [25380, 25384], {```
}
text.html.markdown meta.paragraph.markdown: [25384, 25385], {
}
text.html.markdown meta.paragraph.markdown: [25385, 25408], {Conditional Validation
}
text.html.markdown meta.paragraph.markdown markup.heading.2.markdown punctuation.definition.heading.markdown: [25408, 25430], {----------------------}
text.html.markdown meta.paragraph.markdown: [25430, 25431], {
}
text.html.markdown: [25431, 25432], {
}
text.html.markdown meta.paragraph.markdown: [25432, 25511], {Sometimes it will make sense to validate an object only when a given predicate
}
text.html.markdown meta.paragraph.markdown: [25511, 25554], {is satisfied. You can do that by using the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25554, 25555], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [25555, 25558], {:if}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25558, 25559], {`}
text.html.markdown meta.paragraph.markdown: [25559, 25564], { and }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25564, 25565], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [25565, 25572], {:unless}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25572, 25573], {`}
text.html.markdown meta.paragraph.markdown: [25573, 25589], { options, which
}
text.html.markdown meta.paragraph.markdown: [25589, 25620], {can take a symbol, a string, a }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25620, 25621], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [25621, 25625], {Proc}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25625, 25626], {`}
text.html.markdown meta.paragraph.markdown: [25626, 25633], { or an }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25633, 25634], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [25634, 25639], {Array}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25639, 25640], {`}
text.html.markdown meta.paragraph.markdown: [25640, 25658], {. You may use the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25658, 25659], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [25659, 25662], {:if}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25662, 25663], {`}
text.html.markdown meta.paragraph.markdown: [25663, 25664], {
}
text.html.markdown meta.paragraph.markdown: [25664, 25716], {option when you want to specify when the validation }
text.html.markdown meta.paragraph.markdown markup.bold.markdown punctuation.definition.bold.markdown: [25716, 25718], {**}
text.html.markdown meta.paragraph.markdown markup.bold.markdown: [25718, 25724], {should}
text.html.markdown meta.paragraph.markdown markup.bold.markdown punctuation.definition.bold.markdown: [25724, 25726], {**}
text.html.markdown meta.paragraph.markdown: [25726, 25742], { happen. If you
}
text.html.markdown meta.paragraph.markdown: [25742, 25778], {want to specify when the validation }
text.html.markdown meta.paragraph.markdown markup.bold.markdown punctuation.definition.bold.markdown: [25778, 25780], {**}
text.html.markdown meta.paragraph.markdown markup.bold.markdown: [25780, 25790], {should not}
text.html.markdown meta.paragraph.markdown markup.bold.markdown punctuation.definition.bold.markdown: [25790, 25792], {**}
text.html.markdown meta.paragraph.markdown: [25792, 25822], { happen, then you may use the
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25822, 25823], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [25823, 25830], {:unless}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25830, 25831], {`}
text.html.markdown meta.paragraph.markdown: [25831, 25840], { option.
}
text.html.markdown meta.paragraph.markdown: [25840, 25841], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [25841, 25844], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [25844, 25845], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown: [25845, 25865], {Using a Symbol with }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25865, 25866], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [25866, 25869], {:if}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25869, 25870], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown: [25870, 25875], { and }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25875, 25876], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [25876, 25883], {:unless}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25883, 25884], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [25884, 25885], {
}
text.html.markdown: [25885, 25886], {
}
text.html.markdown meta.paragraph.markdown: [25886, 25908], {You can associate the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25908, 25909], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [25909, 25912], {:if}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25912, 25913], {`}
text.html.markdown meta.paragraph.markdown: [25913, 25918], { and }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25918, 25919], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [25919, 25926], {:unless}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [25926, 25927], {`}
text.html.markdown meta.paragraph.markdown: [25927, 25964], { options with a symbol corresponding
}
text.html.markdown meta.paragraph.markdown: [25964, 26042], {to the name of a method that will get called right before validation happens.
}
text.html.markdown meta.paragraph.markdown: [26042, 26081], {This is the most commonly used option.
}
text.html.markdown meta.paragraph.markdown: [26081, 26082], {
}
text.html.markdown meta.paragraph.markdown: [26082, 26090], {```ruby
}
text.html.markdown meta.paragraph.markdown: [26090, 26102], {class Order }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [26102, 26103], {<}
text.html.markdown meta.paragraph.markdown: [26103, 26123], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [26123, 26140], {  validates :card}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [26140, 26141], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [26141, 26174], {number, presence: true, if: :paid}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [26174, 26175], {_}
text.html.markdown meta.paragraph.markdown: [26175, 26186], {with_card?
}
text.html.markdown meta.paragraph.markdown: [26186, 26187], {
}
text.html.markdown: [26187, 26209], {  def paid_with_card?
}
text.html.markdown meta.block-level.markdown markup.raw.block.markdown: [26209, 26236], {    payment_type == "card"
}
text.html.markdown: [26236, 26242], {  end
}
text.html.markdown meta.paragraph.markdown: [26242, 26246], {end
}
text.html.markdown meta.paragraph.markdown: [26246, 26250], {```
}
text.html.markdown meta.paragraph.markdown: [26250, 26251], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [26251, 26254], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [26254, 26255], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown: [26255, 26275], {Using a String with }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26275, 26276], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [26276, 26279], {:if}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26279, 26280], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown: [26280, 26285], { and }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26285, 26286], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [26286, 26293], {:unless}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26293, 26294], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [26294, 26295], {
}
text.html.markdown: [26295, 26296], {
}
text.html.markdown meta.paragraph.markdown: [26296, 26351], {You can also use a string that will be evaluated using }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26351, 26352], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [26352, 26356], {eval}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26356, 26357], {`}
text.html.markdown meta.paragraph.markdown: [26357, 26371], { and needs to
}
text.html.markdown meta.paragraph.markdown: [26371, 26444], {contain valid Ruby code. You should use this option only when the string
}
text.html.markdown meta.paragraph.markdown: [26444, 26481], {represents a really short condition.
}
text.html.markdown meta.paragraph.markdown: [26481, 26482], {
}
text.html.markdown meta.paragraph.markdown: [26482, 26490], {```ruby
}
text.html.markdown meta.paragraph.markdown: [26490, 26503], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [26503, 26504], {<}
text.html.markdown meta.paragraph.markdown: [26504, 26524], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [26524, 26578], {  validates :surname, presence: true, if: "name.nil?"
}
text.html.markdown meta.paragraph.markdown: [26578, 26582], {end
}
text.html.markdown meta.paragraph.markdown: [26582, 26586], {```
}
text.html.markdown meta.paragraph.markdown: [26586, 26587], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [26587, 26590], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [26590, 26591], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown: [26591, 26609], {Using a Proc with }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26609, 26610], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [26610, 26613], {:if}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26613, 26614], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown: [26614, 26619], { and }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26619, 26620], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [26620, 26627], {:unless}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26627, 26628], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [26628, 26629], {
}
text.html.markdown: [26629, 26630], {
}
text.html.markdown meta.paragraph.markdown: [26630, 26666], {Finally, it's possible to associate }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26666, 26667], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [26667, 26670], {:if}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26670, 26671], {`}
text.html.markdown meta.paragraph.markdown: [26671, 26676], { and }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26676, 26677], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [26677, 26684], {:unless}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26684, 26685], {`}
text.html.markdown meta.paragraph.markdown: [26685, 26693], { with a }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26693, 26694], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [26694, 26698], {Proc}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26698, 26699], {`}
text.html.markdown meta.paragraph.markdown: [26699, 26707], { object
}
text.html.markdown meta.paragraph.markdown: [26707, 26737], {which will be called. Using a }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26737, 26738], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [26738, 26742], {Proc}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [26742, 26743], {`}
text.html.markdown meta.paragraph.markdown: [26743, 26785], { object gives you the ability to write an
}
text.html.markdown meta.paragraph.markdown: [26785, 26863], {inline condition instead of a separate method. This option is best suited for
}
text.html.markdown meta.paragraph.markdown: [26863, 26875], {one-liners.
}
text.html.markdown meta.paragraph.markdown: [26875, 26876], {
}
text.html.markdown meta.paragraph.markdown: [26876, 26884], {```ruby
}
text.html.markdown meta.paragraph.markdown: [26884, 26898], {class Account }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [26898, 26899], {<}
text.html.markdown meta.paragraph.markdown: [26899, 26919], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [26919, 26962], {  validates :password, confirmation: true,
}
text.html.markdown meta.paragraph.markdown: [26962, 27009], {    unless: Proc.new { |a| a.password.blank? }
}
text.html.markdown meta.paragraph.markdown: [27009, 27013], {end
}
text.html.markdown meta.paragraph.markdown: [27013, 27017], {```
}
text.html.markdown meta.paragraph.markdown: [27017, 27018], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [27018, 27021], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [27021, 27022], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown: [27022, 27054], {Grouping Conditional validations}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [27054, 27055], {
}
text.html.markdown: [27055, 27056], {
}
text.html.markdown meta.paragraph.markdown: [27056, 27134], {Sometimes it is useful to have multiple validations use one condition, it can
}
text.html.markdown meta.paragraph.markdown: [27134, 27159], {be easily achieved using }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [27159, 27160], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [27160, 27172], {with_options}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [27172, 27173], {`}
text.html.markdown meta.paragraph.markdown: [27173, 27175], {.
}
text.html.markdown meta.paragraph.markdown: [27175, 27176], {
}
text.html.markdown meta.paragraph.markdown: [27176, 27184], {```ruby
}
text.html.markdown meta.paragraph.markdown: [27184, 27195], {class User }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [27195, 27196], {<}
text.html.markdown meta.paragraph.markdown: [27196, 27216], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [27216, 27222], {  with}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [27222, 27223], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [27223, 27238], {options if: :is}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [27238, 27239], {_}
text.html.markdown meta.paragraph.markdown: [27239, 27257], {admin? do |admin|
}
text.html.markdown meta.paragraph.markdown: [27257, 27312], {    admin.validates :password, length: { minimum: 10 }
}
text.html.markdown meta.paragraph.markdown: [27312, 27355], {    admin.validates :email, presence: true
}
text.html.markdown meta.paragraph.markdown: [27355, 27361], {  end
}
text.html.markdown meta.paragraph.markdown: [27361, 27365], {end
}
text.html.markdown meta.paragraph.markdown: [27365, 27369], {```
}
text.html.markdown meta.paragraph.markdown: [27369, 27370], {
}
text.html.markdown meta.paragraph.markdown: [27370, 27396], {All validations inside of }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [27396, 27397], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [27397, 27409], {with_options}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [27409, 27410], {`}
text.html.markdown meta.paragraph.markdown: [27410, 27448], { block will have automatically passed
}
text.html.markdown meta.paragraph.markdown: [27448, 27462], {the condition }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [27462, 27463], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [27463, 27477], {if: :is_admin?}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [27477, 27478], {`}
text.html.markdown meta.paragraph.markdown: [27478, 27479], {
}
text.html.markdown meta.paragraph.markdown: [27479, 27480], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [27480, 27483], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [27483, 27484], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown: [27484, 27515], {Combining Validation Conditions}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [27515, 27516], {
}
text.html.markdown: [27516, 27517], {
}
text.html.markdown meta.paragraph.markdown: [27517, 27596], {On the other hand, when multiple conditions define whether or not a validation
}
text.html.markdown meta.paragraph.markdown: [27596, 27614], {should happen, an }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [27614, 27615], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [27615, 27620], {Array}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [27620, 27621], {`}
text.html.markdown meta.paragraph.markdown: [27621, 27664], { can be used. Moreover, you can apply both }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [27664, 27665], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [27665, 27668], {:if}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [27668, 27669], {`}
text.html.markdown meta.paragraph.markdown: [27669, 27674], { and
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [27674, 27675], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [27675, 27682], {:unless}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [27682, 27683], {`}
text.html.markdown meta.paragraph.markdown: [27683, 27708], { to the same validation.
}
text.html.markdown meta.paragraph.markdown: [27708, 27709], {
}
text.html.markdown meta.paragraph.markdown: [27709, 27717], {```ruby
}
text.html.markdown meta.paragraph.markdown: [27717, 27732], {class Computer }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [27732, 27733], {<}
text.html.markdown meta.paragraph.markdown: [27733, 27753], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [27753, 27789], {  validates :mouse, presence: true,
}
text.html.markdown meta.paragraph.markdown: [27789, 27843], {                    if: ["market.retail?", :desktop?]
}
text.html.markdown meta.paragraph.markdown: [27843, 27908], {                    unless: Proc.new { |c| c.trackpad.present? }
}
text.html.markdown meta.paragraph.markdown: [27908, 27912], {end
}
text.html.markdown meta.paragraph.markdown: [27912, 27916], {```
}
text.html.markdown meta.paragraph.markdown: [27916, 27917], {
}
text.html.markdown meta.paragraph.markdown: [27917, 27955], {The validation only runs when all the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [27955, 27956], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [27956, 27959], {:if}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [27959, 27960], {`}
text.html.markdown meta.paragraph.markdown: [27960, 27988], { conditions and none of the
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [27988, 27989], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [27989, 27996], {:unless}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [27996, 27997], {`}
text.html.markdown meta.paragraph.markdown: [27997, 28026], { conditions are evaluated to }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [28026, 28027], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [28027, 28031], {true}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [28031, 28032], {`}
text.html.markdown meta.paragraph.markdown: [28032, 28034], {.
}
text.html.markdown meta.paragraph.markdown: [28034, 28035], {
}
text.html.markdown meta.paragraph.markdown: [28035, 28065], {Performing Custom Validations
}
text.html.markdown meta.paragraph.markdown markup.heading.2.markdown punctuation.definition.heading.markdown: [28065, 28094], {-----------------------------}
text.html.markdown meta.paragraph.markdown: [28094, 28095], {
}
text.html.markdown: [28095, 28096], {
}
text.html.markdown meta.paragraph.markdown: [28096, 28172], {When the built-in validation helpers are not enough for your needs, you can
}
text.html.markdown meta.paragraph.markdown: [28172, 28235], {write your own validators or validation methods as you prefer.
}
text.html.markdown meta.paragraph.markdown: [28235, 28236], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [28236, 28239], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [28239, 28240], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown: [28240, 28257], {Custom Validators}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [28257, 28258], {
}
text.html.markdown: [28258, 28259], {
}
text.html.markdown meta.paragraph.markdown: [28259, 28301], {Custom validators are classes that extend }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [28301, 28302], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [28302, 28324], {ActiveModel::Validator}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [28324, 28325], {`}
text.html.markdown meta.paragraph.markdown: [28325, 28333], {. These
}
text.html.markdown meta.paragraph.markdown: [28333, 28358], {classes must implement a }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [28358, 28359], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [28359, 28367], {validate}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [28367, 28368], {`}
text.html.markdown meta.paragraph.markdown: [28368, 28412], { method which takes a record as an argument
}
text.html.markdown meta.paragraph.markdown: [28412, 28488], {and performs the validation on it. The custom validator is called using the
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [28488, 28489], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [28489, 28503], {validates_with}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [28503, 28504], {`}
text.html.markdown meta.paragraph.markdown: [28504, 28513], { method.
}
text.html.markdown meta.paragraph.markdown: [28513, 28514], {
}
text.html.markdown meta.paragraph.markdown: [28514, 28522], {```ruby
}
text.html.markdown meta.paragraph.markdown: [28522, 28540], {class MyValidator }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [28540, 28541], {<}
text.html.markdown meta.paragraph.markdown: [28541, 28565], { ActiveModel::Validator
}
text.html.markdown meta.paragraph.markdown: [28565, 28588], {  def validate(record)
}
text.html.markdown meta.paragraph.markdown: [28588, 28628], {    unless record.name.starts_with? 'X'
}
text.html.markdown meta.paragraph.markdown: [28628, 28655], {      record.errors[:name] }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [28655, 28656], {<}
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [28656, 28657], {<}
text.html.markdown meta.paragraph.markdown: [28657, 28696], { 'Need a name starting with X please!'
}
text.html.markdown meta.paragraph.markdown: [28696, 28704], {    end
}
text.html.markdown meta.paragraph.markdown: [28704, 28710], {  end
}
text.html.markdown meta.paragraph.markdown: [28710, 28714], {end
}
text.html.markdown meta.paragraph.markdown: [28714, 28715], {
}
text.html.markdown meta.paragraph.markdown: [28715, 28728], {class Person
}
text.html.markdown meta.paragraph.markdown: [28728, 28763], {  include ActiveModel::Validations
}
text.html.markdown meta.paragraph.markdown: [28763, 28792], {  validates_with MyValidator
}
text.html.markdown meta.paragraph.markdown: [28792, 28796], {end
}
text.html.markdown meta.paragraph.markdown: [28796, 28800], {```
}
text.html.markdown meta.paragraph.markdown: [28800, 28801], {
}
text.html.markdown meta.paragraph.markdown: [28801, 28879], {The easiest way to add custom validators for validating individual attributes
}
text.html.markdown meta.paragraph.markdown: [28879, 28902], {is with the convenient }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [28902, 28903], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [28903, 28929], {ActiveModel::EachValidator}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [28929, 28930], {`}
text.html.markdown meta.paragraph.markdown: [28930, 28957], {. In this case, the custom
}
text.html.markdown meta.paragraph.markdown: [28957, 28990], {validator class must implement a }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [28990, 28991], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [28991, 29004], {validate_each}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [29004, 29005], {`}
text.html.markdown meta.paragraph.markdown: [29005, 29031], { method which takes three
}
text.html.markdown meta.paragraph.markdown: [29031, 29108], {arguments: record, attribute and value which correspond to the instance, the
}
text.html.markdown meta.paragraph.markdown: [29108, 29179], {attribute to be validated and the value of the attribute in the passed
}
text.html.markdown meta.paragraph.markdown: [29179, 29189], {instance.
}
text.html.markdown meta.paragraph.markdown: [29189, 29190], {
}
text.html.markdown meta.paragraph.markdown: [29190, 29198], {```ruby
}
text.html.markdown meta.paragraph.markdown: [29198, 29219], {class EmailValidator }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [29219, 29220], {<}
text.html.markdown meta.paragraph.markdown: [29220, 29248], { ActiveModel::EachValidator
}
text.html.markdown meta.paragraph.markdown: [29248, 29294], {  def validate_each(record, attribute, value)
}
text.html.markdown meta.paragraph.markdown: [29294, 29341], {    unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+}
text.html.markdown meta.paragraph.markdown constant.character.escape.markdown: [29341, 29343], {\.}
text.html.markdown meta.paragraph.markdown: [29343, 29360], {)+[a-z]{2,})\z/i
}
text.html.markdown meta.paragraph.markdown: [29360, 29391], {      record.errors[attribute] }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [29391, 29392], {<}
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [29392, 29393], {<}
text.html.markdown meta.paragraph.markdown: [29393, 29435], { (options[:message] || "is not an email")
}
text.html.markdown meta.paragraph.markdown: [29435, 29443], {    end
}
text.html.markdown meta.paragraph.markdown: [29443, 29449], {  end
}
text.html.markdown meta.paragraph.markdown: [29449, 29453], {end
}
text.html.markdown meta.paragraph.markdown: [29453, 29454], {
}
text.html.markdown meta.paragraph.markdown: [29454, 29467], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [29467, 29468], {<}
text.html.markdown meta.paragraph.markdown: [29468, 29488], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [29488, 29536], {  validates :email, presence: true, email: true
}
text.html.markdown meta.paragraph.markdown: [29536, 29540], {end
}
text.html.markdown meta.paragraph.markdown: [29540, 29544], {```
}
text.html.markdown meta.paragraph.markdown: [29544, 29545], {
}
text.html.markdown meta.paragraph.markdown: [29545, 29622], {As shown in the example, you can also combine standard validations with your
}
text.html.markdown meta.paragraph.markdown: [29622, 29645], {own custom validators.
}
text.html.markdown meta.paragraph.markdown: [29645, 29646], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [29646, 29649], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [29649, 29650], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown: [29650, 29664], {Custom Methods}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [29664, 29665], {
}
text.html.markdown: [29665, 29666], {
}
text.html.markdown meta.paragraph.markdown: [29666, 29739], {You can also create methods that verify the state of your models and add
}
text.html.markdown meta.paragraph.markdown: [29739, 29755], {messages to the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [29755, 29756], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [29756, 29762], {errors}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [29762, 29763], {`}
text.html.markdown meta.paragraph.markdown: [29763, 29812], { collection when they are invalid. You must then
}
text.html.markdown meta.paragraph.markdown: [29812, 29848], {register these methods by using the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [29848, 29849], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [29849, 29857], {validate}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [29857, 29858], {`}
text.html.markdown meta.paragraph.markdown: [29858, 29888], { class method, passing in the
}
text.html.markdown meta.paragraph.markdown: [29888, 29931], {symbols for the validation methods' names.
}
text.html.markdown meta.paragraph.markdown: [29931, 29932], {
}
text.html.markdown meta.paragraph.markdown: [29932, 30007], {You can pass more than one symbol for each class method and the respective
}
text.html.markdown meta.paragraph.markdown: [30007, 30074], {validations will be run in the same order as they were registered.
}
text.html.markdown meta.paragraph.markdown: [30074, 30075], {
}
text.html.markdown meta.paragraph.markdown: [30075, 30083], {```ruby
}
text.html.markdown meta.paragraph.markdown: [30083, 30097], {class Invoice }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [30097, 30098], {<}
text.html.markdown meta.paragraph.markdown: [30098, 30118], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [30118, 30140], {  validate :expiration}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [30140, 30141], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [30141, 30145], {date}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [30145, 30146], {_}
text.html.markdown meta.paragraph.markdown: [30146, 30152], {cannot}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [30152, 30153], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [30153, 30155], {be}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [30155, 30156], {_}
text.html.markdown meta.paragraph.markdown: [30156, 30158], {in}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [30158, 30159], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [30159, 30162], {the}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [30162, 30163], {_}
text.html.markdown meta.paragraph.markdown: [30163, 30169], {past,
}
text.html.markdown meta.paragraph.markdown: [30169, 30182], {    :discount}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [30182, 30183], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [30183, 30189], {cannot}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [30189, 30190], {_}
text.html.markdown meta.paragraph.markdown: [30190, 30192], {be}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [30192, 30193], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [30193, 30200], {greater}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [30200, 30201], {_}
text.html.markdown meta.paragraph.markdown: [30201, 30205], {than}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [30205, 30206], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [30206, 30211], {total}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [30211, 30212], {_}
text.html.markdown meta.paragraph.markdown: [30212, 30218], {value
}
text.html.markdown meta.paragraph.markdown: [30218, 30219], {
}
text.html.markdown: [30219, 30263], {  def expiration_date_cannot_be_in_the_past
}
text.html.markdown meta.block-level.markdown markup.raw.block.markdown: [30263, 30327], {    if expiration_date.present? && expiration_date < Date.today
}
text.html.markdown meta.block-level.markdown markup.raw.block.markdown: [30327, 30386], {      errors.add(:expiration_date, "can't be in the past")
}
text.html.markdown meta.block-level.markdown markup.raw.block.markdown: [30386, 30394], {    end
}
text.html.markdown: [30394, 30400], {  end
}
text.html.markdown: [30400, 30401], {
}
text.html.markdown: [30401, 30451], {  def discount_cannot_be_greater_than_total_value
}
text.html.markdown meta.block-level.markdown markup.raw.block.markdown: [30451, 30481], {    if discount > total_value
}
text.html.markdown meta.block-level.markdown markup.raw.block.markdown: [30481, 30546], {      errors.add(:discount, "can't be greater than total value")
}
text.html.markdown meta.block-level.markdown markup.raw.block.markdown: [30546, 30554], {    end
}
text.html.markdown: [30554, 30560], {  end
}
text.html.markdown meta.paragraph.markdown: [30560, 30564], {end
}
text.html.markdown meta.paragraph.markdown: [30564, 30568], {```
}
text.html.markdown meta.paragraph.markdown: [30568, 30569], {
}
text.html.markdown meta.paragraph.markdown: [30569, 30626], {By default such validations will run every time you call }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [30626, 30627], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [30627, 30633], {valid?}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [30633, 30634], {`}
text.html.markdown meta.paragraph.markdown: [30634, 30647], {. It is also
}
text.html.markdown meta.paragraph.markdown: [30647, 30717], {possible to control when to run these custom validations by giving an }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [30717, 30718], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [30718, 30721], {:on}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [30721, 30722], {`}
text.html.markdown meta.paragraph.markdown: [30722, 30723], {
}
text.html.markdown meta.paragraph.markdown: [30723, 30737], {option to the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [30737, 30738], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [30738, 30746], {validate}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [30746, 30747], {`}
text.html.markdown meta.paragraph.markdown: [30747, 30769], { method, with either: }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [30769, 30770], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [30770, 30777], {:create}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [30777, 30778], {`}
text.html.markdown meta.paragraph.markdown: [30778, 30782], { or }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [30782, 30783], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [30783, 30790], {:update}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [30790, 30791], {`}
text.html.markdown meta.paragraph.markdown: [30791, 30793], {.
}
text.html.markdown meta.paragraph.markdown: [30793, 30794], {
}
text.html.markdown meta.paragraph.markdown: [30794, 30802], {```ruby
}
text.html.markdown meta.paragraph.markdown: [30802, 30816], {class Invoice }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [30816, 30817], {<}
text.html.markdown meta.paragraph.markdown: [30817, 30837], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [30837, 30878], {  validate :active_customer, on: :create
}
text.html.markdown meta.paragraph.markdown: [30878, 30879], {
}
text.html.markdown: [30879, 30901], {  def active_customer
}
text.html.markdown meta.block-level.markdown markup.raw.block.markdown: [30901, 30971], {    errors.add(:customer_id, "is not active") unless customer.active?
}
text.html.markdown: [30971, 30977], {  end
}
text.html.markdown meta.paragraph.markdown: [30977, 30981], {end
}
text.html.markdown meta.paragraph.markdown: [30981, 30985], {```
}
text.html.markdown meta.paragraph.markdown: [30985, 30986], {
}
text.html.markdown meta.paragraph.markdown: [30986, 31017], {Working with Validation Errors
}
text.html.markdown meta.paragraph.markdown markup.heading.2.markdown punctuation.definition.heading.markdown: [31017, 31047], {------------------------------}
text.html.markdown meta.paragraph.markdown: [31047, 31048], {
}
text.html.markdown: [31048, 31049], {
}
text.html.markdown meta.paragraph.markdown: [31049, 31068], {In addition to the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [31068, 31069], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [31069, 31075], {valid?}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [31075, 31076], {`}
text.html.markdown meta.paragraph.markdown: [31076, 31081], { and }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [31081, 31082], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [31082, 31090], {invalid?}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [31090, 31091], {`}
text.html.markdown meta.paragraph.markdown: [31091, 31173], { methods covered earlier, Rails provides a number of methods for working with the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [31173, 31174], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [31174, 31180], {errors}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [31180, 31181], {`}
text.html.markdown meta.paragraph.markdown: [31181, 31238], { collection and inquiring about the validity of objects.
}
text.html.markdown meta.paragraph.markdown: [31238, 31239], {
}
text.html.markdown meta.paragraph.markdown: [31239, 31318], {The following is a list of the most commonly used methods. Please refer to the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [31318, 31319], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [31319, 31338], {ActiveModel::Errors}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [31338, 31339], {`}
text.html.markdown meta.paragraph.markdown: [31339, 31395], { documentation for a list of all the available methods.
}
text.html.markdown meta.paragraph.markdown: [31395, 31396], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [31396, 31399], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [31399, 31400], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [31400, 31401], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [31401, 31407], {errors}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [31407, 31408], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [31408, 31409], {
}
text.html.markdown: [31409, 31410], {
}
text.html.markdown meta.paragraph.markdown: [31410, 31443], {Returns an instance of the class }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [31443, 31444], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [31444, 31463], {ActiveModel::Errors}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [31463, 31464], {`}
text.html.markdown meta.paragraph.markdown: [31464, 31573], { containing all errors. Each key is the attribute name and the value is an array of strings with all errors.
}
text.html.markdown meta.paragraph.markdown: [31573, 31574], {
}
text.html.markdown meta.paragraph.markdown: [31574, 31582], {```ruby
}
text.html.markdown meta.paragraph.markdown: [31582, 31595], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [31595, 31596], {<}
text.html.markdown meta.paragraph.markdown: [31596, 31616], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [31616, 31674], {  validates :name, presence: true, length: { minimum: 3 }
}
text.html.markdown meta.paragraph.markdown: [31674, 31678], {end
}
text.html.markdown meta.paragraph.markdown: [31678, 31679], {
}
text.html.markdown meta.paragraph.markdown: [31679, 31699], {person = Person.new
}
text.html.markdown meta.paragraph.markdown: [31699, 31724], {person.valid? # => false
}
text.html.markdown meta.paragraph.markdown: [31724, 31738], {person.errors
}
text.html.markdown meta.paragraph.markdown: [31738, 31814], { # => {:name=>["can't be blank", "is too short (minimum is 3 characters)"]}
}
text.html.markdown meta.paragraph.markdown: [31814, 31815], {
}
text.html.markdown meta.paragraph.markdown: [31815, 31853], {person = Person.new(name: "John Doe")
}
text.html.markdown meta.paragraph.markdown: [31853, 31877], {person.valid? # => true
}
text.html.markdown meta.paragraph.markdown: [31877, 31899], {person.errors # => []
}
text.html.markdown meta.paragraph.markdown: [31899, 31903], {```
}
text.html.markdown meta.paragraph.markdown: [31903, 31904], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [31904, 31907], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [31907, 31908], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [31908, 31909], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [31909, 31917], {errors[]}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [31917, 31918], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [31918, 31919], {
}
text.html.markdown: [31919, 31920], {
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [31920, 31921], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [31921, 31929], {errors[]}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [31929, 31930], {`}
text.html.markdown meta.paragraph.markdown: [31930, 32199], { is used when you want to check the error messages for a specific attribute. It returns an array of strings with all error messages for the given attribute, each string with one error message. If there are no errors related to the attribute, it returns an empty array.
}
text.html.markdown meta.paragraph.markdown: [32199, 32200], {
}
text.html.markdown meta.paragraph.markdown: [32200, 32208], {```ruby
}
text.html.markdown meta.paragraph.markdown: [32208, 32221], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [32221, 32222], {<}
text.html.markdown meta.paragraph.markdown: [32222, 32242], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [32242, 32300], {  validates :name, presence: true, length: { minimum: 3 }
}
text.html.markdown meta.paragraph.markdown: [32300, 32304], {end
}
text.html.markdown meta.paragraph.markdown: [32304, 32305], {
}
text.html.markdown meta.paragraph.markdown: [32305, 32343], {person = Person.new(name: "John Doe")
}
text.html.markdown meta.paragraph.markdown: [32343, 32367], {person.valid? # => true
}
text.html.markdown meta.paragraph.markdown: [32367, 32396], {person.errors[:name] # => []
}
text.html.markdown meta.paragraph.markdown: [32396, 32397], {
}
text.html.markdown meta.paragraph.markdown: [32397, 32429], {person = Person.new(name: "JD")
}
text.html.markdown meta.paragraph.markdown: [32429, 32454], {person.valid? # => false
}
text.html.markdown meta.paragraph.markdown: [32454, 32523], {person.errors[:name] # => ["is too short (minimum is 3 characters)"]
}
text.html.markdown meta.paragraph.markdown: [32523, 32524], {
}
text.html.markdown meta.paragraph.markdown: [32524, 32544], {person = Person.new
}
text.html.markdown meta.paragraph.markdown: [32544, 32569], {person.valid? # => false
}
text.html.markdown meta.paragraph.markdown: [32569, 32590], {person.errors[:name]
}
text.html.markdown meta.paragraph.markdown: [32590, 32657], { # => ["can't be blank", "is too short (minimum is 3 characters)"]
}
text.html.markdown meta.paragraph.markdown: [32657, 32661], {```
}
text.html.markdown meta.paragraph.markdown: [32661, 32662], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [32662, 32665], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [32665, 32666], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [32666, 32667], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [32667, 32677], {errors.add}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [32677, 32678], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [32678, 32679], {
}
text.html.markdown: [32679, 32680], {
}
text.html.markdown meta.paragraph.markdown: [32680, 32684], {The }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [32684, 32685], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [32685, 32688], {add}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [32688, 32689], {`}
text.html.markdown meta.paragraph.markdown: [32689, 32787], { method lets you manually add messages that are related to particular attributes. You can use the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [32787, 32788], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [32788, 32808], {errors.full_messages}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [32808, 32809], {`}
text.html.markdown meta.paragraph.markdown: [32809, 32813], { or }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [32813, 32814], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [32814, 32825], {errors.to_a}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [32825, 32826], {`}
text.html.markdown meta.paragraph.markdown: [32826, 32981], { methods to view the messages in the form they might be displayed to a user. Those particular messages get the attribute name prepended (and capitalized). }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [32981, 32982], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [32982, 32985], {add}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [32985, 32986], {`}
text.html.markdown meta.paragraph.markdown: [32986, 33078], { receives the name of the attribute you want to add the message to, and the message itself.
}
text.html.markdown meta.paragraph.markdown: [33078, 33079], {
}
text.html.markdown meta.paragraph.markdown: [33079, 33087], {```ruby
}
text.html.markdown meta.paragraph.markdown: [33087, 33100], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [33100, 33101], {<}
text.html.markdown meta.paragraph.markdown: [33101, 33121], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [33121, 33128], {  def a}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [33128, 33129], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [33129, 33135], {method}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [33135, 33136], {_}
text.html.markdown meta.paragraph.markdown: [33136, 33140], {used}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [33140, 33141], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [33141, 33144], {for}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [33144, 33145], {_}
text.html.markdown meta.paragraph.markdown: [33145, 33165], {validation_purposes
}
text.html.markdown meta.paragraph.markdown: [33165, 33232], {    errors.add(:name, "cannot contain the characters !@#%*()_-+=")
}
text.html.markdown meta.paragraph.markdown: [33232, 33238], {  end
}
text.html.markdown meta.paragraph.markdown: [33238, 33242], {end
}
text.html.markdown meta.paragraph.markdown: [33242, 33243], {
}
text.html.markdown meta.paragraph.markdown: [33243, 33279], {person = Person.create(name: "!@#")
}
text.html.markdown meta.paragraph.markdown: [33279, 33280], {
}
text.html.markdown meta.paragraph.markdown: [33280, 33301], {person.errors[:name]
}
text.html.markdown meta.paragraph.markdown: [33301, 33353], { # => ["cannot contain the characters !@#%*()_-+="]
}
text.html.markdown meta.paragraph.markdown: [33353, 33354], {
}
text.html.markdown meta.paragraph.markdown: [33354, 33382], {person.errors.full_messages
}
text.html.markdown meta.paragraph.markdown: [33382, 33439], { # => ["Name cannot contain the characters !@#%*()_-+="]
}
text.html.markdown meta.paragraph.markdown: [33439, 33443], {```
}
text.html.markdown meta.paragraph.markdown: [33443, 33444], {
}
text.html.markdown meta.paragraph.markdown: [33444, 33476], {Another way to do this is using }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [33476, 33477], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [33477, 33480], {[]=}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [33480, 33481], {`}
text.html.markdown meta.paragraph.markdown: [33481, 33489], { setter
}
text.html.markdown meta.paragraph.markdown: [33489, 33490], {
}
text.html.markdown meta.paragraph.markdown: [33490, 33498], {```ruby
}
text.html.markdown meta.paragraph.markdown: [33498, 33513], {  class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [33513, 33514], {<}
text.html.markdown meta.paragraph.markdown: [33514, 33534], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [33534, 33543], {    def a}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [33543, 33544], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [33544, 33550], {method}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [33550, 33551], {_}
text.html.markdown meta.paragraph.markdown: [33551, 33555], {used}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [33555, 33556], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [33556, 33559], {for}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [33559, 33560], {_}
text.html.markdown meta.paragraph.markdown: [33560, 33580], {validation_purposes
}
text.html.markdown meta.paragraph.markdown: [33580, 33646], {      errors[:name] = "cannot contain the characters !@#%*()_-+="
}
text.html.markdown meta.paragraph.markdown: [33646, 33654], {    end
}
text.html.markdown meta.paragraph.markdown: [33654, 33660], {  end
}
text.html.markdown meta.paragraph.markdown: [33660, 33661], {
}
text.html.markdown: [33661, 33699], {  person = Person.create(name: "!@#")
}
text.html.markdown: [33699, 33700], {
}
text.html.markdown: [33700, 33723], {  person.errors[:name]
}
text.html.markdown: [33723, 33777], {   # => ["cannot contain the characters !@#%*()_-+="]
}
text.html.markdown: [33777, 33778], {
}
text.html.markdown: [33778, 33799], {  person.errors.to_a
}
text.html.markdown: [33799, 33858], {   # => ["Name cannot contain the characters !@#%*()_-+="]
}
text.html.markdown meta.paragraph.markdown: [33858, 33862], {```
}
text.html.markdown meta.paragraph.markdown: [33862, 33863], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [33863, 33866], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [33866, 33867], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [33867, 33868], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [33868, 33881], {errors[:base]}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [33881, 33882], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [33882, 33883], {
}
text.html.markdown: [33883, 33884], {
}
text.html.markdown meta.paragraph.markdown: [33884, 34131], {You can add error messages that are related to the object's state as a whole, instead of being related to a specific attribute. You can use this method when you want to say that the object is invalid, no matter the values of its attributes. Since }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [34131, 34132], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [34132, 34145], {errors[:base]}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [34145, 34146], {`}
text.html.markdown meta.paragraph.markdown: [34146, 34235], { is an array, you can simply add a string to it and it will be used as an error message.
}
text.html.markdown meta.paragraph.markdown: [34235, 34236], {
}
text.html.markdown meta.paragraph.markdown: [34236, 34244], {```ruby
}
text.html.markdown meta.paragraph.markdown: [34244, 34257], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [34257, 34258], {<}
text.html.markdown meta.paragraph.markdown: [34258, 34278], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [34278, 34285], {  def a}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [34285, 34286], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [34286, 34292], {method}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [34292, 34293], {_}
text.html.markdown meta.paragraph.markdown: [34293, 34297], {used}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [34297, 34298], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [34298, 34301], {for}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [34301, 34302], {_}
text.html.markdown meta.paragraph.markdown: [34302, 34322], {validation_purposes
}
text.html.markdown meta.paragraph.markdown: [34322, 34340], {    errors[:base] }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [34340, 34341], {<}
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [34341, 34342], {<}
text.html.markdown meta.paragraph.markdown: [34342, 34380], { "This person is invalid because ..."
}
text.html.markdown meta.paragraph.markdown: [34380, 34386], {  end
}
text.html.markdown meta.paragraph.markdown: [34386, 34390], {end
}
text.html.markdown meta.paragraph.markdown: [34390, 34394], {```
}
text.html.markdown meta.paragraph.markdown: [34394, 34395], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [34395, 34398], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [34398, 34399], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [34399, 34400], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [34400, 34412], {errors.clear}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [34412, 34413], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [34413, 34414], {
}
text.html.markdown: [34414, 34415], {
}
text.html.markdown meta.paragraph.markdown: [34415, 34419], {The }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [34419, 34420], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [34420, 34425], {clear}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [34425, 34426], {`}
text.html.markdown meta.paragraph.markdown: [34426, 34503], { method is used when you intentionally want to clear all the messages in the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [34503, 34504], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [34504, 34510], {errors}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [34510, 34511], {`}
text.html.markdown meta.paragraph.markdown: [34511, 34543], { collection. Of course, calling }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [34543, 34544], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [34544, 34556], {errors.clear}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [34556, 34557], {`}
text.html.markdown meta.paragraph.markdown: [34557, 34615], { upon an invalid object won't actually make it valid: the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [34615, 34616], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [34616, 34622], {errors}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [34622, 34623], {`}
text.html.markdown meta.paragraph.markdown: [34623, 34681], { collection will now be empty, but the next time you call }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [34681, 34682], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [34682, 34688], {valid?}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [34688, 34689], {`}
text.html.markdown meta.paragraph.markdown: [34689, 34820], { or any method that tries to save this object to the database, the validations will run again. If any of the validations fail, the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [34820, 34821], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [34821, 34827], {errors}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [34827, 34828], {`}
text.html.markdown meta.paragraph.markdown: [34828, 34862], { collection will be filled again.
}
text.html.markdown meta.paragraph.markdown: [34862, 34863], {
}
text.html.markdown meta.paragraph.markdown: [34863, 34871], {```ruby
}
text.html.markdown meta.paragraph.markdown: [34871, 34884], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [34884, 34885], {<}
text.html.markdown meta.paragraph.markdown: [34885, 34905], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [34905, 34963], {  validates :name, presence: true, length: { minimum: 3 }
}
text.html.markdown meta.paragraph.markdown: [34963, 34967], {end
}
text.html.markdown meta.paragraph.markdown: [34967, 34968], {
}
text.html.markdown meta.paragraph.markdown: [34968, 34988], {person = Person.new
}
text.html.markdown meta.paragraph.markdown: [34988, 35013], {person.valid? # => false
}
text.html.markdown meta.paragraph.markdown: [35013, 35034], {person.errors[:name]
}
text.html.markdown meta.paragraph.markdown: [35034, 35101], { # => ["can't be blank", "is too short (minimum is 3 characters)"]
}
text.html.markdown meta.paragraph.markdown: [35101, 35102], {
}
text.html.markdown meta.paragraph.markdown: [35102, 35122], {person.errors.clear
}
text.html.markdown meta.paragraph.markdown: [35122, 35153], {person.errors.empty? # => true
}
text.html.markdown meta.paragraph.markdown: [35153, 35154], {
}
text.html.markdown meta.paragraph.markdown: [35154, 35172], {p.save # => false
}
text.html.markdown meta.paragraph.markdown: [35172, 35173], {
}
text.html.markdown meta.paragraph.markdown: [35173, 35189], {p.errors[:name]
}
text.html.markdown: [35189, 35255], {# => ["can't be blank", "is too short (minimum is 3 characters)"]
}
text.html.markdown meta.paragraph.markdown: [35255, 35259], {```
}
text.html.markdown meta.paragraph.markdown: [35259, 35260], {
}
text.html.markdown meta.block-level.markdown markup.heading.markdown punctuation.definition.heading.markdown: [35260, 35263], {###}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [35263, 35264], { }
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [35264, 35265], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown: [35265, 35276], {errors.size}
text.html.markdown meta.block-level.markdown markup.heading.markdown entity.name.section.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [35276, 35277], {`}
text.html.markdown meta.block-level.markdown markup.heading.markdown: [35277, 35278], {
}
text.html.markdown: [35278, 35279], {
}
text.html.markdown meta.paragraph.markdown: [35279, 35283], {The }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [35283, 35284], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [35284, 35288], {size}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [35288, 35289], {`}
text.html.markdown meta.paragraph.markdown: [35289, 35356], { method returns the total number of error messages for the object.
}
text.html.markdown meta.paragraph.markdown: [35356, 35357], {
}
text.html.markdown meta.paragraph.markdown: [35357, 35365], {```ruby
}
text.html.markdown meta.paragraph.markdown: [35365, 35378], {class Person }
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [35378, 35379], {<}
text.html.markdown meta.paragraph.markdown: [35379, 35399], { ActiveRecord::Base
}
text.html.markdown meta.paragraph.markdown: [35399, 35457], {  validates :name, presence: true, length: { minimum: 3 }
}
text.html.markdown meta.paragraph.markdown: [35457, 35461], {end
}
text.html.markdown meta.paragraph.markdown: [35461, 35462], {
}
text.html.markdown meta.paragraph.markdown: [35462, 35482], {person = Person.new
}
text.html.markdown meta.paragraph.markdown: [35482, 35507], {person.valid? # => false
}
text.html.markdown meta.paragraph.markdown: [35507, 35533], {person.errors.size # => 2
}
text.html.markdown meta.paragraph.markdown: [35533, 35534], {
}
text.html.markdown meta.paragraph.markdown: [35534, 35599], {person = Person.new(name: "Andrea", email: "andrea@example.com")
}
text.html.markdown meta.paragraph.markdown: [35599, 35623], {person.valid? # => true
}
text.html.markdown meta.paragraph.markdown: [35623, 35649], {person.errors.size # => 0
}
text.html.markdown meta.paragraph.markdown: [35649, 35653], {```
}
text.html.markdown meta.paragraph.markdown: [35653, 35654], {
}
text.html.markdown meta.paragraph.markdown: [35654, 35692], {Displaying Validation Errors in Views
}
text.html.markdown meta.paragraph.markdown markup.heading.2.markdown punctuation.definition.heading.markdown: [35692, 35729], {-------------------------------------}
text.html.markdown meta.paragraph.markdown: [35729, 35730], {
}
text.html.markdown: [35730, 35731], {
}
text.html.markdown meta.paragraph.markdown: [35731, 35811], {Once you've created a model and added validations, if that model is created via
}
text.html.markdown meta.paragraph.markdown: [35811, 35885], {a web form, you probably want to display an error message when one of the
}
text.html.markdown meta.paragraph.markdown: [35885, 35903], {validations fail.
}
text.html.markdown meta.paragraph.markdown: [35903, 35904], {
}
text.html.markdown meta.paragraph.markdown: [35904, 35981], {Because every application handles this kind of thing differently, Rails does
}
text.html.markdown meta.paragraph.markdown: [35981, 36056], {not include any view helpers to help you generate these messages directly.
}
text.html.markdown meta.paragraph.markdown: [36056, 36132], {However, due to the rich number of methods Rails gives you to interact with
}
text.html.markdown meta.paragraph.markdown: [36132, 36210], {validations in general, it's fairly easy to build your own. In addition, when
}
text.html.markdown meta.paragraph.markdown: [36210, 36266], {generating a scaffold, Rails will put some ERB into the }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [36266, 36267], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [36267, 36281], {_form.html.erb}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [36281, 36282], {`}
text.html.markdown meta.paragraph.markdown: [36282, 36288], { that
}
text.html.markdown meta.paragraph.markdown: [36288, 36354], {it generates that displays the full list of errors on that model.
}
text.html.markdown meta.paragraph.markdown: [36354, 36355], {
}
text.html.markdown meta.paragraph.markdown: [36355, 36428], {Assuming we have a model that's been saved in an instance variable named
}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [36428, 36429], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [36429, 36434], {@post}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [36434, 36435], {`}
text.html.markdown meta.paragraph.markdown: [36435, 36457], {, it looks like this:
}
text.html.markdown meta.paragraph.markdown: [36457, 36458], {
}
text.html.markdown meta.paragraph.markdown: [36458, 36466], {```ruby
}
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [36466, 36467], {<}
text.html.markdown meta.paragraph.markdown: [36467, 36493], {% if @post.errors.any? %>
}
text.html.markdown meta.paragraph.markdown: [36493, 36524], {  <div id="error_explanation">
}
text.html.markdown meta.paragraph.markdown: [36524, 36532], {    <h2>}
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [36532, 36533], {<}
text.html.markdown meta.paragraph.markdown: [36533, 36622], {%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
}
text.html.markdown meta.paragraph.markdown: [36622, 36623], {
}
text.html.markdown meta.block-level.markdown markup.raw.block.markdown: [36623, 36632], {    <ul>
}
text.html.markdown meta.block-level.markdown markup.raw.block.markdown: [36632, 36683], {    <% @post.errors.full_messages.each do |msg| %>
}
text.html.markdown meta.block-level.markdown markup.raw.block.markdown: [36683, 36709], {      <li><%= msg %></li>
}
text.html.markdown meta.block-level.markdown markup.raw.block.markdown: [36709, 36723], {    <% end %>
}
text.html.markdown meta.block-level.markdown markup.raw.block.markdown: [36723, 36733], {    </ul>
}
text.html.markdown: [36733, 36742], {  </div>
}
text.html.markdown meta.paragraph.markdown meta.other.valid-bracket.markdown: [36742, 36743], {<}
text.html.markdown meta.paragraph.markdown: [36743, 36752], {% end %>
}
text.html.markdown meta.paragraph.markdown: [36752, 36756], {```
}
text.html.markdown meta.paragraph.markdown: [36756, 36757], {
}
text.html.markdown meta.paragraph.markdown: [36757, 36833], {Furthermore, if you use the Rails form helpers to generate your forms, when
}
text.html.markdown meta.paragraph.markdown: [36833, 36897], {a validation error occurs on a field, it will generate an extra }
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [36897, 36898], {`}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown: [36898, 36903], {<div>}
text.html.markdown meta.paragraph.markdown markup.raw.inline.markdown punctuation.definition.raw.markdown: [36903, 36904], {`}
text.html.markdown meta.paragraph.markdown: [36904, 36912], { around
}
text.html.markdown meta.paragraph.markdown: [36912, 36923], {the entry.
}
text.html.markdown meta.paragraph.markdown: [36923, 36924], {
}
text.html.markdown meta.paragraph.markdown: [36924, 36928], {```
}
text.html.markdown meta.paragraph.markdown: [36928, 36945], {<div class="field}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [36945, 36946], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [36946, 36950], {with}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [36950, 36951], {_}
text.html.markdown meta.paragraph.markdown: [36951, 36960], {errors">
}
text.html.markdown meta.paragraph.markdown: [36960, 37035], { <input id="post_title" name="post[title]" size="30" type="text" value="">
}
text.html.markdown meta.paragraph.markdown: [37035, 37042], {</div>
}
text.html.markdown meta.paragraph.markdown: [37042, 37046], {```
}
text.html.markdown meta.paragraph.markdown: [37046, 37047], {
}
text.html.markdown meta.paragraph.markdown: [37047, 37121], {You can then style this div however you'd like. The default scaffold that
}
text.html.markdown meta.paragraph.markdown: [37121, 37171], {Rails generates, for example, adds this CSS rule:
}
text.html.markdown meta.paragraph.markdown: [37171, 37172], {
}
text.html.markdown meta.paragraph.markdown: [37172, 37176], {```
}
text.html.markdown meta.paragraph.markdown: [37176, 37182], {.field}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [37182, 37183], {_}
text.html.markdown meta.paragraph.markdown markup.italic.markdown: [37183, 37187], {with}
text.html.markdown meta.paragraph.markdown markup.italic.markdown punctuation.definition.italic.markdown: [37187, 37188], {_}
text.html.markdown meta.paragraph.markdown: [37188, 37197], {errors {
}
text.html.markdown meta.paragraph.markdown: [37197, 37213], {  padding: 2px;
}
text.html.markdown meta.paragraph.markdown: [37213, 37238], {  background-color: red;
}
text.html.markdown meta.paragraph.markdown: [37238, 37256], {  display: table;
}
text.html.markdown meta.paragraph.markdown: [37256, 37258], {}
}
text.html.markdown meta.paragraph.markdown: [37258, 37262], {```
}
text.html.markdown meta.paragraph.markdown: [37262, 37263], {
}
text.html.markdown meta.paragraph.markdown: [37263, 37337], {This means that any field with an error ends up with a 2 pixel red border.}