ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## 验证规则 全部的验证类型与对应的验证选项如下,大部分能根据语义理解,特殊的几个下文会单独说明。 ~~~ // 规则 public function rules() { return [ 'a' => ['integer', 'unsigned' => true, 'min' => 1, 'max' => 1000000, 'length' => 10, 'minLength' => 3, 'maxLength' => 5], 'b' => ['double', 'unsigned' => true, 'min' => 1, 'max' => 1000000, 'length' => 10, 'minLength' => 3, 'maxLength' => 5], 'c' => ['alpha', 'length' => 10, 'minLength' => 3, 'maxLength' => 5], 'd' => ['alphaNumeric', 'length' => 10, 'minLength' => 3, 'maxLength' => 5], 'e' => ['string', 'length' => 10, 'minLength' => 3, 'maxLength' => 5, 'filter' => ['trim', 'strip_tags', 'htmlspecialchars']], 'f' => ['email', 'length' => 10, 'minLength' => 3, 'maxLength' => 5], 'g' => ['phone'], 'h' => ['url', 'length' => 10, 'minLength' => 3, 'maxLength' => 5], 'i' => ['in', 'range' => ['A', 'B'], 'strict' => true], 'j' => ['date', 'format' => 'Y-m-d'], 'k' => ['compare', 'compareAttribute' => 'a'], 'l' => ['match', 'pattern' => '/^[\w]{1,30}$/'], 'm' => ['call', 'callback' => [$this, 'check']], 'n' => ['file', 'mimes' => ['audio/mp3', 'video/mp4'], 'maxSize' => 1024 * 1], 'r' => ['image', 'mimes' => ['image/gif', 'image/jpeg', 'image/png'], 'maxSize' => 1024 * 1], ]; } ~~~ ## `call` 验证类型 该类型为用户自定义验证规则,`callback` 内指定一个用户自定义的方法来验证。 自定义的方法如下: ~~~ // 自定义验证 public function check($fieldValue) { // 验证代码 // ... // 返回结果 return true; } ~~~ >[info] 返回 true 通过验证,false 返回错误。 ## `file` / `image` 验证类型 该类型用来验证文件,包含的两个验证选项如下: - mimes:输入你想要限制的文件mime类型,[MIME参考手册](http://www.w3school.com.cn/media/media_mimeref.asp) 。 - maxSize:允许的文件最大尺寸,单位 KB。 验证成功后模型类会增加一个同名属性,该属性为 `Psr\Http\Message\UploadedFileInterface` 类的实例化对象,在模型内可直接调用 `$this->[attributeName]->moveTo($targetPath)` 移动到你需要存放的位置,更多方法请查看 "文件上传" 章节。