Php--yii Framework Form Validation

Source: Internet
Author: User

When generating a form from a form widget at the view level, field can only exist in the database, for example: use yii\helpers\html; Use Yii\widgets\activeform; Use Yii\captcha\captcha; ' Sign-form ', ' Options ' =>[' action ' = ' usermessage/signform ', ' method ' = ' post ', ' enctype ' = ' multipart/' Form-data ']);?> Field ($model, ' username ')?> Field ($model, ' password ')->passwordinput ()?> Field ($model, ' Repassword ')->passwordinput ()?> Field ($model, ' age ')?> Field ($model, ' sex ')?> Field ($model, ' phone ')?> Field ($model, ' Captcha ')->widget (Captcha::classname (), [' captchaaction ' = ' usermessage/captcha ', ' template ' = > ' {image}{input} ',]?> Field ($model, ' email ')?> Field ($model, ' userimg ')->fileinput ()?> ' Btn btn-primary '])?> In this case, we do not need to design the database to confirm the password and Verification code fields, then we need to define the corresponding attributes in the model layer: public $repassword; Public $userimg; Public $captcha;p ublic $phone; Define validation rules in the model Layer Rules:public function rules () {return [[' username ', ' password ', ' Repassword ', ' Captcha ', ' age ', ' sex ', ' phone ', ' email ', ' filter ', ' filter ' = ' trim ', ' on ' = ' register '], [[' Username ', ' Password ', ' Repassword ', ' captcha ', ' age ', ' sex ', ' phone ', ' email ', ' required ', ' message ' = ' {attribute} cannot be empty '], [[' Username '], ' match ', ' pattern ' = '/^\w{6,20}$/', ' message ' = ' {attribute} ' for 6-20 digit letters or underscores '],//[' username ', ' Unique ', ' targetclass ' = ' \common\models\user ', ' message ' = ' This username have already been taken '],//[[' age ', ' Number ', ' integeronly ' =>true, ' Max ' =>150, ' min ' =>18, ' toobig ' + ' {attribute} can only be within 18-150 integers ', ' toosmall ' = > ' {attribute} can only be within 18-150 integers '], [[' Password '], ' match ', ' pattern ' = '/^[a-z_]\w{5,19}$/', ' message ' = ' { attribute} is a 6-20 digit letter or underscore, cannot start with a number '], [' Repassword ', ' compare ', ' compareattribute ' = ' password ', ' message ' =>‘ Two times input password inconsistent '], [' Sex ', ' in ', ' range ' =>[' male ', ' female ', ' message ' = ' {attribute} can only be male or female '], [[' Phone '], ' match ', ' pattern ' = > '/^ (13|15|18) [0-9]{9}$/', ' message ' and ' {attribute} ' can only be 11 digits from the beginning of 13,15,18 "],//[' phone ', ' checkphone '], [[' Email ', ' userimg '], ' string ', ' max ' = + ', [' email ', ' email ', '//[' email ', ' unique '], [' userimg ', ' file ', ' Skiponempty ' =&gt ; false, ' extensions ' = ' png,jpg,gif '], [' Captcha ', ' captcha ', ' message ' = ' = ' Please enter correctly {attribute} ', ' captchaaction ' = > ' Usermessage/captcha '],];} Parse: Filter: Filters, ' filter ' = ' trim ', which means go to space required: required, indicates cannot be empty match: match regular, need to use with pattern, define regular expression, ' pattern ' = '/^\w {6,20}$/', unique: Verify the uniqueness of the data, in the registration of more than used, it is important to note that in the rules rule is defined by the uniqueness of authentication, only on the server side to verify, if you want to display on the form page, you need to open the " Enableajaxvalidation "=>ture; For example: ' Sign-form ',//' enableajaxvalidation ' + true,//enable AJAX validation, send attribute values to server side for validation and return results, default to ' Enableclientvalidation ' + = true,//Enable client-side validation, the default value is True, closed after form without JS authentication ' options ' =>[' action ' = ' usermessage/signform ', ' method ' = ' post ', ' Enctype ' = ' multipart/form-data ']);?> here, it's important to note that Ajax validation works on all of the properties here, so there's another way to open it in a field: Field ($model, ' username ', [' enableajaxvalidation ' =>true])->textinput (), which acts on the username attribute alone. To implement the form AJAX authentication uniqueness, the background also need an AJAX judgment: $model->load (Yii:: $app->request->post ()); if (yii:: $app->request->isajax) {yii:: $app->response->format = \yii\web\response::format_json; return \ Yii\bootstrap\activeform::validate ($model); It is best to perform $model->load (Yii:: $app->request->post ()) when there is data submission; Operation, do not do extra processing, and then Judge Ajax, otherwise Ajax validation may error 500. If there is a verification code, there is another problem: return \yii\bootstrap\activeform::validate ($model); this validates all the attributes, and the verification code is regenerated after the validate is executed. Then when the form submitted when we do data validation error, the solution: \yii\bootstrap\activeform::validate () This method actually has two parameters, $model, $attributes, We can specify AJAX to verify certain properties, such as: \yii\bootstrap\activeform::validate ($model, [' username ', ' email ', ' phone ']); In this way, the AJAX validation validates only the Username,email,phone three fields, without affecting the verification code. Number: Digital verification, plus ' integeronly ' =>true, means only integers, max,min respectively represents the maximum minimum, toobig and Toosmall are error messages that exceed the maximum and below the minimum value compare: comparison, For the comparison between the two properties, ' compareattribute ' = ' password ', which represents a comparison with password in: And range, defines the range, which indicates that the value of the property must be in this range, typically used to verify certain fixed values email: Mailbox Verification File: Document Validation Extensions can define the type of upload file Captcha: Verification code verification, need to define the method to generate code, ' captchaaction ' = ' Usermessage/captcha ', Usermessage represents the controller name, captcha means that the method name can define an actions method in the controller layer to add Captcha method:/** * method to generate verification code */Public function actions () {parent::actions (); return [' captcha ' = ' = ' class ' = ' yii\captcha\captchaaction ',//' Fixedveri Fycode ' = yii_env_test? ' Testme ': null, ' maxLength ' = 3, ' minLength ' = 3],]; Each validation can add a scenario: ' on ' = ' register '; instantiate the model layer at the controller layer $model = new usermessage (); $model->setscenario (' register '); Defines an object that uses a scenario for the register to define the role of the scene in the model layer/** * Define the validation scenario */Public Function scenarios () {return [' register ' = = [' username ', ' pas Sword ', ' Repassword ', ' age ', ' sex ', ' phone ', ' email ', ' login ' = ' [' username ', ' password ', ' age ', ' sex ', ' phone ', ' email ' ‘], ];} The application scenario ' on ' = ' register ' is then defined after the corresponding validation rule; When the form is validated, the parameters that are within the action scenario can be added to the library without the restriction of the validation rule: The check box is an array after it is submitted, so the Save () is executed. Before you need to process the value of the check box into a string

Php--yii Framework Form validation

Related Article

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.