namespace app\index\controller; use app\common\controller\Base; use think\facade\Request; use app\common\model\ User as UserModel; class User extends Base { //Registration page public function register() { $this->assign('title','User Register'); return $this->fetch(); }
//Process the registration information submitted by the user public function insert() { If (request :: isajax ()) { /** * Use models to create data #&*/##// Verification data $ data = request :: Post (); // Data $rule= 'app\common\validate\User';//Customized validation rules $res=$this->validate($data,$rule);//Start validating data ’ ’ s ’ s ’ ’ s ’ ’ s ’ ’ s ’ ’ s ’ t 1 ]; } Else { // If the data is correct if (usermodel :: create ($ data)) { Return ['Status' = & GT ;1,'message'=>'Congratulations, the registration is successful']; } else ‐ ‐ ‐ } ‐ ‐ ‐ ‐‐ '=>'Register Failure, please check ']; } }
namespace app\common\validate; use think\Validate; class User extends Validate { /** * Current validation rules * @var array */ protected $rule = [ /** * Another way of writing: * * 'name|Username'=>'require|length:5,20|chsAlphaNum', * 'email|Mailbox'=>'require|email |unique:zh_user', * 'password|password'=>'require|alphaNum|length:6,20|confirm', * 'mobile|手机'=>'require|mobile|unique :zh_user|number' */ 'name|username'=>[ 'require'=> 'require', 'length'=>'5,20', 'chsAlphaNum'=>'chsAlphaNum',//Only Chinese characters, characters and numbers are allowed ], ' email|Mailbox'=>[ 'require'=>'require', 'unique'=>'zh_user',//This field value is unique in the zh_user table email'=>'email', ], ‐ ’ ’ ’ s ’ s ’ being ', 'unique'=>'zh_user',//This field value is unique in the zh_user table 'number'=>'number', ], 'password | Password '= & gt; [ ' requine '= & gt;' reques, 'alphaanum' = & gt; 'alphaum', // Only allow letters plus numbers 'length' = & gt; ' 6,20',//Length limit 'confirm'=>'confirm',//Automatically perform equality verification with the password_confirm field #
Solved, it turned out that the name attribute setting on the registration page was wrong
There is too much content on the registration page, and it seems that it cannot be posted.
The registration page is exactly the same as what the teacher wrote
Controller
namespace app\index\controller;
use app\common\controller\Base;
use think\facade\Request;
use app\common\model\ User as UserModel;
class User extends Base
{
//Registration page
public function register()
{
$this->assign('title','User Register');
return $this->fetch();
}
//Process the registration information submitted by the user
public function insert()
{
If (request :: isajax ())
{
/**
* Use models to create data
#&*/##// Verification data
$ data = request :: Post (); // Data
$rule= 'app\common\validate\User';//Customized validation rules
$res=$this->validate($data,$rule);//Start validating data
’ ’ s ’ s ’ ’ s ’ ’ s ’ ’ s ’ ’ s ’ t 1 ];
}
Else
{
// If the data is correct
if (usermodel :: create ($ data))
{
Return ['Status' = & GT ;1,'message'=>'Congratulations, the registration is successful'];
}
else
‐ ‐ ‐ } ‐ ‐ ‐ ‐‐ '=>'Register Failure, please check '];
}
}
}
Else
{
$ this-& gt error ("" Request Type Error ",' Register ') ;
}
}
}
Validation rules namespace app\common\validate\User.php:
namespace app\common\validate;
use think\Validate;
class User extends Validate
{
/**
* Current validation rules
* @var array
*/
protected $rule = [
/**
* Another way of writing:
* * 'name|Username'=>'require|length:5,20|chsAlphaNum',
* 'email|Mailbox'=>'require|email |unique:zh_user',
* 'password|password'=>'require|alphaNum|length:6,20|confirm',
* 'mobile|手机'=>'require|mobile|unique :zh_user|number'
*/
'name|username'=>[
'require'=> 'require',
'length'=>'5,20',
'chsAlphaNum'=>'chsAlphaNum',//Only Chinese characters, characters and numbers are allowed
],
' email|Mailbox'=>[
'require'=>'require',
'unique'=>'zh_user',//This field value is unique in the zh_user table email'=>'email',
],
‐ ’ ’ ’ s ’ s ’ being ',
'unique'=>'zh_user',//This field value is unique in the zh_user table
'number'=>'number',
],
'password | Password '= & gt; [
' requine '= & gt;' reques,
'alphaanum' = & gt; 'alphaum', // Only allow letters plus numbers
'length' = & gt; ' 6,20',//Length limit
'confirm'=>'confirm',//Automatically perform equality verification with the password_confirm field
#
Post the code and take a look