如何在Laravel中使用带有自定义消息的规则对象验证图像数组类型
实际上,我试图创建rule对象,它能够验证图像数组中的每种图像类型,不仅足够,而且我必须在rule对象的override message函数中显示自定义消息如何在Laravel中使用带有自定义消息的规则对象验证图像数组类型,laravel,laravel-validation,Laravel,Laravel Validation,实际上,我试图创建rule对象,它能够验证图像数组中的每种图像类型,不仅足够,而且我必须在rule对象的override message函数中显示自定义消息 <?php namespace App\Rules; use Illuminate\Contracts\Validation\Rule; class ImagesArray implements Rule { /** * Create a new rule instance. * * @re
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class ImagesArray implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return [$attribute => 'mimes:jpeg,jpg,png' ];
here i need to validate these file types.
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The validation error message.';
here, I need to show my custom messgae.
}
}
您应该使用请求。
例如,createq-request类:php-artisan-make:request-MyRequest
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class MyRequest 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 [
'image' => 'mimes:jpeg,jpg,png',
];
}
public function messages()
{
return [
'image.mimes' => 'This image is not supported.',
];
}
让我知道这是否有用。谢谢 您应该使用请求。
例如,createq-request类:php-artisan-make:request-MyRequest
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class MyRequest 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 [
'image' => 'mimes:jpeg,jpg,png',
];
}
public function messages()
{
return [
'image.mimes' => 'This image is not supported.',
];
}
让我知道这是否有用。谢谢 验证数组或嵌套参数时,应在访问特定数组索引的规则中使用
。但是,如果您想对该数组上的每个索引应用规则,可以使用*
$validator=validator::make($request->all()[
'image.*'=>'mimes:jpeg,jpg,png',
], [
“image.*”=>“无效的文件类型。”,
]);
或者如果你使用的是申请表
公共功能规则(){
返回[
'image.*'=>'mimes:jpeg,jpg,png',
];
}
公共职能部门{
返回[
“image.*”=>“无效的文件类型。”,
];
}
有关详细信息,请参阅
验证数组或嵌套参数时,应在访问特定数组索引的规则中使用
。但是,如果您想对该数组上的每个索引应用规则,可以使用*
$validator=validator::make($request->all()[
'image.*'=>'mimes:jpeg,jpg,png',
], [
“image.*”=>“无效的文件类型。”,
]);
或者如果你使用的是申请表
公共功能规则(){
返回[
'image.*'=>'mimes:jpeg,jpg,png',
];
}
公共职能部门{
返回[
“image.*”=>“无效的文件类型。”,
];
}
有关详细信息,请参阅
这是表单请求,如果我必须为此使用Laravel规则对象呢?这是表单请求,如果我必须为此使用Laravel规则对象呢?