An open API service indexing awesome lists of open source software.

https://github.com/slpcode/form-request-validation

form-request-validation 是一个不局限于框架的请求数据验证层,使用了illuminate/validation
https://github.com/slpcode/form-request-validation

form php request rules validate validation validator verification-layer verify

Last synced: 6 months ago
JSON representation

form-request-validation 是一个不局限于框架的请求数据验证层,使用了illuminate/validation

Awesome Lists containing this project

README

          

form-request-validation

![StyleCI build status](https://github.styleci.io/repos/190148835/shield)
[![Packagist](https://img.shields.io/packagist/v/slpcode/form-request-validation.svg)](https://packagist.org/packages/slpcode/form-request-validation)

==========

form-request-validation 是一个不局限于框架的请求数据验证层,使用了illuminate/validation(laravel框架的验证模块)。
详细用法可查阅laravel文档 https://laravel.com/docs/5.4/validation#available-validation-rules。
如果需要无依赖版,可以使用overtrue大神的 https://github.com/overtrue/validation

## Installing

```shell
$ composer require slpcode/form-request-validation -vvv
```

## FormRequest使用

```php
'required|max:20',
'age' => 'required|min:6'
];
}

/**
* 设置验证错误信息
*
* @return array
*/
protected function messages()
{
return [];
}

/**
* 自定义字段名称
*
* @return array
*/
protected function attributes()
{
return [];
}
}
```

```php
check();

```

## FormRequest扩展验证
```php
getValidator()->extend('mobile', function ($attribute, $value, $parameters, $validator) {
// return ...;
}, ':attribute 格式不正确');
}
}

```

## FormRequest自定义消息语言:
> 语言列表可以从这里拿:https://github.com/caouecs/Laravel-lang

```php
setLang('en', '语言包路径');
```

## Validator 使用
```php
'required|min:5|max:20',
'age' => 'required|max:2',
///...
];
$data = [
///...
];
// 可选
// 自定义错误消息
$messages = [

'name.required' => '名称不能为空',
//...
];
// 可选
// 自定义属性名称
$attributes = [
'name' => '用户名',
'age' => '年龄',
];
$validatorObj = $validator->make($data, $rules, $messages, $attributes);
//判断验证是否通过
if ($validatorObj->fails()) {
//未通过
//输出错误消息
dd($validatorObj->messages()->all());
} else {

}
```

## Validator扩展验证
```php
extend(
'mobile',
function ($attribute, $value, $parameters, $validator) {
// return ...;
}, ':attribute 格式不正确');

```

## Validator自定义消息语言:
> 语言列表可以从这里拿:https://github.com/caouecs/Laravel-lang

```php