我在试着用拉威尔进行表格验证. 我的表单中有一个名为"Category"的输入文本字段,字段名为"cat",缩写为"cat".

我这样定义了验证规则.

public static $rules=array(
         "name"=>"required|min:3",
          "cat"=>"required"
           );

当验证失败时,我会收到这样的错误消息

The name field is required.
The cat field is required.

But i want to display it as "The category field is required" instead of 'cat'.
How can i change 'cat' to 'Category' in error message ?.

推荐答案

您可以为您的字段指定自定义错误消息,如下所示.

$messages = array(
    'cat.required' => 'The category field is required.',
);

$validator = Validator::make($input, $rules, $messages);

Please see Custom Error Messages section in laravel documentation for more information.

Or you can keep a mapping for your field names like below. And you can set those into you validator. So you can see descriptive name instead of real field name.

$attributeNames = array(
   'name' => 'Name',
   'cat' => 'Category',     
);

$validator = Validator::make ( Input::all (), $rules );
$validator->setAttributeNames($attributeNames);

Laravel相关问答推荐

当使用Craftable PRO时,Wysiwig是什么时候生成的?

自定义验证在表单请求上始终返回 true laravel

如何解决此 Backblaze B2 S3 兼容 API 错误?

Inertiajs - 使用链接组件添加记录但无法在不使用表单的情况下清除文本区域

如何返回Blade Laravel 中的按钮?

Laravel + SQLite = SQLSTATE[HY000]一般错误:8次try 写入只读数据库

如何监控 Laravel 队列是否正在运行?

Laravel 查询构建器 - 如何按别名分组,或进行原始 groupBy

Laravel 5.x 中 onUpdate / onDelete 的可用操作

如何在 Laravel 5.2 中手动发送密码重置请求?

laravel Blade中多选选项中的旧值

数组的旧输入?

约定表名(带下划线)

Class类 App\Http\Controllers\Auth\Request 不存在.

找不到框laravel/homestead

如何在 Laravel Mix 中将公共路径更改为包含下划线的内容?

基于日期的 Laravel 日志(log)文件

使用 Laravel Mix 合并多个文件

Laravel or where

有没有办法让表名自动添加到 Eloquent 查询方法中?