gpt4 book ai didi

javascript - 通过 ajax : showing error message throws error 验证 Laravel 数组

转载 作者:行者123 更新时间:2023-11-30 20:55:28 25 4
gpt4 key购买 nike

尝试通过 FormRequest 验证在 laravel 中验证数组

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class VendorStoreRoom 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 [
'roomtype_id' => 'required',
'price' => 'required|digits_between:1,8',
'amenities' => 'required',

//'max_children' => 'required',
'max_adult' => 'required',
//'capacity' => 'required',
'floor.*' => 'required',
'room_count' => 'min:1|max:50',
'room_number.*' => 'required',
//'room_number' => 'required|max:5',
'image' => 'required',
];
}


public function messages()
{
return [
'roomtype_id.required' => 'Please select a room type',

'price.required' => 'Price cannot be empty',
'price.digits_between' => 'Price cannot exceed 8 digits',

'amenities.required' => 'Atleast select one amenities',

//'max_children.required' => 'Vendor name cannot be empty',

'max_adult.required' => 'Maximum adult can not be empty',

//'capacity.required' => 'Vendor name cannot be empty',

'floor.*.required' => 'Please select a floor',

'room_count.min' => 'Room count cannot be empty',
'room_count.max' => 'Room count cannot exceed 50',

'room_number.*.required' => 'Room number cannot be empty',
//'room_number.max' => 'Room number cannot exceed 5 characters',

'image.required' => 'Atleast select one image',
/*'image.*.mimes' => 'Image Must be JPEG, JPG or PNG',
'image.*.min' => 'Image size must be more than 10 kb',
'image.*.max' => 'Image size cannot exceed 300 mb', */


];
}
}

这是我得到的回复:

{"message":"The given data was invalid.","errors":{"roomtype_id":["Please select a room type"],"price":["Price cannot be empty"],"amenities":["Atleast select one amenities"],"image":["Atleast select one image"],"floor.0":["Please select a floor"],"room_number.0":["Room number cannot be empty"]}}

我在 jquery 中发现了这些错误,并像下面这样:

这很好用:$("#amenities_error").text(data.responseJSON.errors.amenities);

但不是这个:$("#floor_error").text(data.responseJSON.errors.floor.0);我怎样才能做到这一点?

有什么我可以做的,比如 data.responseJSON.errors."floor.0"或 data.responseJSON.errors.{floor.0}

最佳答案

floor.0 是导致此错误的关键字符串。但是,javascript 将 . 视为属性分隔符。因此,javascript 搜索具有属性 0 的对象 floor
要解决此问题,您必须为此键使用数组语法。

$("#floor_error").text(data.responseJSON.errors["floor.0"]);

关于javascript - 通过 ajax : showing error message throws error 验证 Laravel 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47709755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com