英文:
How to validate 2 form request in the same controller in laravel
问题
Sure, here are the translated code parts:
我正在验证信用卡,为此我创建了两个表单请求:
`php artisan make:request StoreAmexRequest`
`php artisan make:request StoreVisaRequest`
如何在同一个控制器中使用它们?
public function store(Request $request)
{
if ($request->credit_card['number'][0] == 3) {
new StoreAmexRequest(),
}
if ($request->credit_card['number'][0] == 4) {
new StoreVisaRequest(),
]);
}
}
我的代码不起作用,$request变量未接收`StoreAmexRequest()`。
我正在创建信用卡验证器,AMEX卡验证器与VISA卡不同,因为AMEX卡有15位卡号和4位CVV码,而VISA卡有16位卡号。
是否需要使用`php artisan make:request`,因为这是用于返回JSON响应的API?
`\app\Http\Requests\StoreAmexRequest`:
public function authorize()
{
return true;
}
public function rules()
{
$year = date('Y');
return [
'credit_card.name' => ['required', 'min:3'],
'credit_card.number' => ['bail', 'required', 'min:15', 'max:16', new CredirCardRule],
'credit_card.expiration_month' => ['required', 'digits:2'],
'credit_card.expiration_year' => ['required', 'integer', 'digits:4', "min:$year"],
'credit_card.cvv' => ['required', 'integer', 'digits_between:3,4']
];
}
public function failedValidation(Validator $validator)
{
throw new HttpResponseException(response()->json([
$validator->errors(),
]));
}
Please note that I've translated the code parts and removed the request for translation in your request. If you have any specific questions or need further assistance with the code, please let me know.
英文:
I am validating a credit card, for which I created two form requests:
php artisan make:request StoreAmexRequest
php artisan make:request StoreVisaRequest
How can I use them in the same controller?
public function store(Request $request)
{
if ($request->credit_card['number'][0] == 3) {
new StoreAmexRequest(),
}
if ($request->credit_card['number'][0] == 4) {
new StoreVisaRequest(),
]);
}}
My code doesn't work, the $request variable doesn't receive the StoreAmexRequest()
.
I am creating a credit card validator, the AMEX card validator is different from VISA cards, since AMEX is 15 digits and the CVV is 4 digits, and in VISA it is 16 digits.
It is necessary to use php artisan make:request since it is for an API that returns the response in JSON?
\app\Http\Requests\StoreAmexRequest
:
public function authorize()
{
return true;
}
public function rules()
{
$year = date('Y');
return [
'credit_card.name' => ['required', 'min:3'],
'credit_card.number' => ['bail', 'required', 'min:15', 'max:16', new CredirCardRule],
'credit_card.expiration_month' => ['required', 'digits:2'],
'credit_card.expiration_year' => ['required', 'integer', 'digits:4', "min:$year"],
'credit_card.cvv' => ['required', 'integer', 'digits_between:3,4']
];
}
public function failedValidation(Validator $validator)
{
throw new HttpResponseException(response()->json([
$validator->errors(),
]));
}
答案1
得分: 3
你可以只使用一个表单请求来验证两者。
public function store(StoreCreditCardRequest $request)
{
YourCreditCardModel::create($request->validated());
}
然后将规则拆分到表单请求内部。
public function rules(): array
{
if ($this->credit_card['number'][0] == 3) {
return $this->amexRules();
}
if ($this->credit_card['number'][0] == 4) {
return $this->visaRules();
}
}
protected function amexRules(): array
{
return [
// 针对美国运通卡的验证规则
];
}
protected function visaRules(): array
{
return [
// 针对维萨卡的验证规则
];
}
英文:
You could just use a single form request that validates both.
public function store(StoreCreditCardRequest $request)
{
YourCreditCardModel::create($request->validated());
}
And split the rules inside the form request
public function rules(): array
{
if ( $this->credit_card['number'][0] == 3 ) {
return $this->amexRules();
}
if ( $this->credit_card['number'][0] == 4 ) {
return $this->visaRules();
}
}
protected function amexRules(): array
{
return [
// your validation rules for amex cards
];
}
protected function visaRules(): array
{
return [
// your validation rules for visa cards
];
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论