yuiliusardian/validation

在 Laravel 之外使用 Laravel 验证,这可以为我们的项目验证任何数据,非常有用和有帮助。

v1.0.4 2020-01-19 08:14 UTC

This package is auto-updated.

Last update: 2024-09-19 18:54:01 UTC


README

这个验证包基于 Laravel 验证 illuminate/validation,因此我们可以在 Laravel 之外使用 Laravel 验证,并在我们的项目中自由使用。

入门

要求

  • PHP >= 5.6.4

安装

$ composer -v require yuliusardian/validation

或者编辑你的 composer.json 并安装它。

  "require": {
    "yuliusardian/validation": "1.0"
  }
$ composer -v install

基本用法

<?php
/**
 * since we installed it via composer,  we've to call autoload, this is optional 
 */
 require_once 'path/to/composer-vendor/autoload.php';
 
 use YuliusArdian\Validation\Factory;

 $validator = new Factory;
 $data  = ['name' => 'Hasna Putri Rahmatunissa', 'email' => 'hsnaputri'];
 $rules = ['name' => 'required|max:10', 'email' => 'required|email'];
 
 $validator = $validator->make($data, $rules);
 $errors    = $validator->errors()->all();
 var_dump($errors); 

从上面的代码中,它将返回错误

string(47) "The name may not be greater than 10 characters."
string(40) "The email must be a valid email address."

自定义语言

到目前为止,此包仅提供两种语言,查看 src/lang 目录,你会看到 en 和 id 文件夹。默认语言是 en。如果你愿意,可以自由贡献。以下是一些使用自定义语言的示例

<?php
$validator = new Factory('id');

更多详情

有关验证的更多详细信息,如自定义消息、可用方法、钩子等,请访问 Laravel 验证文档