Mayfish Data Warehousing Validation code

Source: Internet
Author: User
Tags md5 php tutorial

The rules for executable validation are
Notempty cannot be empty
Number can only be an integer
Isemail Mailbox address is correct
Whether the Hasone is unique (duplicates, whether it already exists)
Regex Custom Regular expression

The format of the validation is
Array (validation method, field name for validation, prompt for validation error)
For validation of regular expressions
Array ("Regex", "mobile", '/^13d{9}$/', "User name cannot be left blank")
Execute fragment to write data ...
Perform a data warehousing operation
Private Function PostData () {
$fields = Array ("username", "password", "email");
$post = Array_map ("trims", $_post); Clear all extra spaces on both sides of the data
$post = parsehtml ($post, $fields); Clears the specified field contents for HTML processing
$data = Parsefields ($post, $fields); Extract fields that can be written to the database (prevent others from bypassing your page to submit some ulterior data)
$DB = & M ("member");
Perform data validation
if (! $DB->verify ($data)) {
Validation failed, take out the cause of the failure, and submit to the template page
$this->assign ("error", $DB->getverifyerror ());
Submit the data submitted to the template (to achieve the user does not seem to have left the page feeling)
$this->assign ("Default", $post);
Render Registration page Template
$this->display ("www.111cn.net/register.html");
}
else {
Writing to the database
$result = $DB->create ($data);
Returns a Boolean indicating that the data write failed, rendering the registration page template
if (Is_bool ($result)) {
$this->assign ("Default", $post);
$this->display ("/register.html");
}
else {
Registration successful, Render registration successful page template
$this->assign ("username", $data ["username"]);
$this->display ("/reg_success.html");
}
}
}

Action Code
<?php Tutorial
Class Membermodel extends Appmodel
{
/** setting database table name **/
Protected $tableName = "M Embers ";
/**
* Data validation rule
/
protected $verify = Array (
Array ("Notempty", "username", "user name cannot be left blank"),
Array (" Hasone "," username "," This user already exists, please try again with another user name "),
Array (" Notempty "," Password "," Password cannot be left blank "),
Array (" Notempty "," Email "," Mailbox address can not be left blank "),
Array (" Isemail "," email "," email address format is not correct "),
Array (" Hasone "," email "," email address has been occupied ")
);
/**
* Overwrite the method of adding data to the parent class
* MD5 encrypt the user's password before invoking the parent class's method to write to the database
/
Public Function Create ($data) {
$data = Array_map ("Addslashes", $data); Safely escapes the punctuation marks (single, double quotes) in the data
$data ["password"] = MD5 ($data ["Password"]);
return Parent::create ($data);
}
}
?

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.