很多时候插入和修改的时候必须要对数据进行验证,下面就介绍如何在模型中使用验证器。
1、打开app/home/models/Robots.php模型,这里面使用到的$this->getDI()->getValidate()
是获取validate服务,必须先在服务中注册,否则只能使用$validate = new Validate(); $validate = $validate->addRules($rules);
<?php
namespace App\Home\Models;
use Phalcon\Mvc\Model;
// use Common\Validate;
class Robots extends Model {
public function initialize() {
$this->setSource('ph_robots');
}
/**
* @desc 插入之前验证
* @author zhaoyang
* @date 2018年5月16日 下午10:57:48
*/
public function beforeValidationOnCreate() {
$rules = [
//表示name插入时必须验证
['name', 'regex', '姓名必须为6-10位数字字母下划线', '/^[a-zA-Z\d_]{6,10}$/', 1],
// 或者这么写
// ['name', 'alnum', '姓名必须为字母数字', '', 1],
// ['name', 'stringlength', '姓名长度为6-10位', [6,10], 1],
['name', 'uniqueness', '姓名已存在', '', 1],
// 表示插入时必须验证
['type', 'inclusionin', '类型只能为1,2,3', [1,2,3], 1],
// 也可以这么写
// ['type', 'callback', '类型只能为1,2,3', function ($data) {
// $type = $data->type ?? null;
// if (in_array($type, [1, 2, 3])){
// return true;
// }else{
// return false;
// }
// }, 1],
// 表示插入时必须验证
['weight', 'digit', '重量只能为数字', '', 1],
['weight', 'between', '重量只能在1-200之间', [1, 200], 1],
// 或者这么写
// ['weight', 'regex', '重量只能在1-200之间的整数', '/^(1\d?\d?)|200$/', 1],
];
$validate = $this->getDI()->getValidate()->addRules($rules);
// $validate = new Validate();
// $validate = $validate->addRules($rules);
return $this->validate($validate);
}
/**
* @desc 更新之前验证
* @author zhaoyang
* @date 2018年5月16日 下午10:57:57
*/
public function beforeValidationOnUpdate() {
$rules = [
//表示name更新时必须验证
['name', 'regex', '姓名必须为6-10位数字字母下划线', '/^[a-zA-Z\d_]{6,10}$/', 1],
['name', 'uniqueness', '姓名已存在', '', 1],
// 表示更新时存在即验证
// ['type', 'inclusionin', '类型只能为1,2,3', [1,2,3]],
//或者
['type', 'callback', '类型只能为1,2,3', function ($data) {
$type = $data->type ?? null;
return $data->checkType($type);
}, 1],
// 表示更新时存在即验证
['weight', 'digit', '重量只能为数字'],
['weight', 'between', '重量只能在1-200之间', [1, 200]],
];
$validate = $this->getDI()->getValidate()->addRules($rules);
return $this->validate($validate);
}
public function checkType($type){
if (in_array($type, [1, 2, 3])){
return true;
}else{
return false;
}
}
}
2、打开app/home/controller/RobotsController.php
<?php
/**
* @desc 主页
* @author zhaoyang
* @date 2018年3月23日 上午10:11:38
*/
namespace App\Home\Controllers;
use Common\BaseController;
use App\Home\Models\Robots;
class RobotsController extends BaseController {
public function indexAction() {
$robotsList = Robots::find();
foreach ($robotsList as $robot){
echo $robot->id,' ',$robot->name,' ',$robot->type,' ',$robot->weight,'<br/>';
}
}
public function addAction() {
$data = $this->get();
$robots = new Robots();
$res = $robots->create($data); // or save($data)
var_dump($res, $robots->id ?? '插入失败', $robots->getMessages());exit;
}
public function infoAction() {
$id = $this->get('id', 'int');
$robot = Robots::findFirst($id);
echo $robot->id,' ',$robot->name,' ',$robot->type,' ',$robot->weight;exit;
}
public function editAction() {
$data = $this->get();
$robot = Robots::findFirst($data['id']);
$res = $robot->update($data); // or save($data)
var_dump($res, $res ? '更新成功' : '更新失败', $robot->getMessages());exit;
}
public function deleteAction() {
$id = $this->get('id', 'int');
$robot = Robots::findFirst($id);
$res = $robot->delete();
var_dump($res, $res ? '删除成功' : '删除失败', $robot->getMessages());exit;
}
}
3、访问/robots/add/name/robot_one/type/0/weight/13d
访问/robots/add/name/robot_two/type/2/weight/122
访问/robots/edit/id/1/type/10/weight/15a