1. Go to this page and download the library: Download starrysea/validate library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// app/Providers/AppServiceProvider.php
use Starrysea\Validate\Validate;
class AppServiceProvider
{
// ...
public function boot()
{
Validate::FormRules(); // 扩展表单验证规则
// ...
}
// ...
}
// resources/lang/en/validation.php
return [
// ...
'identitys' => 'The :attribute not a valid id number.',
'phone' => 'The :attribute not a legal chinese mobile number.',
'english' => 'The :attribute contains characters other than spaces, english, and numbers.',
'password' => 'The :attribute does not meet the password verification rules.',
];
// resources/lang/zh_cn/validation.php
return [
// ...
'identitys' => ':attribute 不是合法的身份证号码',
'phone' => ':attribute 不是合法的中国手机号码',
'english' => ':attribute 包含了空格、英文、数字以外的字符',
'password' => ':attribute 不符合密码验证规则',
];
// vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php
use Starrysea\Validate\FormRequest as FormRequestDevelop;
class FormRequest
{
// ...
// 引入表单验证扩展
use FormRequestDevelop;
// 重写创建验证实例方法
protected function createDefaultValidator(ValidationFactory $factory)
{
return $factory->make(
$this->validationData(), $this->container->call([$this, 'combine']),
$this->messages(), $this->attributes()
);
}
// 重写获取验证数据方法
public function validated()
{
$rules = $this->container->call([$this, 'combine']);
return $this->only(collect($rules)->keys()->map(function ($rule) {
return explode('.', $rule)[0];
})->unique()->toArray());
}
// ...
}
class FormRequestGatherTest
{
// 展现构筑规则方法名
protected $showSource = true;
// 展现当前请求验证的所有规则
protected $showRule = true;
// 该规则任何时候都生效, 但是优先级最低, 可被其它规则重写
public function rules()
{
return [
'idcode' => 'identitys',
'phone' => 'phone'
];
}
// 构筑规则获得如下方法, 该规则仅 get 请求生效
public function rulesGet()
{
return [
'phone' => 'bail|
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.