Thinkphp implements automatic verification and completion ,. Thinkphp implements automatic verification and automatic completion. the automatic verification and automatic completion of Thinkphp are based on the content submitted by the form, perform rule verification and processing on some data, and insert the data to Thinkphp for automatic verification and completion,
The automatic verification and automatic completion of Thinkphp are based on the content submitted by the root form, and some data is verified and processed by rules, and then inserted into the database.
1. automatic verification format:
Array (verification field 1, verification rule, error prompt, [verification condition, additional rule, verification time]), array (verification field 2, verification rule, error prompt, [verification conditions, adding rules, verification time]),...);
Verification conditions:
Self: EXISTS_VALIDATE or 0 fields are verified (default)
Self: MUST_VALIDATE or 1 must be verified
Self: verification when VALUE_VALIDATE or 2 is not empty
Verification Time:
Self: MODEL_INSERT or 1 verification when adding data
Self: MODEL_UPDATE or 2 verification when editing data
Self: MODEL_BOTH or 3 verification in all cases (default)
2. automatic completion format:
Array (completion Field 1, completion rule, [completion condition, additional rule]), array (completion field 2, completion rule, [completion condition, additional rules]),...);
Completion Time:
Self: Process newly added data in MODEL_INSERT or 1 (default)
Self: processing when MODEL_UPDATE or 2 updates data
Self: MODEL_BOTH or 3 all situations are processed
Small instance (registration)
HTML layout:
Model (MemberModel)
<? Phpnamespace Admin \ Model; use Think \ Model; class MemberModel extends Model {/* automatic verification */protected $ _ validate = array ('username ','', 'The user name is unique! ', Self: EXISTS_VALIDATE, 'Unique', self: MODEL_INSERT), array ('password', 'require', 'no password entered! ', Self: EXISTS_VALIDATE, '', self: MODEL_INSERT), array ('repassword', 'password',' duplicate password is incorrect! ', Self: EXISTS_VALIDATE, 'Confirm', self: MODEL_INSERT), array ('phone', '11',' incorrect phone number length! ', Self: EXISTS_VALIDATE, 'length', self: MODEL_INSERT), array ('email', 'Email ',' incorrect email format! ', Self: EXISTS_VALIDATE, '', self: MODEL_INSERT);/* automatic completion */protected $ _ auto = array ('password', 'enabled ', self: MODEL_INSERT, 'callback'), array ('state', '1', self: MODEL_INSERT), array ('portrait', 'portrait', self :: MODEL_INSERT, 'callback'), array ('create _ time', 'createtime', self: MODEL_INSERT, 'callback ')); /* encrypt the password */public function encrypt () {return md5 (crypt (I ('post. password/s '), 'Zh ');}/* creation time */public function createTime () {return time ();}/* upload profile */public function portrait () {if ($ _ FILES ['portrait'] ['name']) {// if the uploaded profile picture $ upload = new \ Think \ Upload (); // instantiate the upload class $ upload-> maxSize = 3145728; // Set the attachment upload size $ upload-> exts = array ('jpg ', 'GIF', 'PNG ', 'jpeg '); // Set the attachment upload type $ upload-> rootPath = '. /Uploads/portrait/'; // sets the root directory for uploading attachments // Uploads a single file $ info = $ upload-> uploadOne ($ _ FILES [' Portrait ']); if (! $ Info) {// upload error message $ this-> error ($ upload-> getError ());} else {// obtain the information of the uploaded file after the upload is successful $ portraitPath = '. /Uploads/portrait /'. $ info ['savepath']. $ info ['savename']; $ image = new \ Think \ Image (); $ image-> open ($ portraitPath ); // Generate a thumbnail with a center crop of 80*80 $ image-> thumb (150,150, \ Think \ Image: IMAGE_THUMB_CENTER)-> save ($ portraitPath ); return $ info ['savepath']. $ info ['savename'] ;}}}
Corresponding data table structure:
Detect and insert data to the database in the controller:
/* Register */public function register () {if (IS_POST) {$ member = D ('member'); if ($ member-> create ()) {if ($ member-> add () {$ this-> success ('registration successful! ');} Else {$ this-> error ('registration failed! ') ;}} Else {exit ($ member-> getError () ;}}$ this-> display ();}
The above is all the content of this article, hoping to help you learn
Articles you may be interested in:
- Php form verification implementation code
- Php session application instance logon verification
- Php cookie logon verification Sample code
- PHP verification code (latest modification, fully customized! )
- Php mobile phone number verification regular expression
- PHP code for session sharing and login verification through session id
- A beautiful php verification code class (share)
- PHP generates image verification codes and click to switch instances
- PHP uses CURL to simulate logon to websites with verification codes
The automatic verification and automatic completion of keys and Thinkphp are based on the content submitted by the root form. some data is verified and processed by rules, and then inserted into the data...