Home > Article > Backend Development > Thinkphp automatic verification and automatic filling invalid solution_PHP tutorial
Automatic verification and autofilling are functions that are often used when using thinkphp, but occasionally you encounter situations where automatic verification and autofilling do not work. This article explains how thinkphp's automatic verification and autofilling may not work. Do some analysis of the reasons and propose corresponding solutions.
(1) There is a problem with the create() method
ThinkPHP automatic verification and automatic filling are implemented when creating the data object create(), so the invalidation of automatic verification and automatic filling is largely related to create().
The create method syntax is as follows:
create(mixed data, string type)
data represents the accepted data, and type represents the specific operation of this time (write or update data). Both parameters can be omitted. If the data parameter is omitted, $_POST data will be accepted by default, while type is automatically recognized by the system by default.
But the system’s automatic recognition of type is flawed. When the incoming field has a primary key field, the system recognizes it as an update operation, otherwise it is a write operation. So when the primary key field does not grow automatically but requires SQL writing, then automatic validation and automatic filling may not be effective.
For example, when adding a data record, if there is a primary key field in the form or a primary key field is generated in the system (such as entering a device number), then ThinkPHP will consider this operation to be an update operation. For example, the automatic verification and filling set below Will all be skipped:
protected $_validate = array(
//Verify that the title is unique when adding a new one
array('title','','Title already exists!',0,'unique',1),
};
//Autofill
protected $_auto = array(
// Fill in the timestamp when adding new
array('pubtime','time',1,'function'),
);
Although during the operation, the add() operation was executed to write the data to the data table, you will find that the automatic verification and automatic filling are invalid.
When this happens, you only need to explicitly pass the operation type into the create() method, that is, create($_POST,1), telling the system that this operation is to write data. In addition, if the incoming data is not $_POST, the data must also be passed in as a parameter, such as create($_GET).
(2) Data fields do not correspond
Due to carelessness, the form fields and data table fields were not properly matched.
(3) Data table fields have been changed
During the development process, the table field names were changed, but the cache was not updated in time, causing the system to judge the field as invalid and unset it. Therefore, after changing the table field name, clear the data table cache under Runtime/Data promptly.
(4) Model naming error
The Model is named incorrectly and is not named in strict accordance with the specifications. For example, the first letter is not capitalized or carelessness results in the wrong order of letters, too many or too few letters, etc. Such errors often directly lead to model failure, so special attention needs to be paid to capitalization.
(5) The data table has no self-increasing id
That is, when there is no auto-incremented ID in the data table, the data that needs to be verified will not be found during verification, so it will fail.
The above are several possible reasons why ThinkPHP's automatic verification and automatic filling are invalid. When problems occur, you must eliminate them one by one. I believe you will be able to find the errors. Of course, there are some unknown errors. If you find them For another reason, please leave a comment below.