自定义验证不调用 passes() 函数
Custom validation not calling the passes() function
我正在尝试进行验证,以按照 Custom Validation Rules 中的步骤检查数组中是否至少提供了一项
Routes.php
Route::middleware(['auth:api', 'bindings'])->group(function () {
Route::prefix('api')->group(function () {
Route::apiResources([
'exam-papers/{examPaper}/questions' => ExamPaperQuestionsController::class
]);
});
});
ValidateArrayElementRule.php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class ValidateArrayElementRule implements Rule
{
public function __construct()
{
//
}
public function passes($attribute, $value)
{
echo "there";
return count($value) > 0;
}
public function message()
{
return 'At least one element is required!';
}
}
ExamPaperQuestionsController.php
public function store(ExamPaperQuestionStoreRequest $request, ExamPaper $examPaper)
{
return response()->json([])->setStatusCode(201);
}
在我的测试文件中我有
public function error_422_if_no_questions_provided()
{
Permission::factory()->state(['name' => 'create exam paper question'])->create();
$this->user->givePermissionTo('create exam paper question');
$this->actingAs($this->user, 'api')
->postJson('/api/exam-papers/' . $this->examPaper->id . '/questions', [])
->assertStatus(422);
}
ExamPaperQuestionStoreRequest.php
class ExamPaperQuestionStoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->user()->can('create exam paper question');
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
echo "HERE";
return [
'questions' => [new ValidateArrayElementRule],
'questions.*.description' => 'required'
];
}
}
测试失败
Expected status code 422 but received 201.
我可以看到记录了文本“HERE”,但没有记录“there”。为什么我的验证 passes()
函数没有被调用?
为什么不在验证中使用简单方法? :
$request->validate([
'title' => 'required|min:5|max:20',
'detail' => 'required',
'cat_image' => 'required',
]);
假设如果您的请求包含空值,那么它不会调用自定义验证。所以你必须添加 required filed 以确保请求有 key questions
'questions' => ["required",new ValidateArrayElementRule]
Incase 问题是可选的,如果输入,则至少需要两到三个项目,然后您可以在验证时使用 required。
默认laravel支持数组最小值
'questions' => ["required","array","min:1"]
我正在尝试进行验证,以按照 Custom Validation Rules 中的步骤检查数组中是否至少提供了一项 Routes.php
Route::middleware(['auth:api', 'bindings'])->group(function () {
Route::prefix('api')->group(function () {
Route::apiResources([
'exam-papers/{examPaper}/questions' => ExamPaperQuestionsController::class
]);
});
});
ValidateArrayElementRule.php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class ValidateArrayElementRule implements Rule
{
public function __construct()
{
//
}
public function passes($attribute, $value)
{
echo "there";
return count($value) > 0;
}
public function message()
{
return 'At least one element is required!';
}
}
ExamPaperQuestionsController.php
public function store(ExamPaperQuestionStoreRequest $request, ExamPaper $examPaper)
{
return response()->json([])->setStatusCode(201);
}
在我的测试文件中我有
public function error_422_if_no_questions_provided()
{
Permission::factory()->state(['name' => 'create exam paper question'])->create();
$this->user->givePermissionTo('create exam paper question');
$this->actingAs($this->user, 'api')
->postJson('/api/exam-papers/' . $this->examPaper->id . '/questions', [])
->assertStatus(422);
}
ExamPaperQuestionStoreRequest.php
class ExamPaperQuestionStoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->user()->can('create exam paper question');
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
echo "HERE";
return [
'questions' => [new ValidateArrayElementRule],
'questions.*.description' => 'required'
];
}
}
测试失败
Expected status code 422 but received 201.
我可以看到记录了文本“HERE”,但没有记录“there”。为什么我的验证 passes()
函数没有被调用?
为什么不在验证中使用简单方法? :
$request->validate([
'title' => 'required|min:5|max:20',
'detail' => 'required',
'cat_image' => 'required',
]);
假设如果您的请求包含空值,那么它不会调用自定义验证。所以你必须添加 required filed 以确保请求有 key questions
'questions' => ["required",new ValidateArrayElementRule]
Incase 问题是可选的,如果输入,则至少需要两到三个项目,然后您可以在验证时使用 required。
默认laravel支持数组最小值
'questions' => ["required","array","min:1"]