Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用laravel规则设置laravel自定义验证消息_Php_Laravel - Fatal编程技术网

Php 如何使用laravel规则设置laravel自定义验证消息

Php 如何使用laravel规则设置laravel自定义验证消息,php,laravel,Php,Laravel,让我先显示我的代码。这是我的控制器功能代码 public function save(Request $request) { try { $this->validate($request, Venue::rules()); // Validation Rules $venue = Venue::saveOrUpdate($request); if($venue !== false) { if($reques

让我先显示我的代码。这是我的控制器功能代码

public function save(Request $request) {
    try {
        $this->validate($request, Venue::rules()); // Validation  Rules 
        $venue = Venue::saveOrUpdate($request);
        if($venue !== false) {
            if($request->get('continue', false)) {
                return redirect()->route('admin.venue.edit', ['id' => $venue->id])->with('success', trans('admin.venue.save_success'));
            } else {
                return redirect()->route('admin.venue.index')->with('success', trans('admin.venue.save_success'));
            }
        } else {
            return back()->with('error', "Unable to save venue")->withInput();
        }

    } catch (\Exception $ex) {
        return back()->with('error', "Unable to save venue")->withInput();
    }
}
public static function rules($id = '') {
    return [
        'name' => 'required|string|max:255',
        'logo' => 'required',
        'status' => 'required|string|in:' . implode(",", Venue::STATUSES),
        'venue_type_id' => 'required|string|not_in:0',
         'client_id' => 'required|string|not_in:0',
    ];
}
这是我的模型函数代码

public function save(Request $request) {
    try {
        $this->validate($request, Venue::rules()); // Validation  Rules 
        $venue = Venue::saveOrUpdate($request);
        if($venue !== false) {
            if($request->get('continue', false)) {
                return redirect()->route('admin.venue.edit', ['id' => $venue->id])->with('success', trans('admin.venue.save_success'));
            } else {
                return redirect()->route('admin.venue.index')->with('success', trans('admin.venue.save_success'));
            }
        } else {
            return back()->with('error', "Unable to save venue")->withInput();
        }

    } catch (\Exception $ex) {
        return back()->with('error', "Unable to save venue")->withInput();
    }
}
public static function rules($id = '') {
    return [
        'name' => 'required|string|max:255',
        'logo' => 'required',
        'status' => 'required|string|in:' . implode(",", Venue::STATUSES),
        'venue_type_id' => 'required|string|not_in:0',
         'client_id' => 'required|string|not_in:0',
    ];
}
所以现在当我提交表单验证时显示消息。我想更改此邮件。如何更改

让我显示带有验证消息的表单:

您可以像这样添加自定义错误

$validation->errors()->add('error_input', 'error text');

return redirect()->back()->withInput()->withErrors($validation);


您可以通过以下方式自定义验证消息:

转到
resources->lang->en->validation.php

你看

'custom' => [
        'attribute-name' => [
            'rule-name' => 'custom-message',
        ],
    ],

根据需要编辑这些信息。

您可以通过覆盖
messages()
方法自定义
表单请求所使用的
错误信息。在
场地
课堂上添加自定义
消息
,如下所示-

public static function messages($id = '') {
return [
    'name.required' => 'You must enter your name',
    'logo.required' => 'You must upload logo',
    'key.rules' => 'your messages'
];
在控制器上添加
消息
作为第三个
参数
如-

$this->validate($request, Venue::rules(), Venue::messages());

这是我做这件事的方式,可以作为指导。从您的表单中,您基本上有4个输入字段,让我们假设它们被命名为名称、客户、徽标和场地类型。控制器中验证表单请求的函数如下所示:
注意-你应该把- 使用验证程序; 使用\Http\Request-在班上名列前茅

public function validateFormRequest($request){ try { //specify your custom message here $messages = [ 'required' => 'The :attribute field is required', 'string' => 'The :attribute must be text format', 'file' => 'The :attribute must be a file', 'mimes' => 'Supported file format for :attribute are :mimes', 'max' => 'The :attribute must have a maximum length of :max', ]; $validator = Validator::make($request->all(), [ 'name' => 'required|string|max:75', 'client' => 'required|string|max:75', 'logo' => 'required|file|mimes:jpeg,png,jpg,gif', 'venue_type' => 'required|string', ], $messages); if($validator->fails()){ // Validation Failed..log errors or Return Errors to view/blade } else{ // Validation passed..Return true or positive info. i.e request can be saved } }catch (Exception $ex){ //Log your errors or return some error message to your view/blade } } 公共函数validateFormRequest($request){ 尝试 { //在此处指定自定义消息 $messages=[ '必需'=>'属性字段是必需的', '字符串'=>'属性必须为文本格式', 'file'=>'属性必须是文件', 'mimes'=>'支持的:属性文件格式为:mimes', 'max'=>'属性的最大长度必须为:max', ]; $validator=validator::make($request->all()[ 'name'=>'必需|字符串|最大值:75', '客户端'=>'必需|字符串|最大值:75', 'logo'=>'必需|文件|模拟:jpeg、png、jpg、gif', “场馆类型”=>“必需”|字符串”, ](),; 如果($validator->fails()){ //验证失败..记录错误或将错误返回到视图/刀片服务器 }否则{ //验证通过..返回真实或肯定信息。即,可以保存请求 } }捕获(例外$ex){ //记录错误或向视图/刀片返回一些错误消息 } }
请尝试FormRequest(),并参考标题“自定义错误消息”
。您可以将此错误添加到验证中,并返回redirect()->back()->withInput()->withErrors($validation);有人可以发布可编辑代码吗?因为我gt error
未定义变量:验证
所以如果我的表单中有15个字段..那么我需要在返回时设置的所有内容?使用Illumb\Support\Facades\Validator$validation=Validator::make($request->all(),[‘用户名’=>‘必需’、‘用户名’=>‘必需’、‘用户电话’=>‘必需’、‘用户电子邮件’=>‘必需’、‘用户协议’=>‘必需’、‘地址’=>‘必需’、‘数量’=>‘必需’、‘整数’;这是自定义验证器示例,您首先使用此示例,然后添加自定义验证器是否可以在我在模型上创建的规则上添加新的自定义消息。Gt答案。thanxquick question
'client_id'=>'required | string | not_in:0'
是我设置“client_id.required'=>'Select client'时的id,它不起作用。try dd(Input::all())在验证之前,请查看您作为客户机id得到的内容
“客户机id”=>“0”
gt。如果未选择客户机,那么它肯定不是空的,这就是为什么没有必需的验证错误。是否有任何方法可以设置,如果id为0,则在自定义消息中将其计为null,在自定义消息中我们定义name.required类似于客户机id.required.somethin.类似这样的东西?