pickup_contentsテーブルにfruit_idが存在したら422となるバリデーションRuleの例
DeleteFruitRequest.php
<?php namespace App\Http\Requests\CM\Fruit; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Http\Request; use App\Rules\CM\Fruit\IsPickUpFruitRule; class DeleteFruitRequest extends FormRequest { private $fruit_id; public function __construct( Request $request, IsPickUpFruitRule $is_pickup_fruit_rule ) { parent::__construct(); $this->fruit_id = $request->fruit_id; $this->is_pickup_fruit_rule = $is_pickup_fruit_rule; } /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { $fruit_id = $this->fruit_id; return [ 'fruit_id' => [ 'numeric', 'required', $this->is_pickup_fruit_rule, ], ]; } public function validationData() { return array_merge($this->request->all(), [ 'fruit_id' => $this->fruit_id, ]); } }
もくじ
validationData()
JSONのキーをfruit_idとしてバリデーションする為の処理
rules()
ここでカスタムRuleを割り当ててバリデーションを指定
カスタムRule
IsPickUpFruitRule.php
<?php namespace App\Rules\CM\Fruit; use Illuminate\Contracts\Validation\Rule; use App\Repositories\Pickup\PickupContentInterface; class IsPickUpFruitRule implements Rule { public function __construct( PickupContentInterface $i_pc ) { $this->i_pc = $i_pc; } public function passes($attribute, $value) { // ピックアップにfruit_idが含まれている場合は422エラー $pickup_content = $this->i_pc->findWhere(['fruit_id' => $value]); if ($pickup_content != null) { return false; } return true; } /** * Get the validation error message. * * @return string */ public function message() { return __('validation_client_administrator.messages.already_pickup_fruit_id'); } }
passes($attribute, $value)
- trueを返せばバリデーション通過
- falseで422エラーとなる
Ruleの中で他の値もまとめてバリデーションする例
Reqeust
<?php namespace App\Http\Requests\CM; use App\Rules\CM\DestroyUserManagementRule; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Http\Request; class DestroyUserManagementRequest extends FormRequest { protected $user_account_id; protected $destroy_user_management_rule; public function __construct( Request $request, DestroyUserManagementRule $destroy_user_management_rule ) { parent::__construct(); $this->user_account_id = $request->user_account_id; $this->destroy_user_management_rule = $destroy_user_management_rule; } /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } public function rules() { return [ 'user_account_id' => [ 'numeric', 'required', $this->destroy_user_management_rule, ] ]; } }
Rule
<?php namespace App\Rules\CM; use App\Repositories\Owner\OwnerInterface; use Illuminate\Contracts\Validation\Rule; use Illuminate\Http\Request; use App\Repositories\User\UserAccountInterface; use App\Repositories\User\UserInterface; class DestroyUserManagementRule implements Rule { public function __construct( Request $request, UserAccountInterface $i_ua, UserInterface $i_u, OwnerInterface $i_o ) { $this->request = $request; $this->i_ua = $i_ua; $this->i_u = $i_u; $this->i_o = $i_o; } /** * ・user_accounts.idが存在するか * ・users.user_account_idが存在するか * ・owners.default_auth_identifierが設定されているか * * @return boolean */ public function passes($attribute, $value): bool { $user_account = $this->i_ua->findWhere([ 'id' => $value, 'owner_id' => $this->request['owner_id'], ]); $user = $this->i_u->findWhere([ 'user_account_id' => $value, 'owner_id' => $this->request['owner_id'], ]); $owner = $this->i_o->find($this->request['owner_id']); if ($user_account == null || $user == null || !isset($owner->default_auth_identifier)) { return false; } return true; } public function message() { return __('validation.invalid_value'); } }
ファイルアップロードでのバリデーション