php - 浅层嵌套资源中的 Laravel 请求验证
问题描述
我有两个资源
组织
组织用户
鉴于我想在这两种资源中创建并且创建必须遵循特定要求,我正在使用请求规则()和属性()。
例如,由于组织需要有一个唯一的名称,我正在使用
public function rules()
{
return [
'name' => [
'required', 'min:3', Rule::unique((new Organization)->getTable())->ignore($this->route()->organization->id ?? null)
],
(...)
];
}
这很好用。采用相同的流程来验证组织用户
class OrganizationUserRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'user_id' => [
'required', 'exists:'.(new User)->getTable().',id', Rule::unique((new OrganizationUser)->getTable())->ignore($this->route()->user->id ?? null)
],
(...)
];
}
/**
* Get the validation attributes that apply to the request.
*
* @return array
*/
public function attributes()
{
return [
'user_id' => 'user',
(...)
];
}
}
那么我尝试添加的任何用户都会给我一个
该用户已被占用。
无论使用哪个用户。
OrganizationUser 模型是
class OrganizationUser extends Model
{
protected $fillable = [
'user_id', (...)
];
/**
* Get the user
*
* @return \App\User
*/
public function user()
{
return $this->belongsTo(User::class);
}
(...)
}
此外,OrganizationUserController 是
/**
* Show the form for creating a new organization
*
* @return \Illuminate\View\View
*/
public function create($id, Organization $model1, User $model2)
{
return view('organizations.users.create', [
'id'=> $id,
'organizations' => $model1::where('id',$id)->get(['id', 'name']),
'portal_users' => $model2->all(),
]);
}
和 create.blade.php 中的那个字段
<div class="form-group{{ $errors->has('user_id') ? ' has-danger' : '' }}">
<label class="form-control-label" for="input-user_id">{{ __('User') }}</label>
<select name="user_id" id="input-organization" class="form-control{{ $errors->has('user_id') ? ' is-invalid' : '' }}" placeholder="{{ __('User') }}" required>
@foreach ($portal_users as $portal_user)
<option value="{{ $portal_user->id }}" {{ $portal_user->id == old('user_id') ? 'selected' : '' }}>{{ $portal_user->name }}</option>
@endforeach
</select>
@include('alerts.feedback', ['field' => 'user_id'])
</div>
解决方案
为了解决它,我已经完成了接下来的步骤
- 创建提供者
php artisan make:provider UniqueOrgUserServiceProvider
- 在那个提供者
<?php
namespace App\Providers;
use App\OrganizationUser;
use Illuminate\Support\ServiceProvider;
class UniqueOrgUserServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
\Validator::extend('uniqueorguser',
function($attribute, $value, $parameters, $validator)
{
$value1 = (int)request()->get($parameters[0]);
if (is_numeric($value) && is_numeric($value1))
{
return (!(OrganizationUser::where($attribute, $value)
->where($parameters[0], $value1)
->count() > 0));
}
return false;
});
}
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
}
- 在中注册提供程序
config/app.php
'providers' => [
/*
* Application Service Providers...
*/
App\Providers\UniqueOrgUserServiceProvider::class,
],
- 更改
OrganizationUserRequest
为
class OrganizationUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => [
'required', 'min:3'
],
'is_admin' => [
'required','boolean'
],
'organization_id' => [
'required', 'exists:'.(new Organization)->getTable().',id'
],
'user_id' => [
'required', 'uniqueorguser:organization_id', 'exists:'.(new User)->getTable().',id',
]
];
}
/**
* Get the validation attributes that apply to the request.
*
* @return array
*/
public function attributes()
{
return [
'user_id' => 'user',
'organization_id' => 'organization'
];
}
public function messages()
{
return [
'user_id.uniqueorguser' => 'The user already exists in this organization!'
];
}
}
推荐阅读
- javascript - 使用 setInterval React 提高互联网速度
- algorithm - R307 指纹传感器可处理超过 1000 个指纹
- ios - 在 deinit 中的 NotificationCenter.default.removeObserver(self),为什么 deinit 它甚至被调用?
- mongodb - 此容器使用 Mongoose Schema Typescript 遮蔽了“this”的外部值
- python - 如何解决此错误:AttributeError: 'numpy.ndarray' object has no attribute 'crop'
- amazon-web-services - 了解 AWS 安全组上的 Source 属性
- java - 这个java代码中可能的竞争条件?
- macos - 三列布局的 SwiftUI macOS 工具栏图标对齐
- java - 使用自定义分隔符发布 Maven 工件
- docker - 单个文件挂载到 docker 容器不起作用