Thinkphp Learning Notes (10) perform automatic validation of the form data in the front desk in model ____php

Source: Internet
Author: User

Index.html

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >


UserModel.class.php

<?php
	class Usermodel extends model{
		//Parameters:
		//1. Validation fields (names in the form or auxiliary fields such as Authenticode)
		//2. Validation rules (used with additional rules)
		//3. Error reminder
		//4. Validation conditions (0:1:2:) It is best to select constants in the manual to fill
		//5. Additional rules; authentication method: Regex;function name; callback;confirm (verify two fields are the same Equal;in;unique
//				common rules: require: must exist; email mailbox; url;currency currency; number;
		//6. Verification time
//				Refers to the database when the database operation of the verification time (add data, edit, all clear under Validation)
//				Model::model_insert
//				model::model_update
/ /				Model::model_both//
		automatic verification start
		protected $_validate=array (
			' username ', ' require ', ' username required ') , 0,0,1),
			Array (' username ', ' checklen ', ' username length not valid ', 0, ' callback ', 3),
			Array (' Password ', ' require ', ' username required ', 0,0,1),
			array (' Repassword ', ' require ', ' username required ', 0,0,1),
			Array (' Password ', ' Repassword ', ' inconsistent password ', 0, ' Confirm ', 1),
//			Array (' Createip ', ' email ', ' mailbox format not correct ', 0, ' regex ', 1)
		;
		Public Function Checklen ($data) {
			if (strlen ($data) >15| | Strlen ($data) <5) return
				FALSE;
			return true;
		}
		function Modeltest () {
			echo ' test cross model operation, calling method ';} ' in the
		model
? >

AutoAction.class.php

 * Automatic validation in <?php/** * thinkphp
 (takes effect when create method in action)
 * 		mainly written in custom model, validate the form or field in the database when you finish entering information
 * Auto-complete
 * The 		field that the user entered is not the user's manual input *
 field mapping
 * Prevents the 		user from seeing the field name from the foreground and guessing the name of the field in the database table
 *
* */ Class Autoaction extends action{public
	function Index () {
		$this->display ();
	}
	function Add () {
		//Custom model
		$user =d (' user ');
		if ($user->create ()) {
			if ($user->add ()) {
				$this->success ("registered successfully");
			} else{
				$this->error ($user->geterror ());
			}
		else{
			$this->error ($user->geterror ());}}
? >


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.