Validating a form registered to the test table
To validate the form before registering:
User name non-null authentication, two times the input password must be consistent that is equal authentication, the age between 18~50 is range authentication, the mailbox format is verified.
Automatic validation is a method of data validation provided by the Thinkphp model layer that automates data validation when creating data objects using create.
Data validation enables validation of data types, business rules, security judgments, and more.
There are two ways of validating data:
- Static mode: The validation rule is defined in the model class through the $_validate attribute.
- Dynamic: Use the Validate method of the model class to dynamically create automatic validation rules.
In any way, the definition of a validation rule is a uniform rule that defines the format as:
array(
array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),
array(验证字段2,验证规则,错误提示,[验证条件,附加规则,验证时间]),
......
);
Validation field (required)
The name of the form field that needs to be validated, which is not necessarily a database field, or it can be a secondary field for a form, such as a confirmation password and verification code, and so on. Validation fields can be set arbitrarily if there are individual validation rules and field-independent conditions, such as expire expiration rules that are not related to form fields. If a field mapping is defined, the validation field name here should be the actual table field instead of the form field.
Validation rules (required)
To validate the rules, you need to combine additional rules, if the use of regular validation of additional rules, the system also has built-in regular validation rules, can be used directly as a validation rule, including: Require field must, email mailbox, URL URL address, currency currency, Number.
Prompt information (required)
Definition of the hint message for validation failure
Validation criteria (optional)
The following scenarios are included:
- Self::exists_validate or 0 exists field is verified (default)
- Self::must_validate or 1 must be verified
- Self::value_validate or 2 value is not empty when validating
Additional Rules (optional)
Used with validation rules, including some of the following rules:
rules |
Description |
Regex |
Regular validation, the defined validation rule is a regular expression (default) |
function |
function validation, the defined validation rule is a function name |
Callback |
Method validation, a defined validation rule is a method of the current model class |
Confirm |
Verify that the two fields in the form are the same, and that the validation rule defined is a field name |
Equal |
Verify that the value is equal to a value that is defined by the preceding validation rule |
NotEqual |
Verify that the value is not equal to a value that is defined by the previous validation rule (new in the 3.1.2 version) |
Inch |
Verifies whether a defined validation rule can be an array or a comma-delimited string within a range |
Notin |
Verify that the validation rule defined is not within a range, either an array or a comma-delimited string (3.1.2 version added) |
Length |
Validation length, the defined validation rule can be a number (representing a fixed length) or a range of numbers (for example, 3,12 indicates a range from 3 to 12) |
Between |
Validation scopes, defined validation rules representing ranges, can use strings or arrays, such as 1,31 or array (1,31) |
Notbetween |
Validation is not in a range, defined validation rules represent scope, can use strings or arrays (3.1.2 version added) |
Expire |
Verify that the validation rules defined in the validity period represent a time range that can be used to time, for example, you can use 2012-1-15,2013-1-15 to indicate that the current commit is valid between 2012-1-15 and 2013-1-15, or you can use a timestamp definition |
Ip_allow |
Verify that IP is allowed, the defined validation rules represent a list of allowed IP addresses, separated by commas, for example 201.12.2.5,201.12.2.6 |
Ip_deny |
Verify that IP is prohibited, the defined validation rules represent a list of forbidden IP addresses, separated by commas, for example 201.12.2.5,201.12.2.6 |
Unique |
Validation is unique, the system will query the database based on the current value of the field to determine if the same value exists, and when the form data contains a primary key field, unique cannot be used to determine the primary key field itself |
Verification Time (optional)
- Self::model_insert or 1 new data when validating
- Self::model_update or 2 edit data when validating
- Self::model_both or 3 verification in all cases (default)
The verification time here needs to be noted, not only in these three cases, you can add additional validation time based on your business needs.
There are two ways to verify this: static and dynamic validation.
First, static verification
The automatic validation rules for the model are defined in the model class, which we call static definitions.
Verify the validation condition in the model of the test table: Create a new testModel.class.php, define the $_validate
properties as follows:
<?phpnamespace home\model;use think\model;class Testmodel extends model{ //Static verification protected $_validate = Array ( ' uid ', ' require ', ' username cannot be null '), array (' pwd ', ' require ', ' password cannot be null '), array (' repwd ', ' pwd ', ' Confirm password incorrect ', 1, ' confirm '), Array (' Age ', ' 18,50 ', ' ages must be between 18-50 years old ', 1, ' between '), array (' email ', ' email ', ' Mailbox format is incorrect '), ); }
Once you have defined the validation rules, you can call them automatically when you create a data object using the Create method:
<?phpnamespace home\controller;use home\controller\checkcontroller;class Zhucecontroller extends CheckController { function zhuce () { ///static authentication, cannot be displayed directly behind, must be authenticated to register $CW = ""; if (!empty ($_get)) { $CW = $_get["CW"]; } if (empty ($_post)) { $this->assign ("error", $CW); $this->display (); } else { $model = new \home\model\testmodel (); $model = D ("test"); Dynamic validation can be used with the D method if (! $model->create ()) { $e = $model->geterror (); $url = "zhuce/cw/{$e}"; $this->error ("Registration failed! ", $url, 1); } else { $model->add (); }
Template zhuce.html:
<body><form action= "__action__" method= "POST" ><div> User name: <input type= "text" name= "UID" id= "UID"/ > </div><br/><div> Password: <input type= "text" name= "pwd" id= "pwd"/></div><br/> <div> Confirm Password: <input type= "text" name= "repwd" id= "repwd"/> </div><br/><div> Age: <input Type= "Text" name= "Age" id= "age"/> </div><br/><div> mailbox: <input type= "text" name= "email" id= " Email "/> </div><br/><div> Name: <input type=" text "name=" name "/></div><br/> <div><{$error}></div> <!--display error message--><input type= "Submit" value= "register"/></form >
Request Zhuce Method:
Second, dynamic verification
If the dynamic authentication method, it is more flexible, according to different needs, in the operation of the same model when the use of different validation rules, such as the above static authentication method can be changed to:
<?phpnamespace home\controller;use home\controller\checkcontroller;class Zhucecontroller extends CheckController { function zhuce () { if (empty ($_post)) { $this->display (); } else { //$model = new \home\model\testmodel (); $model = D ("test"); Dynamic validation can be used with the D method//dynamic authentication $rules = Array ( ' uid ', ' require ', ' user name cannot be null ') ; Call validate () to join the validation rule $r = $model->validate ($rules)->create ();//If the validation failure returns false, successfully returns the registered test table array information // Var_dump ($r); if (! $r) { echo $model->geterror ();//Output error message if validation fails } else { $model->add (); } } }
We can also display the error prompt immediately after the form, which will use Ajax. To verify that the user name is not empty, for example:
In the template zhuce.html:
<script src= ". /.. /.. /.. /.. /jquery-1.11.2.min.js "></script> Do another yhm method in the Zhuce controller:
Verify that the user name is non-null function Yhm () { $model = D ("test"); $rules = Array ( ' uid ', ' require ', ' user name cannot be null ') ; if (! $model->validate ($rules)->create ()) { $fh = $model->geterror (); $this->ajaxreturn ($fh, ' eval '); Ajax returns the data, which returns the JSON format by default, and Eval returns the string because datatype is text, so use the eval format } else { $fh = "OK"; $this->ajaxreturn ($fh, ' eval '); } }
Request Zhuce Method:
Other validation is a similar approach, submitting the corresponding data to the corresponding method, using the corresponding validation rules.
thinkphp Framework Form Validation