Help us understand the problem. What is going on with this article?

バリデーションの拡張

背景

laravelのバリデーションにある
alpha: 中身が全部英字かどうか
alpha_dash: 英字または-または_であるかどうか
alpha_num: 英数字であるかどうか

のバリデーションを行うと、日本語のままでも行けてしまいます。

困ってますね・・・

1.独自のバリデーションを作成する

App\Validator\CustomValidator.php
namespace App\Validator;

use \Illuminate\Validation\Validator;

class CustomValidator extends Validator
{

    /**
     * [validateZipcode 中身が全部英字かどうか]
     * @param  [type] $attribute  [description]
     * @param  [type] $value      [description]
     * @param  [type] $parameters [description]
     * @return [type]             [description]
     */
    public function validateIsAlpha($attribute, $value, $parameters)
    {
        return preg_match('/^[a-zA-Z]+$/', $value);
    }

    /**
     * [validateIsAlphaDash 英字または-または_であるかどうか]
     * @param  {[type]} $attribute  [description]
     * @param  {[type]} $value      [description]
     * @param  {[type]} $parameters [description]
     * @return {[type]}             [description]
     */
    public function validateIsAlphaDash($attribute, $value, $parameters)
    {
        return preg_match('/^[a-zA-Z\-_]+$/', $value);
    }

    /**
     * [validateIsAlphaDash 英数字であるかどうか]
     * @param  {[type]} $attribute  [description]
     * @param  {[type]} $value      [description]
     * @param  {[type]} $parameters [description]
     * @return {[type]}             [description]
     */
    public function validateIsAlphaNum($attribute, $value, $parameters)
    {
        return preg_match('/^[0-9a-zA-Z]+$/', $value);
    }
}

第一引数$attributeはバリデーションの名前です。
第二引数$valueはバリデーションする値、第三引数($parameters)はオプションで渡したい値です。
第三引数$parametersで値を渡したい場合は以下の様にモデルで記述します。

'age'  => 'digits_between:1,99'

2.サービスプロバイダー作成と追加

php artisan make:provider ValidatorServiceProvider
App\Providers\ValidatorServiceProvider.php
namespace App\Providers;

use App\Validator\CustomValidator;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;

class ValidatorServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
      Validator::resolver(function ($translator, $data, $rules, $messages, $attributes) {
          return new CustomValidator($translator, $data, $rules, $messages, $attributes);
      });
    }
}

最後

config/app.php
'providers' => [
    // 追加
    App\Providers\ValidatorServiceProvider::class,
];

以上です。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away