سلام من از کد زیر توی کلاس ریکوئست برای ولیدیشن دیتا استفاده کردم زمانی که از متد POST استفاده میکنم مشکلی نداره اما وقتی متد رو به PUT تغییر میدم ولیدیشن کار نمی کنه
کلاس ریکوئست :
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class VenueRequest extends FormRequest
{
/**
* 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()
{
return [
'name' => ['required', 'string', 'max:255'],
'description' => ['string', 'max:255'],
'address' => ['required', 'string', 'max:255'],
'city' => ['required', 'string', 'max:255'],
'state' => ['required', 'string', 'max:255'],
'country' => ['required', 'string', 'max:255'],
'latitude' => ['regex:/^-?([1-8]?[1-9]|[1-9]0)\.{1}\d{1,6}/', 'max:255'],
'latitude' => ['regex:/^-?([1-8]?[1-9]|[1-9]0)\.{1}\d{1,6}/', 'max:255'],
'welcome_title' => ['string', 'max:255'],
'welcome_body' => ['string', 'max:255'],
'welcome_link' => ['string', 'max:255'],
];
}
}
کنترلر :
public function update(VenueRequest $request, Venue $venue)
{
$data = $venue->update($request);
return AM::result($data)->make();
}
فایل api.php
:
Route::put('beacons/{beacon}', 'BeaconController@update')->middleware('auth:api');
فیلدهای form-data در postman:
name:لوکیشن 1
description:پلاک 6544654
address: آدرس سیبسیب یبل بلس ، لبلسی
city:تهران
state:تهران
country:ایران
latitude:35.764327
longitude:51.470390
welcome_title:شرکت 34234
welcome_body:خوش آمدید
welcome_link:http://google.ir
خروجی Postman:
{
"message": "The given data was invalid.",
"errors": {
"name": [
"The name field is required."
],
"address": [
"The address field is required."
],
"city": [
"The city field is required."
],
"state": [
"The state field is required."
],
"country": [
"The country field is required."
]
}
}
لاراول بهم ارور بالا رو نشون میده در صورتی که من داخل form-data پستمن همه ی فیلدا رو وارد کردم.
بهترین پاسخ
انتخاب شده توسط Javad Ezaz
Javad Ezaz
1 سال پیش
راه حل رو پیدا کردم.
زمانی که از ریکوئست PUT استفاده می کنیم باید توی postman قسمت Body به جای form-data
از x-www-form-urlencoded
استفاده کنیم.