非LaravelプロジェクトでのValidatorベリファイアの使用
8569 ワード
インストール
プロンプトメッセージのインポート
プロジェクトルートディレクトリlang/zh_の作成cn/validation.phpファイル
由来:https://learnku.com/articles/...
パッケージhandler
パッケージhandler $translation_pathは、作成したばかりのlangディレクトリ に位置決めされる. $translation_localeは多言語ディレクトリ名、すなわちzh_cn
参照:http://www.xiaosongit.com/ind...
使用
直接使用
カスタムメッセージプロンプトと定義プロパティ名
エラーメッセージの印刷
PS:検証ルールはLaravel Validatorドキュメントをご覧ください
finish!
composer require illuminate/validation
プロンプトメッセージのインポート
プロジェクトルートディレクトリlang/zh_の作成cn/validation.phpファイル
':attribute ',
'active_url' => ':attribute URL',
'after' => ':attribute :date ',
'after_or_equal' => ':attribute :date ',
'alpha' => ':attribute ',
'alpha_dash' => ':attribute 、 、 ',
'alpha_num' => ':attribute ',
'array' => ':attribute ',
'before' => ':attribute :date ',
'before_or_equal' => ':attribute :date ',
'between' => [
'numeric' => ':attribute :min :max ',
'file' => ':attribute :min :max KB ',
'string' => ':attribute :min :max ',
'array' => ':attribute :min :max ',
],
'boolean' =>':attribute true false, 1 0 ',
'confirmed' => ':attribute ',
'date' => ':attribute ',
'date_format' => ':attribute :format ',
'different' => ':attribute :other',
'digits' => ':attribute :digits .',
'digits_between' => ':attribute :min :max ',
'dimensions' => ':attribute ',
'distinct' => ':attribute ',
'email' => ':attribute ',
'exists' => ' :attribute .',
'file' => ':attribute ',
'filled' => ':attribute ',
'image' => ':attribute jpeg, png, bmp gif ',
'in' => ' :attribute ',
'in_array' => ':attribute :other',
'integer' => ':attribute ',
'ip' => ':attribute IP 。',
'json' => ':attribute JSON ',
'max' => [
'numeric' => ':attribute :max ',
'file' => ':attribute :max',
'string' => ':attribute :max ',
'array' => ':attribute :max .',
],
'mimes' => ':attribute :values',
'min' => [
'numeric' => ':attribute :min ',
'file' => ':attribute :min KB',
'string' => ':attribute :min ',
'array' => ':attribute :min ',
],
'not_in' => ' :attribute ',
'numeric' => ':attribute ',
'present' => ':attribute ',
'regex' => ':attribute ',
'required' => ':attribute ',
'required_if' => ':attribute :other :value',
'required_unless' => ':attribute , :other :values ',
'required_with' => ':attribute :values ',
'required_with_all' => ':attribute :values ',
'required_without' => ':attribute :values ',
'required_without_all' => ':attribute :values ',
'same' => ':attribute :other ',
'size' => [
'numeric' => ':attribute :size ',
'file' => ':attribute :size KB',
'string' => ':attribute :size ',
'array' => ':attribute :size ',
],
'string' => ':attribute ',
'timezone' => ':attribute .',
'unique' => ':attribute ',
'url' => ':attribute ',
/*
|--------------------------------------------------------------------------
| Custom Validation Language Lines
|--------------------------------------------------------------------------
|
| Here you may specify custom validation messages for attributes using the
| convention "attribute.rule" to name the lines. This makes it quick to
| specify a specific custom language line for a given attribute rule.
|
*/
'custom' => [
'attribute-name' => [
'rule-name' => 'custom-message',
],
],
/*
|--------------------------------------------------------------------------
| Custom Validation Attributes
|--------------------------------------------------------------------------
|
| The following language lines are used to swap attribute place-holders
| with something more reader friendly such as E-Mail Address instead
| of "email". This simply helps us make messages a little cleaner.
|
*/
'attributes' => [
// 'name' => ' ',
// 'age' => ' ',
],
];
由来:https://learnku.com/articles/...
パッケージhandler
パッケージhandler
namespace App\handlers;
class Validator extends \Illuminate\Validation\Factory
{
/***
*
*
* @return \Illuminate\Validation\Factory
*/
public static function getInstance()
{
static $validator = null;
if ($validator === null) {
$translation_path = __DIR__ . '/../lang';
$translation_locale = 'zh_cn';
$translation_file_loader = new \Illuminate\Translation\FileLoader(new \Illuminate\Filesystem\Filesystem,
$translation_path);
$translator = new \Illuminate\Translation\Translator($translation_file_loader, $translation_locale);
$validator = new \Illuminate\Validation\Factory($translator);
}
return $validator;
}
}
参照:http://www.xiaosongit.com/ind...
使用
直接使用
//
$data['title'] = '123';
$data['content'] = '123';
//
$validator = Validator::getInstance()->make($data, [
'title' => 'required|min:10',
'content' => 'required|min:10',
]);
カスタムメッセージプロンプトと定義プロパティ名
//
$data['title'] = '123';
$data['content'] = '123';
//
$rules = [
'title' => 'required|min:10',
'content' => 'required|min:10',
];
//
$messages = [
'title.required' => ':title '
];
//
$attributes = [
'title' => ' ',
'content' => ' ',
];
//
$validator = Validator::getInstance()->make($data, $rules, $message, $attributes);
エラーメッセージの印刷
if ($validator->fails()) {
print_r($validator->errors()->all());
}
PS:検証ルールはLaravel Validatorドキュメントをご覧ください
finish!