davek1312 / validation
Bootstrap illuminate/validation 包
Requires
- davek1312/app: 0.2.*
- davek1312/translation: 0.0.*
- illuminate/validation: ^5.4
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-09-20 07:08:09 UTC
README
Bootstrap illuminate/validation 包。
安装
此包可在 Packagist 上找到,您可以使用 Composer 安装它。
composer require davek1312/validation
配置
将 vendor/davek1312/validation/davek1312
文件夹复制到您的应用程序根目录。
语言文件
默认验证消息可查看于 vendor/davek1312/validation/lang/en/validation.php
。
地区语言文件
要为验证设置不同的地区语言文件,请查看 Translation Package 文档。
覆盖验证消息
要覆盖验证消息,请在 davek1312/translation/lang/vendor/validation/{locale}/
下添加自定义的 validation.php
。
规则
要查看可用的验证规则,请查看 Laravel 文档。
使用方法
<?php
use Davek1312\Validation\Validator;
$data = [
'key' => 'value'
];
$rules = [
'key' => 'required'
];
$validator = new Validator($data, $rules, [], $locale = 'en');
//Check if the validation passes
$validator->passes();
//Check if the validation fails
$validator->failed();
//Get the errors
$validator->errors();
自定义错误消息
如果需要,您可以使用自定义错误消息进行验证而不是默认消息。您可以通过程序或语言文件定义客户消息。
程序化
//This will be the message for the required rule
$customValidationMessages = [
'required' => 'The :attribute field is required.',
];
$validator = new Validator($data, $rules, $customValidationMessages);
//This will be the message for the required rule only for the `email` attribute
$customValidationMessages = [
'email.required' => 'We need to know your e-mail address!',
];
$validator = new Validator($data, $rules, $customValidationMessages);
在语言文件中
将您的客户消息添加到您的 davek1312/translation/lang/vendor/validation/{locale}/validation.php
文件中的 custom
数组中。
'custom' => [
'email' => [
//This will be the message for the required rule only for the `email` attribute
'required' => 'We need to know your e-mail address!',
],
],
如果您希望验证消息中的 :attribute 部分被自定义属性名称替换,您可以在 davek1312/translation/lang/vendor/validation/{locale}/validation.php
文件中的属性数组中指定自定义名称。
'attributes' => [
'email' => 'email address',
],
自定义验证规则
要注册自定义验证规则,请查看 App Package 文档。
protected function register() {
// Will NOT run if the value being validated is empty
$this->registerValidationRules('foo', 'Class@validateFoo');
// Will run if the value being validated is empty
$this->registerImplicitValidationRules('foo', 'Class@validateFoo');
// Custom place-holder replacements for error messages
$this->registerValidationReplacers('foo', 'Class@replaceFoo');
}
自定义验证消息
您还需要为您的自定义规则定义一个错误消息。您可以通过内联自定义消息数组或添加验证语言文件中的条目来实现。此消息应位于数组的第一层,而不是在自定义数组中,自定义数组仅用于属性特定的错误消息。
"foo" => "Your input was invalid!",