CakePHP introduces basic user test based on regular expressions

Source: Internet
Author: User
CakePHP provides some built-in regular expressions for data validation, including VALID_NOT_EMPTY, VALID_NUMBER, VALID_EMAIL, and VALID_YEAR. These constants are defined in cake/libs/validators. php and should not be modified. You may find these constants helpful.

By introducing the regular expression-based Basic user test, you should now have a brief understanding of the CakePHP data test. By defining your own regular expressions to perform data validation, you can further control the success/failure conditions of each form field in the Tor.

Database Design
Table Create Table
Users Create table 'users '(
'Id' int (10) not null AUTO_INCREMENT,
'Username' varchar (40) not null,
'Password' varchar (40) not null,
'Email 'varchar (255) not null,
'First _ name' varchar (40) not null,
'Last _ name' varchar (40) not null,
Primary key ('id '),
Unique key 'username' ('Username '),
Unique key 'password' ('password ')
) ENGINE = MyISAM default charset = utf8
User model
123456789101112 VALID_NOT_EMPTY, 'password' => VALID_NOT_EMPTY, 'Email '=> VALID_EMAIL) ;}?>

This is a good start, but not enough. Make sure that the field length does not exceed the limit and the user name does not exist. In this case, you need to define your own regular expression for validation, and define a function to find the user name in the users table before saving the user.

Regular expression

A regular expression is a character pattern used to compare a string with another string. For example, the character * in the regular expression matches any character for any number of times. Do not worry about regular expressions. The following example should help you get started.

Define your own tests

CakePHP provides some built-in regular expressions for data validation, including VALID_NOT_EMPTY, VALID_NUMBER, VALID_EMAIL, and VALID_YEAR. These constants are defined in cake/libs/validators. php and should not be modified. You may find these constants helpful.

For the username and password fields, make sure that the data submitted cannot exceed 40 characters. It is also helpful to check the length of the user name and password not less than 6 characters. The regular expression used to match strings of 6 to 40 characters is similar to/^. {6, 40} $ /. Read this regular expression from left to right:

  • /-Indicates the beginning of the regular expression
  • ^-IndicatesStart with a string
  • .-IndicatesAny character
  • {6, 40}-indicatesIt must be at least 6 characters long but cannot exceed 40 characters
  • $-IndicatesString end
  • /-Indicates that the regular expression ends.

Therefore, when the string is read together, this regular expression means "starting from the start of the string, there are at least 6 characters but no more than 40 characters, and then the string ends ".

Replace VALID_NOT_EMPTY with a regular expression enclosed by single quotes (to prevent PHP from trying to interpret any special characters ).

123456789101112 '/^. {6, 40} $/', 'password' => '/^. {6, 40} $/', 'Email '=> VALID_EMAIL);}?>

Make sure to save all the files, return to http: // 127.0.0.1/framework/cake_1.2.5/users/register, and then try to register the user with a user name with four characters.

Note:Regular expressions can implement many functions, but they cannot perform operations such as checking whether the user name has been registered.

Further test

Sometimes, you cannot check the data to determine whether it is valid. For example, the user name may be between 6 to 40 characters, but you must check the database to check whether the user name is in use. CakePHP provides the ability to manually mark fields as invalid. Let's take a look at the beforeValidate method below. This method should be addedUser model.

123456789 FunctionbeforeValidate () {if (! $ This-> id) {if ($ this-> findCount (array ('user. username' => $ this-> data ['user'] ['username'])> 0) {$ this-> invalidate ('username _ unique '); returnfalse ;}} returntrue ;}

This method tells the model to check whether the submitted data has an ID before running any tests. If there is no ID, search for other users with the same user name. If such a user is found, the user name field is marked as invalid and other checks are skipped (false is returned ). You can enable this function by changing the username input line in the register. ctp view to the following code.

12 Echo $ form-> input ('username', array ('after' => $ form-> error ('username _ unique ', 'The username is taken. please try again. ')));

This tells the register view what to do in case of an error message 'username _ unique' (set in the beforeValidate method.

Save the file and try again. First, access http: // 127.0.0.1/framework/cake_1.2.5/users/knownusers to obtain the list of existing users. Access http: // 127.0.0.1/framework/cake_1.2.5/users/register, and then try to create a user with the same user name. The following results are displayed:

The username is taken. Please try again.

Good data validation is an important step in creating any secure application. When building Tor applications, you need to find opportunities to improve data inspection. Do not be afraid to introduce more data tests than in this tutorial. Never assume that the user will send the required data to you. Test all content. CakePHP can help you easily complete the process.

Conclusion

This section introduces the basic user test based on regular expressions in CakePHP.
For details, refer to: developerWorks CakePHP data test in China.
Source code download: CakePHP getting started tutorials source code download -- cakephp-example-for new.rar

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.