Rails Common validation methods

Source: Internet
Author: User

Validates_presence_of:login,: message = "User name cannot be empty!"

Validates_length_of:login,: Minimum = 4,: message = "Username must be 4 to 20 letters or numbers!" Validates_uniqueness_of:login,:case_sensitive = False,: message = "The user name already exists!"

Validates_presence_of:p assword,: message = "Password cannot be empty!"
Validates_length_of:p assword,: minimum = 6,:message=> "Password must be 6 to 20 letters or numbers! "
Validates_presence_of:p assword_confirmation,: message = "Please enter your password again!"
Validates_confirmation_of:p assword,: message = "two times password inconsistent!"

Validates_format_of:email,: Message "The mailbox format is incorrect!",: with =/\a ([^@\s]+) @ (?: [-a-z0-9]+\.) +[a-z]{2,}) \z/i

Here are 5 main validation grammars, each of which is described below:

1, validates_presence_of--confirm that the attribute value is not nil or empty.

Usage: validates_length_of attr ..., [Options ...]

Options:

: Message "=" is the default "is can ' t was blank."
: On =>:save,: Create, or: Update

2, validates_length_of--confirm the length of the property value. Follow some constraints: give at least one length (such as minimum length: minimum, maximum length: maximum, or an interval: in Or:within, but these three can only be selected, the length cannot be negative), and cannot be single: The message option, This confirmation allows separate messages for different acknowledgment failures, as long as: message can also be used.

Usage: validates_length_of attr ..., [Options ...]

Example:

Validates_length_of:name,: maximum = #这个时候可以自定义: Message
Validates_length_of:p Assword,: In + = 6..20 #这个时候采用默认的: message, ignoring custom content
validates_length_of:address, Minimum: message = "seems too short"

Options:
: In (or: within) = The length of the value must be within one range.
: is = = integer, the value must be the character length of an integer.
: Minimum = = is an integer, and the value cannot be less than this integer.
:maximum=> is an integer, and the value cannot be greater than this integer.
: Message = = is a text that can contain a sequence of%d that will be maximun,minimum, or determined to be a length instead.
:on=>: Save,: Create, or: Update
: Too_long = = is a text, using: Maximum: message synonym.
: Too_short = = is a text, using: Minimum: message synonym.
: Wrong_length + = is a text, using: is when: message synonym.

3, validates_uniqueness_of--confirm that the attribute is unique. For each property, verify that the other rows in the database do not currently have the same value as the given column.
Usage: validates_uniqueness_of attr ... [Options ...]

Options:
: Message = default is "has already been taken."
: On =>:save,: Create, or: Update

: scope = attr Limits The check to rows has the same value in the column as the row being checked.

4. validates_confirmation_of--Confirm that the field and its value have the same content. Many forms require the user to enter the same information two times (such as confirmation password) If you use a naming convention where the second field name is appended with _confirmation, you can use Validates_confirmation_of () to check whether two fields have the same value.

Usage: validates_confirmation_of attr ... [Options ...]

Options:
: message = The default is "doesn ' t match confirmation."
: On =>:save,: Create, or: Update

5, validates_format_of--in a mode to confirm the attribute. Each field is confirmed by matching its value with the regular expression.

Usage: validates_format_of attr ...,: with + = RegExp [Options ...]

Options:
: Message = = default is ' is invalid. '
: On =>:save,: Create, or: Update

In addition, there are some validations:

6. validates_acceptance_of--Confirm if checkbox is marked. Many forms have a checkbox, and the user must choose to accept some terms or conditions. This confirmation simply verifies that the box has been confirmed to be marked, and that the attribute value is a string. The property itself is not stored in the database (if you want to explicitly record the acknowledgment, nothing will prevent you from doing so).

Usage: validates_acceptance_of attr ... [Options ...]
Example:

Validates_acceptance_of:terms,: message = "Please accept the terms to proceed"


Options:

: message = The default is "must be accepted."
: On =>:save,: Create, or: Update

7, validates_associated--on the associated object to complete the confirmation. Completes the acknowledgment on the given attribute, which is assumed to be the activity record model. For each acknowledgment associated with a property to fail, a single message will be added to the error List of that property (that is, the failure that occurs for the individual details cause will not be written to the "model" Error List). Be careful not to include a validates_associated () call in the model that is referenced by each other: the first one will try to confirm the second one, which in turn confirms the first one, and so on, directly to your stack overflow.

Usage: validates_associated name ... [Options ...]

Example:

Class Order < ActiveRecord::Base
Has_many:line_items
Belongs_to:user
Validates_associated:line_items,: Message = "is messed up"
Validates_associated:user
End


Options:

: Message = = default is ' is invalid. '
: On =>:save,: Create, or: Update

8. validates_each--uses a block to confirm one or more attributes. Call the block for each property (if: Allow_nil is true, skip the attribute that is nil). The name of the passed property, the value of the attribute to the "model" in the confirmation. As shown in the following example, if a confirmation fails, the block should be added to the "model" Error List

Usage: Validates_each attr ... [Options ...] {|model, attr, value| ...}

Example:

Class User < ActiveRecord::Base
Validates_each:name,: Email do |model, attr, value|
If Value =~/groucho|harpo|chico/i

Model.errors.add (attr, "You can ' t be serious, #{value}")
End
End
End

Options:

: Allow_nil = = Boolean, if: Allow_nil is true, attributes with a value of nil are not passed to the block but are skipped.

: On =>:save,: Create, or: Update

9. validates_exclusion_of--confirm that the attribute is not in a set of values. Verify that the attribute does not appear in the enumeration (any object supports include?) Assertion).
Usage: validates_exclusion_of attr ...,: in = enum [Options ...]

Example:

Class User < ActiveRecord::Base
Validates_exclusion_of:genre,:in =%w{Polka Twostep Foxtrot},

: Message = "No wild Music Allowed"
Validates_exclusion_of:age,: in = = 13..19,: message = "cannot be a teenager"
End


Options:
: Allow_nil = = If the property is nil, and the: Allow_nil option is true. The enumeration is not checked.
: In (or: within) = = An enumerable object.
: Message = = default is ' is invalid. '
: On =>:save,: Create, or: Update

10. validates_inclusion_of--confirm whether the attribute belongs to a set of values. Verify that the value of each property appears in the enumeration (any object supports include?) Assertion).
Usage: validates_inclusion_of attr ...,: in = enum [Options ...]

Example:

Class User < ActiveRecord::Base
Validates_inclusion_of:gender,:in =%w{Male Female},

: Message = "should be ' male ' or ' female '"
Validates_inclusion_of:age,:in = 0..130,: message = "should be between 0 and 130"
End


Options:
: Allow_nil = = If the property is nil, and the: Allow_nil option is true. The enumeration is not checked.
: In (or: within) = = An enumerable object.
: Message "=" is the default "is isn't included in the list."
: On =>:save,: Create, or: Update

11, validates_numericality_of--confirm that the attribute is a valid number. Verify that each property is a valid number. In the: Only_integer option, the attribute must be followed by one or more digits after an optional symbol. In the options (or if the option is not true), any floating-point number that can be allowed by the Ruby float () method is accepted.

Usage: validates_numericality_of attr ... [Options ...]
Example:

Class User < ActiveRecord::Base
Validates_numericality_of:height_in_meters
Validates_numericality_of:age,: Only_integer = True
End


Options:
: Message = = default is ' not a number. '
:on=>: Save,: Create, or: Update
: Only_integer + = If true, the property must be a string that contains an optional symbol followed by a number.

Rails Common authentication Method (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.