ThinkPHP3.1 New feature: field validity detection

Source: Internet
Author: User
3.1 added the field validity check for form submission to better protect data security. This feature is an important part of the 3.1 security feature. 3.1 added the field validity check for form submission to better protect data security. This feature is an important part of the 3.1 security feature.
The validity check of form fields takes effect only when the create method is used to create data objects. There are two methods: 1. you can configure the insertFields and updateFields attributes for the model to add and edit form settings, when you use the create method to create a data object, attributes that are not within the defined scope will be discarded directly to avoid illegal data submission by forms.
The insertFields and updateFields attributes are set using strings (multiple fields are separated by commas) or arrays, for example:
  1. Class UserModel extends Model {
  2. Protected $ insertFields = array ('account', 'password', 'nickname', 'Email ');
  3. Protected $ updateFields = array ('nickname', 'Email ');
  4. }
The field set in the copy code is the actual data table field, which is not affected by field ING.
When we call the create method, the insertFields and updateFields attributes are automatically identified based on the submission type:
  1. D ('user')-> create ();
When the Copy code uses the create method to create a data object, when new user data is added, fields other than 'account', 'password', 'nickname', and 'Email 'are blocked, during editing, fields other than 'nickname' and 'Email 'are blocked.
The following is a string definition method, which is also valid:
  1. Class UserModel extends Model {
  2. Protected $ insertFields = 'account, password, nickname, email ';
  3. Protected $ updateFields = 'Nickname, email ';
  4. }
Copy code 2. if you do not want to define the attributes of insertFields and updateFields, or want to call them dynamically, you can directly call the field method before calling the create method. for example, the implementation has the same effect as the above example:
When adding user data, use:
  1. $ User = M ('user ');
  2. $ User-> field ('account, password, nickname, email ')-> create ();
  3. $ User-> add ();
When copying code and updating user data, use:
  1. $ User = M ('user ');
  2. $ User-> field ('Nick name, email ')-> create ();
  3. $ User-> where ($ map)-> save ();
Copy the code. the fields here are also the actual data table fields. The field method can also use the array method.

After the field validity check is used, you no longer need to worry about injecting illegal field data when submitting the form. Obviously, the second method is more flexible. select as needed!

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.