Common rails verification methods

Source: Internet
Author: User

Validates_presence_of : Login ,: Message => "the user name cannot be blank! "
Validates_length_of : Login ,: Minimum => 4,: Message => "the length of the user name must be 4 to 20 letters or numbers! "Validates_uniqueness_of : Login ,: Case_sensitive => false,: Message => "this user name already exists! "

Validates_presence_of : Password ,: Message => "the password cannot be blank! "
Validates_length_of: Password ,: Minimum => 6,: Message => "the password length must be 6 to 20 letters or numbers! "
Validates_presence_of : Password _ Confirmation ,: Message => "enter the password again! "
Validates_confirmation_of : Password ,: Message => "the two passwords are inconsistent! "

Validates_format_of : Email ,: Message => "Incorrect email format! ",: With =>/\ A ([^ @ \ s] + )@((? : [-A-z0-9] + \.) + [A-Z] {2,}) \ Z/I

 

Five major verification syntaxes are used here, which are described below:

1. validates_presence_of -- make sure that the attribute value is not nil or empty.

Usage: validates_length_of ATTR..., [option...]

Option:

: Message => the default value is "is can't be blank ."
: On =>: Save,: create, or: Update

 

2. validates_length_of -- confirm the length of the attribute value. Follow some constraints: give at least one length (such as the minimum length): Minimum, Maximum length: Maximum, Or an interval: In or:But only one of the three can be selected, and the length cannot be negative), instead of only one: Message option, this validator allows for separation of messages for different validation failures, as long as: message can also be used.

Usage: validates_length_of ATTR..., [option...]

Example:

Validates_length_of: Name,:Maximum=> 50 # at this time, you can customize the message.
Validates_length_of: password, : In=> 6 .. 20 # use the default message to ignore custom content.
Validates_length_of: address,: Minimum=> 10,: Message=> "Seems too short"

 

Option:
: In (or: Within) => the length of the value must be within the same range.
: Is => integer. The value must be an integer in length.
: Minimum => is an integer. The value cannot be smaller than this integer.
: Maximum => is an integer. The value cannot be greater than this integer.
: Message => is a text message that can contain a sequence of % d that will be replaced by maximun, minimum, or a fixed length.
: On =>: Save,: create, or: Update
: Too_long => is a text. It is a message synonym when maximum is used.
: Too_short => is a text, and the message synonym is used when: Minimum is used.
: Wrong_length => is a text, which is the synonym of message when: Is is used.

 

 

3. validates_uniqueness_of -- confirm that the attribute is unique. For each attribute, check that other rows in the database do not have the same value as the given column.
Usage: validates_uniqueness_of ATTR... [option...]

Option:
: Message => the default value is "has already been taken ."
: On =>: Save,: create, or: Update

: Scope => ATTR limits the check to rows having the same value in the column as the row being checked.

 

4. validates_confirmation_of -- confirm that the field has the same content as its value. Many forms require users to enter the same information twice (for example, to confirm the password). If you use the naming convention that the name of the second field is attached with _ confirmation, you can use validates_confirmation_of () to check whether the two fields have the same value.

Usage: validates_confirmation_of ATTR... [option...]

Option:
: Message => the default value is "doesn' t match confirmation ."
: On =>: Save,: create, or: Update

 

5. validates_format_of -- confirm the attribute in one mode. Confirm each field by matching its value with the regular expression.

Usage: validates_format_of ATTR...,: With => Regexp [option...]

Option:
: Message => the default value is "is invalid ."
: On =>: Save,: create, or: Update

 

There are also some verifications as follows:

 

6. validates_acceptance_of -- check whether the checkbox is marked. Many forms have checkbox, and users must select to accept certain terms or conditions. This validation simply checks that the box has been identified and the attribute value is a string. Attributes themselves are not stored in the database (if you want to explicitly record and confirm, nothing will stop you from doing so ).

Usage: validates_acceptance_of ATTR... [option...]
Example:

Validates_acceptance_of: Terms,: Message => "please accept the terms to proceed"

Option:

: Message => the default value is "must be accepted ."
: On =>: Save,: create, or: Update

7. validates_associated -- confirm the associated object. The validation is completed on the given attribute, which is assumed to be an "activity record model ". If you fail to confirm the association of each attribute, a single message will be added to the error list of that attribute (that is, failure due to some specific details, will not be written to the "model" Error List ). Be careful not to include a validates_associated () call in the "model" referenced by each other: the first will try to confirm the second, it will confirm the first one in turn, and so on, directly your stack overflows.

Usage: validates_associated name... [option...]

Example:

Class order <activerecord: Base
Has_ EMS: line_items
Belongs_to: User
Validates_associated: line_items,: Message => "are messed up"
Validates_associated: User
End

Option:

: Message => the default value is "is invalid ."
: On =>: Save,: create, or: Update

 

8. validates_each -- use a block to confirm one or more attributes. Call the block for each attribute (if: allow_nil is true, the NIL attribute is skipped ). Pass the attribute name. The attribute value is included in the confirmed "model. As shown in the following example, if a validation fails, the block should be added to the error list of the "model ".

Usage: validates_each ATTR... [option...] {| 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

Option:

: Allow_nil => Boolean value. If: allow_nil is true, attributes with a value of Nil will not be passed to the block, but will be skipped.

: On =>: Save,: create, or: Update

 

9. validates_exclusion_of -- confirm that the attribute is not in a group of values. Confirm that the property is not in the enumeration (any object supports include? () Assertions ).
Usage: validates_exclusion_of ATTR...,: In => Enum [option...]

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


Option:
: Allow_nil => If the attribute is nil and the: allow_nil option is true. The enumeration is not checked.
: In (or: Within) => an enumerated object.
: Message => the default value is "is invalid ."
: On =>: Save,: create, or: Update

 

10. validates_inclusion_of -- check whether the attribute belongs to a value set. Check whether the values of each attribute appear in enumeration (any object supports include? () Assertions ).
Usage: validates_inclusion_of ATTR...,: In => Enum [option...]

Example:

Class user <activerecord: Base
Validates_inclusion_of: Gender,: In =>% W {Male Female },

: Message => "shocould be 'male' or 'female '"
Validates_inclusion_of: age,: In => 0 .. 130,: Message => "shocould be between 0 and 130"
End


Option:
: Allow_nil => If the attribute is nil and the: allow_nil option is true. The enumeration is not checked.
: In (or: Within) => an enumerated object.
: Message => the default value is "is not supported in the list ."
: On =>: Save,: create, or: Update

 

 

11. validates_numericality_of -- confirm that the attribute is a valid number. Make sure that each attribute is a valid number. In the only_integer option, the attribute must be followed by one or more numbers by an optional symbol. In options (or if the option is not true), any floating point number allowed by the ruby float () method is accepted.

Usage: validates_numericality_of ATTR... [option...]
Example:

Class user <activerecord: Base
Validates_numericality_of: height_in_meters
Validates_numericality_of: age,: only_integer => true
End

Option:
: Message => the default value is "is not a number ."
: On =>: Save,: create, or: Update
: Only_integer => if it is true, the attribute must be a string containing an optional Symbol and followed by a number.

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.