https://readouble.com/laravel/5.8/ja/validation.html
下記のValidatorの雛形があったとする
app/Validators/Member/ServiceValidator.php
<?php namespace App\Validators\Member; use Illuminate\Validation\Factory as Validator; /** * Class CreateUpdateValidator. * * @package namespace App\Validators\Member; */ class ServiceValidator { const SECTION_ID_RULE = "member_section_id_validation:%s"; protected $rules = [ 'member_id' => 'required|member_id_and_module:member', ]; protected $additional_rules = [ "update" => [ 'member.*' => ['is_fill_langmember:section_id'], 'member.*.*.enable' => ['required', 'boolean'], ], "common_update" => [ 'member.layout' => ['required', 'array'], 'member.layout.*' => ['is_fill_langmember:section_id'], 'member.setting' => ['is_fill_langmember:section_id'], 'member.setting.*.favorite' => ['boolean'], ], "section" => [ "section_id" => ['required'] ], "movie" => [ 'file' => ['required', 'max:2097152', 'mimetypes:video/mp4'], ], ]; public function __construct(Validator $v) { $this->v = $v; } public function commonValidate(array $data) { return $this->validate($data); } public function updateValidate(array $data) { if (!is_null($data["section_id"])) { $this->additional_rules["update"] = array_merge($this->additional_rules["update"], $this->additional_rules["section"]); } return $this->validate($data, "update"); } public function commonUpdateValidate(array $data) { return $this->validate($data, "common_update"); } public function movieValidate(array $data) { return $this->validate($data, "movie"); } protected function validate(array $data, string $additional_rule = null) { $rules = !is_null($additional_rule) && isset($this->additional_rules[$additional_rule]) ? array_merge($this->rules, $this->additional_rules[$additional_rule]) : $this->rules; $this->validator = $this->v->make($data, $rules); return $this->validator->validate(); } }
もくじ
updateValidate()を拡張する
- 更新データの配列がユニークであることを判定するルールを追加してみる
app/Validators/Member/UniqueKeyValidator.php
<?php namespace App\Validators\Member; use Illuminate\Http\Request; /** * Class UniqueKeyValidator * * @package namespace App\Validators\Member; */ class UniqueKeyValidator { public function __construct( Request $request ) { $this->request = $request; } /** * keyがユニークであることをチェックする */ public function validate($attribute, $value, $parameters, $validator) { $keys = []; $unique_keys = []; if (!is_array($value)) { return false; } foreach ($value as $item) { $keys[] = $item['key']; } $unique_keys = array_unique($keys); // 元の配列とユニークにした配列で比較する if ($keys != $unique_keys) { return false; } return true; } }
適用
<?php namespace App\Validators\Member; use Illuminate\Validation\Factory as Validator; /** * Class CreateUpdateValidator. * * @package namespace App\Validators\Member; */ class ServiceValidator { + // メンバー詳細のレイアウトのkeyがユニークであるかをチェック + const UNIQUE_KEY_RULE = 'unique_key_validation'; protected $rules = [ 'member_id' => 'required|member_id_and_module:member', ]; ・・・ public function updateValidate(array $data) { if (!is_null($data["section_id"])) { $this->additional_rules["update"] = array_merge($this->additional_rules["update"], $this->additional_rules["movie"]); } + $this->additional_rules["update"] = array_merge($this->additional_rules["update"], self::UNIQUE_KEY_RULE); return $this->validate($data, "update"); } ・・・
クラスを認識させる
app/Http/Providers/AppServiceProvider.php
<?php namespace App\Providers; ・・・ use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Validator; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { ・・・ // メンバー詳細レイアウトのkeyがユニークであることをチェック Validator::extend( 'unique_key_validation', '\App\Validators\Member\UniqueKeyValidator@validate' ); ・・・ }
読み込ませる
laravel > php artisan composer dumpa
利用イメージ
MemberController
<?php namespace App\Http\Controllers\Member; use App\Http\Controllers\Controller; use App\Services\MeberService; use Illuminate\Http\Request; class MemberController extends Controller { public function __construct( MemberService $service ) { $this->service = $service; } ・・・ public function update(Request $request) { $content_json = json_decode($request->getContent(), true); $this->service->updateValidate($content_json["data"], $section_id); $this->service->updateContent("member", $content_json["data"], $section_id); return response(null, 204); }
MemberService
<?php namespace App\Services\Member; ・・・ use App\Validators\Member\ServiceValidator; class MemberrService extends SectionAbstractService implements MemberDesignServiceInterface, PlayAbleInterface { ・・・ public function __construct( ServiceValidator $validator ) { $this->validator = $validator; } ・・・ public function updateValidate($content_data, $section_id = null) { $this->validator->updateValidate([ "update" => $content_data ]); } ・・・