Form validation in Yii

Source: Internet
Author: User
Tags dateformat

There are three types of validation for forms:

1. Client Authentication of Yii

2. Server-side verification of Yii

3. Yii Ajax Verification

For example:

1. Define a rules method in the model corresponding to the form (the party will be automatically called when the form is submitted)

Public Function Rules ()

{

Return Array (

Array ("FORM element name is field name", "Validate Class", "message" = "hint Message"),

Array ("FORM element name is field name", "Validate Class", "message" = "hint Message"),

Array ("FORM element name is field name", "Validate Class", "message" + "hint message")

);

}

2. You need to add the following attributes to the form on the view page

<?php

$form = $this->beginwidget ("Cactiveform", Array (

"Action" = "Index.php?r=user/reg",

"Method" = "POST",

"Enableclientvalidation" =>true,//whether to use client-side validation

"Clientoptions" = = Array (//Client Authentication selection

' Validateonsubmit ' + true,//Whether validation occurs on form submission

),

"Htmloptions" =>array (

"Name" = "frm",

"Enctype" = "Multipart/form-data"

)

));

?>

3. The view page displays error messages

<?php echo $form->error (Model object, "form element name is also field name")?>

The following is the corresponding validation class

Required:crequiredvalidator

Filter:cfiltervalidator

Match:cregularexpressionvalidator

Email:cemailvalidator

Url:curlvalidator

Unique:cuniquevalidator

Compare:ccomparevalidator

Length:cstringvalidator

In:crangevalidator

Numerical:cnumbervalidator

Captcha:ccaptchavalidator

Type:ctypevalidator

File:cfilevalidator

Default:cdefaultvaluevalidator

Exist:cexistvalidator

Boolean:cbooleanvalidator

Date:cdatevalidator

Safe:csafevalidator

Unsafe:cunsafevalidator

1. crequiredvalidator– must be value validation property

requiredvalue-mixed-the desired value

Is the strict-boolean-more stringent?

Example: Array (' username ', ' required '), cannot be empty

Array (' username ', ' required ', ' requiredvalue ' = ' lh ', ' message ' = ' usernmae must be LH '), this value must be LH, if the other values are not verified but

Array (' username ', ' required ', ' requiredvalue ' = ' lh ', ' strict ' =>true), strictly verified can also be appended with ' message ' and ' = ' and ' on ' = these

2. Cfiltervalidator Filter Validation Properties

filter– Method Name (call user-defined function)

Instance:

Array (' username ', ' test ') function test () {$username = $this->username; if ($username! = ' lh ') {$this->adderror (' Us Ername ', ' username must be LH '); } }

Use this method if you still write message=> in the array, the message given is still in your test. Which is the error message in test.

3, Cregularexpressionvalidator-

Regular verifies whether the property allowempty– is empty (default true)

not-whether to reverse the validation logic (default false) pattern– regular expression matches an instance:

Match A-Z array (' username ', ' match ', ' AllowEmpty ' =>true, ' pattern ' = '/[a-z]/i ', ' message ' = ' must be a letter '),

Match is not A-Z array (' username ', ' match ', ' AllowEmpty ' =>true, ' not ' =>true, ' pattern ' = '/[a-z]/i ', ' message ' = = ' must not be a letter '),

4. cemailvalidator– Mailbox Validation Properties:

Whether the allowempty– is empty

allowname– whether the name of the e-mail address is allowed

checkmx– check MX records for email addresses

checkport– If you want to check the e-mail address of Port 25

fullpattern– regular expression, used to verify the e-mail address and part of the name

pattern– Regular Expressions,

An instance of the attribute value used to verify: Array (' username ', ' email ', ' message ' = ' e-mail ', ' pattern ' = '/[a-z]/i '),

5. Curlvalidator–url Validation Properties:

Whether the allowempty– is empty

defaultscheme– the default URI scheme

pattern– Regular Expressions

The validschemes– list should be considered a valid URI plan.

Instance:

Array (' username ', ' url ', ' message ' = ' must URL '),

Array (' username ', ' url ', ' defaultscheme ' = ' http://www.baidu.com '),

6. cuniquevalidator– Uniqueness Validation attribute:

Whether the allowempty– is empty

attributename– Property Name

casesensitive– Case Sensitive

classname– class Name

criteria– Additional query conditions

Instance:

Array (' username ', ' unique ', ' message ' = ' The record exists '),

Array (' username ', ' unique ', ' casesensitive ' =>false, ' message ' = ' The record exists '),

7. ccomparevalidator– Compare Validation Properties:

Whether the allowempty– is empty

compareattribute– properties that need to be compared

CompareValue-The value of the comparison

operator– comparison Operators

strict– rigorous validation (both values and types are equal)

Example://Compare with a value array (' username ', ' compare ', ' comparevalue ' = ' 10′ ', ' operator ' = ' > ', ' message ' = ' = ' must be greater than 10′ '),

Compare with a submitted attribute array (' username ', ' compare ', ' compareattribute ' = ' password ', ' operator ' = ' > ', ' message ' = ' must be greater than password '),

8. cstringvalidator– String Validation Properties:

Whether the allowempty– is empty

encoding– encoding

is– the exact length

max– Maximum Length

min– min. length

toolong– error defining a value that is too large

tooshort– defines the minimum length error

Example: Array (' username ', ' length ', ' Max ' =>10, ' min ' =>5, ' toolong ' = ' too long ', ' tooshort ' = ' too Short '),

Array (' username ', ' length ', ' is ' =>5, ' message ' = ' = ' length must be 5′),

9. crangevalidator– within a range of attributes:

Whether the allowempty– is empty

not– whether the validation logic is reversed.

Range–array Range

strict– rigorous validation (same type and value)

Example: Array (' username ', ' in ', ' Range ' =>array (1,2,3,4,5), ' message ' = ' must in 1 2 3 4 5′ '),

Array (' username ', ' in ', ' not ' =>true, ' range ' =>array (1,2,3,4,5) ', ' message ' = ' must ' in 1 2 3 4 5′),

10. cnumbervalidator– Digital Validation Properties:

Whether the allowempty– is empty

integeronly– integer

integerpattern– Regular Expressions Match integers

max– Maximum Value

min– Minimum value

numberpattern– Match number

Error message when the toobig– value is too large

toosmall– error message with too-small value

Example: Array (' username ', ' numerical ', ' integeronly ' =>true, ' message ' = ' must be int '),

Array (' username ', ' numerical ', ' integeronly ' =>true, ' message ' = ' must be int ', ' Max ' =>100, ' min ' =>10, ' Toobig ' = ' is too big ', ' toosmall ' = ' is too small '),

11, ccaptchavalidator– Verification Code verification properties:

Whether the allowempty– is empty

casesensitive– Case Sensitive

12. ctypevalidator– Type Validation Properties:

Whether the allowempty– is empty

dateformat– date should follow the format pattern (' mm/dd/yyyy ')

datetimeformat– format pattern (' mm/dd/yyyy hh:mm ') to be followed by date time

timeformat– time should follow the format pattern (' hh:mm ')

type– type ' string ', ' integer ', ' float ', ' array ', ' Date ', ' Time ' and ' datetime '

Example: Array (' username ', ' type ', ' dateformat ' = ' mm/dd/yyyy ', ' type ' = ' date '),

13. cfilevalidator– File Validation Properties:

Whether the allowempty– is empty

maxfiles– Maximum number of files

Maximum value of the maxsize– file

minsize– Minimum value

Error message when the toolarge– is too large

toomany– Too much error message

toosmall– error message for too hours

types– allowed file name extensions

Error message given when wrongtype– extension error

14. cdefaultvaluevalidator– Default Value property:

setonempty– set to NULL

value– Default Value

Example: Array (' username ', ' default ', ' Setonempty ' =>true, ' value ' = ' lh '),

15. cexistvalidator– whether there are attributes:

AllowEmpty = is empty

attributename– Property Name

classname– class Name

criteria– Standard

16. cbooleanvalidator– Boolean Type validation property:

Whether the allowempty– is empty

falsevalue– the value of the error state

strict– rigorous validation

truevalue– Value of real state

Example: Array (' username ', ' boolean ', ' TrueValue ' =>1, ' falsevalue ' =>-1, ' message ' = ' = ' The value must be 1 or-1′),

17. cdatevalidator– Date Validation attribute:

Whether the allowempty– is empty

format– format pattern to which date values should be followed

timestampattribute– property names that receive parsing results

Example: Array (' username ', ' date ', ' format ' = ' mm-dd-yyyy ', ' message ' = ' must be mm-dd-yyyy '),

Form validation in Yii

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.