Home > Backend Development > PHP Tutorial > Regarding the question of which layer to place thinkphp data validation on?

Regarding the question of which layer to place thinkphp data validation on?

WBOY
Release: 2016-08-04 09:19:34
Original
1258 people have browsed it

I am a newbie. I encountered a problem when practicing code. When doing multi-form submission data verification, should I put the verification in the controller layer or the model layer? This verification includes judging whether the field is Empty, but also to determine the uniqueness of a certain field, etc. I checked it myself, and some answers are to put the verification on the controller layer, but the TP manual has automatic verification, which is placed on the model layer, so I am a bit confused. I hope you can give me some advice.

Reply content:

I am a newbie. I encountered a problem when practicing code. When doing multi-form submission data verification, should I put the verification in the controller layer or the model layer? This verification includes judging whether the field is Empty, but also to determine the uniqueness of a certain field, etc. I checked it myself, and some answers are to put the verification on the controller layer, but the TP manual has automatic verification, which is placed on the model layer, so I am a bit confused. I hope you can give me some advice.

Give you a manual link, I hope it can help you automatically verify

Place it in the Model layer, so that before automatically creating data $this->create(), you can verify the data according to the validate defined in the Model. Personally, I think encapsulation would be better. This is called dynamic validate

The project I am writing is placed in the controller.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template