Data validation mechanism of zentaophp framework

Source: Internet
Author: User
Tags filter key variables variable

Data validation in projects is especially important in web applications. Next, we will introduce the data validation mechanism of the zentaophp framework.

first, to determine a problem, that is, the validation of the rules where?

In an MVC program, you can put validation rules on each layer. For example, a lot of form validation is automatically validated against the user's input and then given a hint. So which layer does the data validation put on? There is a lot of controversy on the internet about this problem. Some people mainly put on the view of the layer, and some advocates on the control layer. The Zen Path framework chooses the model layer.

Why do you do that? Because the model layer is the lowest layer, all the data operations, have to go through model to deal with. So as long as the data validation in this respect, you can guarantee the accuracy and security of data. Of course, the user of the framework can be at the same time in the front-end plus JS verification, and the model layer of validation will not conflict. Let's look at how to use the data filtering mechanism of Zen channel.

The data filter of Chan DAO is divided into two parts, one is data revision, the other is data verification. This is inspired by the PHP filter extension, which is divided into these two parts. Let's look at some examples of data revisions:

ii. Data Revisions:

$bug = Fixer::input (' Post ')

->add (' Openedby ', $this->app->user->account)

->add (' openeddate ', $now)

->setdefault (' Project,story,task ', 0)

->setdefault (' Openedbuild ', ')

->setif ($this->post->assignedto!= ', ' assigneddate ', $now)

->setif ($this->post->story!= false, ' storyversion ', $this->loadmodel (' Story ')->getversion ($this- >post->story))

->specialchars (' Title,steps,keyword ')

->cleanint (' product, module, severity ')

->join (' Openedbuild ', ', ', ')

->remove (' files, labels ')

->get ();

First, it calls the input method of fixer, whose argument post representation is fetching data from the $_post variable.

The next two lines, add (), add two variables to the data. The next two lines of SetDefault, then, indicate that when the variable does not pass a value, it is set to the default value.

Next comes two lines of setif. Setif a total of three parameters, the first is to determine the conditions, the following two are key and value. That is, when the condition is true, set the $key = $value.

The following spechialchars represent htmlspecialchars processing of these three fields

Finally, you need to remove the two unwanted variables and use the Remove.

The Get method allows you to obtain a complete set of data that has been modified. This collection can already be ready for storage. Let's look at how the data is validated.

third, data inspection

This SQL INSERT statement passes the modified data to the DAO object through the data method and then automatically checks it by AutoCheck (). AutoCheck will judge the length according to the type of field in the database. Errors are automatically logged if the type is incorrect, or if the length is not correct. The Batchcheck () method is then invoked, and Non-null validation is performed on a batch of fields. Of course, you can verify a single field by using the check () method.

if (Dao::iserror ()) Die (Js::error (Dao::geterror ())); Yes, that's it. If Dao::iserror (), the GetError () method is executed and then merged into a JS string that pops up in the form of a warning box. After GetError () is executed, all errors are emptied so that the next execution is not affected.



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.