PHP

【TP5-08】输入和验证

2017-09-29  本文已影响29人  Geeks_Chen

1、表单提交(application/index/view/user/create.html)

View层:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>创建用户</title>
<style>
body {
  font-family:"Microsoft Yahei","Helvetica Neue",Helvetica,Arial,sans-serif;
  font-size:16px;
  padding:5px;
}
.form{
  padding: 15px;
  font-size: 16px;
}

.form .text {
  padding: 3px;
  margin:2px 10px;
  width: 240px;
  height: 24px;
  line-height: 28px;
  border: 1px solid #D4D4D4;
}
.form .btn{
  margin:6px;
  padding: 6px;
  width: 120px;

  font-size: 16px;
  border: 1px solid #D4D4D4;
  cursor: pointer;
  background:#eee;
}
a{
  color: #868686;
  cursor: pointer;
}
a:hover{
  text-decoration: underline;
}
h2{
  color: #4288ce;
  font-weight: 400;
  padding: 6px 0;
  margin: 6px 0 0;
  font-size: 28px;
  border-bottom: 1px solid #eee;
}
div{
  margin:8px;
}
.info{
  padding: 12px 0;
  border-bottom: 1px solid #eee;
}

.copyright{
  margin-top: 24px;
  padding: 12px 0;
border-top: 1px solid #eee;
}
</style>
</head>
<body>
<h2>创建用户</h2>
<FORM method="post" class="form" action="{:url('index/user/add')}">
昵 称:<INPUT type="text" class="text" name="nickname"><br/>
邮 箱:<INPUT type="text" class="text" name="email"><br/>
生 日:<INPUT type="text" class="text" name="birthday"><br/>
<input type="hidden" name="__token__" value="{$Request.token}" />
<INPUT type="submit" class="btn" value=" 提交 ">
</FORM>
  <div class="copyright">
      <a title="官方网站" href="http://www.thinkphp.cn">ThinkPHP</a> 
      <span>V5</span> 
      <span>{ 十年磨一剑-为API开发设计的高性能框架 }</span>
  </div>
</body>
</html>

Controller层:

// 创建用户数据页面
public function create()
{
    return view();
}
// 新增用户数据
public function add()
{
    $user = new UserModel;
    if ($user->allowField(true)->save(input('post.'))) {
        return '用户[ ' . $user->nickname . ':' . $user->id . ' ]新增成功';
    } else {
        return $user->getError();
    }
}

2、表单验证(application/index/validate/User.php)

<?php
namespace app\index\validate;
use think\Validate;

class User extends Validate
{
    // 验证规则
    protected $rule = [
        'nickname' => 'require|min:5|token',
        'email'    => 'require|email',
        'birthday' => 'dateFormat:Y-m-d',
    ];
}

User 验证器添加了三个属性的验证规则,分别是:
昵称必须,而且最小长度为5;
邮箱必须,而且必须是合法的邮件地址;
生日可选,如果填写的话必须为Y-m-d 格式的日期格式。

<?php
namespace app\index\validate;

use think\Validate;

class User extends Validate
{
    // 验证规则
    protected $rule = [
        'nickname' => ['require', 'min'=>5, 'token'],
        'email'    => ['require', 'email'],
        'birthday' => ['dateFormat' => 'Y|m|d'],
    ];
}

控制器add方法操作:

// 新增用户数据
public function add()
{
    $user = new UserModel;
    if ($user->allowField(true)->validate(true)->save(input('post.'))) {
        return '用户[ ' . $user->nickname . ':' . $user->id . ' ]新增成功';
    } else {
        return $user->getError();
    }
}

3、错误提示

<?php
namespace app\index\validate;

use think\Validate;

class User extends Validate
{
    // 验证规则
    protected $rule = [
        ['nickname', 'require|min:5', '昵称必须|昵称不能短于5个字符'],
        ['email', 'checkMail:thinkphp.cn', '邮箱格式错误'],
        ['birthday', 'dateFormat:Y-m-d', '生日格式错误'],
    ];

    // 验证邮箱格式 是否符合指定的域名
    protected function checkMail($value, $rule)
    {
        $result = preg_match('/^\w+([-+.]\w+)*@' . $rule . '$/', $value);
        if (!$result) {
            return '邮箱只能是' . $rule . '域名';
        } else {
            return true;
        }
    }
}

4、自定义验证规则

<?php
namespace app\index\validate;

use think\Validate;

class User extends Validate
{
    // 验证规则
    protected $rule = [
        ['nickname', 'require|min:5', '昵称必须|昵称不能短于5个字符'],
        ['email', 'checkMail:thinkphp.cn', '邮箱格式错误'],
        ['birthday', 'dateFormat:Y-m-d', '生日格式错误'],
    ];

    // 验证邮箱格式 是否符合指定的域名
    protected function checkMail($value, $rule)
    {
        $result = preg_match('/^\w+([-+.]\w+)*@' . $rule . '$/', $value);
        if (!$result) {
            return '邮箱只能是' . $rule . '域名';
        } else {
            return true;
        }
    }
}

5、控制器验证

namespace app\index\controller;

use app\index\model\User as UserModel;
use think\Controller;

class User extends Controller
{
    // 创建用户数据页面
    public function create()
    {
        return view();
    }
    
    public function add()
    {
        $data = input('post.');
        // 数据验证
        $result = $this->validate($data,'User');
        if (true !== $result) {
            return $result;
        }
        $user = new UserModel;
        // 数据保存
        $user->allowField(true)->save($data);
        return '用户[ ' . $user->nickname . ':' . $user->id . ' ]新增成功';
    }
}
上一篇 下一篇

猜你喜欢

热点阅读