THINKPHP5 added rules for different data

Source: Internet
Author: User
In the admin system administrator to add to the data table, because different data will have different ways to add, different data also have different database storage type, below is common several different types of data add rule//undated database storage data type is char

1. Simple text class Add (name, age, name, introduction, etc.)

Add.html Code Snippet

<p class= "col-sm-6" >                                <input class= "Form-control" id= "username" placeholder= "" name= "username"  type = "text" >                            </p>

admin.php function Section

Public function Add ()    {           if (Request ()->ispost ()) {            $data =[//assigns the value of the input to the array                ' username ' =>input (' Username '),                ' Password ' =>input (' password '),            ];            $validate = \think\loader::validate (' Admin ');//Verify link            if (! $validate->scene (' Add ')->check ($data)) {               $ This->error ($validate->geterror ()); die;//failed validation then output error            }            if (db (' admin ')->insert ($data)) {//Add database                return $this->success (' Add Administrator succeeded! ', ' lst ');            } else{                return $this->error (' Add admin failed! ');            }            return;        }        return $this->fetch ();    }

admin.php in the Validate folder
File

<?phpnamespace app\admin\validate;use think\validate;class Admin extends validate{    protected $rule = [//Verify Condition        ' username '  = '  require|max:25|unique:admin ',        ' password ' = '  require ',    ];    protected $message  =   [//error message        ' Username.require ' + ' admin name must be filled ',        ' username.max ' = ' Administrator name length must not be greater than 25 bits ',        ' username.unique ' + ' admin name cannot be duplicated ',        ' password.require ' = ' admin password required ', '    ;    protected $scene = [        ' Add ' = [  ' username ' = ' require|unique:admin ', ' Password '],        ' edit '  =  [' username ' = ' require|unique:admin '],    ];//the function field to which the constraint is scoped}

Add to


2. Long text edit add (article, preface, Introduction, etc.)


Long text usually refers to the article, such as the introduction of the text needs to be modified, here can refer to some plug-ins, the project is referring to Baidu Editor, will download a good Baidu editor file in the Public/static/admin
Such as


Here's how to reference the editor

Code style where add.html is located

<p class= "col-sm-6" >                                 <label >                                    <textarea name= "content"  id= "content" ></textarea >                                 </label>                            </p>

Functions in the Controller are read in the same way as short text, because they are all char types

Complete

3.0-1 Select Add (Gender, yes/no add)


This is the plugin used in Beyond.js.


Source code for

 <p class= "Form-group" > <label for= "username" class= "col-sm-2 Control-label no-padding -right "> Status </label> <p class=" col-sm-6 "> <selec                                    T name= "gender" > <option value= "Please select Status" > select Status </option> <option value= "approved" > Approved </option> <option value= "not approved" >                        Not audited </option> </select> </p> </p> <p class= "Form-group" > <label for= "username" c lass= "col-sm-2 control-label no-padding-right" > Sex </label> <p class= "col-sm-6" &G                                 T         <p class= "Control-group" > <p class= "Radio" >                                           <label> <input                                                        Name= "Form-field-radio" type= "Radio" class= "Colored-blue" value= "male" >                                                <span class= "text" > Men </span> </label>                                                    </p> <p class= "Radio" > <label> <i                                                        Nput name= "Form-field-radio" type= "Radio" class= "Colored-danger" value= "female" >                                                <span class= "text" > Women </span> </label> </p> <p class= "Radio"                    >                                <label> <input name= "form-                                                        Field-radio "type=" Radio "class=" colored-success "value=" not determined ">                                                <span class= "text" > Indeterminate </span> </label>                                 </p> </p> </p> </p>

This sex-recommended class is best used with char types, and if using integer characters is a bit cumbersome, this statically provided option category, and a category of categories that are read into by the database
As follows

The code in HTML is as follows

  <p class= "Form-group" >                            <label for= "group_id" class= "col-sm-2 control-label no-padding-right" > Affiliated columns </label>                            <p class= "col-sm-6" >                                <select name= "Cateid" >                                    <option value= "" > Please select columns </option>                                    {volist name= "cateres" id= "Vo"}                                    <option value= "{$vo. ID}" >{$vo. catename}</option >                                    {/volist}                                </select>                            </p>                            <p class= "Help-block col-sm-4 Red" >* required </p>                        </p>

Cates Source in article.php
Such as

Since the artcle data table is already connected in Modle by default in the article controller, it is recommended to use the Belongsto () function in model/article.php to establish a one-to-many connection (a page that joins multiple data tables, with its own understanding, As if not quite right)

This type of multi-choice is good.

4. Date additions (manually added and automatically added)


Date adds a generic database that corresponds to a date type of


The simplest is to automatically add
There is no need to have an input box, this addition is usually the system fixed time, or get the current time, in the controller only need to take advantage of functions or custom time, such as the use of date ("y-m-d h:i:s"); Get the current time

Manually add time to enter the current time in the input box yourself
Requires a date template plugin, such as a date plugin in Layui

Due to the need to import CSS JS, and so on, here does not explain, in the future when useful, I will specifically explain this

5. Image add


The first is the type of image inside the database.


Code in the Add.html

<p class= "Form-group" >                            <label for= "group_id" class= "col-sm-2 control-label no-padding-right" > Thumbnail </label>                            <p class= "col-sm-6" >                                <input id= "pic" placeholder= "name=" pic "  type=" File ">                            </p>                        </p>

For the controller code, which has a more detailed comment (own understanding)


6. Mobile phone number, mailbox add


Here and add short text, the main is to determine whether the input information is a mobile phone number or mailbox
In the validate/article.php
Add validation information like in the manual to verify

This article explains the different data added rules of THINKPHP5, more relevant content please follow the PHP Chinese web.

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.