创建模型验证器,验证数据正确性
This commit is contained in:
41
app/common/validate/OperationNotice.php
Normal file
41
app/common/validate/OperationNotice.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\validate;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class OperationNotice extends Validate
|
||||
{
|
||||
protected $failException = true;
|
||||
|
||||
protected $rule = [
|
||||
'title' => 'require|max:255',
|
||||
'notice_type' => 'require|in:0,1',
|
||||
'status' => 'require|in:0,1',
|
||||
'publish_at' => 'require|checkPublishAt',
|
||||
];
|
||||
|
||||
protected $scene = [
|
||||
'add' => ['title', 'notice_type', 'status', 'publish_at'],
|
||||
'edit' => ['title', 'notice_type', 'status', 'publish_at'],
|
||||
];
|
||||
|
||||
protected function checkPublishAt($value): bool
|
||||
{
|
||||
if (is_int($value)) {
|
||||
return $value >= 0;
|
||||
}
|
||||
if (is_string($value)) {
|
||||
if ($value === '') {
|
||||
return false;
|
||||
}
|
||||
if (ctype_digit($value)) {
|
||||
return true;
|
||||
}
|
||||
return strtotime($value) !== false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user