経緯
alpha_dash
が日本語に対応していない。
日本語が入力されていても、バリデーション対象にならない。
https://readouble.com/laravel/7.x/ja/validation.html
実現したいこと
alpha_dash
をオーバーライドし、日本語対応したい。
以下のような記述で実現出来る方法はありますでしょうか?
php
1Validator::macro('alpha_dash', (string $value) => preg_match('/^[A-Za-z\d_-]+$/', $value);
追記
現状の実装は、以下のようにしています。
php
1use Illuminate\Contracts\Validation\Rule; 2 3class AlphaDashRule implements Rule 4{ 5 /** 6 * Determine if the validation rule passes. 7 * 8 * @param string $attribute 9 * @param mixed $value 10 * @return bool 11 */ 12 public function passes($attribute, $value) 13 { 14 return preg_match('/^[A-Za-z\d_-]+$/', $value); 15 } 16}
php
1class StoreRequest extends FormRequest 2{ 3 // ... 4 5 /** 6 * Get the validation rules that apply to the request. 7 * 8 * @return array 9 */ 10 public function rules() 11 { 12 return [ 13 'name' => 'required|max:255', 14 'slug' => ['required', 'max:50', new AlphaDashRule], 15 ]; 16 } 17} 18
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。