Laravel自定义验证错误,如何优雅地转换成前端长尾?

2026-04-02 05:081阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计882个文字,预计阅读时间需要4分钟。

Laravel自定义验证错误,如何优雅地转换成前端长尾?

原文链接:https://vien.tech/article/163

前 言:Laravel Validator 默认返回的是英文的提示消息,而大部分情况下我们需要自定义错误返回提示信息。本文将介绍如何自定义错误消息。

自定义错误消息:

1. 定义错误消息常量在 application/translations 目录下创建一个语言文件,例如 en.php,并在其中添加自定义错误消息常量:

return [ 'validation'=> [ 'custom'=> [ 'max_length'=> 'The :attribute must not be greater than :max characters.', // 其他自定义错误消息 ], ],];

2. 在验证器中使用自定义错误消息在验证器类中,使用 $message 属性来指定自定义错误消息:

public function rules(){ return [ 'name'=> 'required|max:255', 'email'=> 'required|email|max:255', ];}

public function messages(){ return [ 'name.max'=> trans('validation.custom.max_length', ['attribute'=> 'name', 'max'=> 255]), // 其他自定义错误消息 ];}

3. 在前端使用自定义错误消息在 Vue 或其他前端框架中,使用 Laravel 的验证库进行验证时,错误消息将自动显示为自定义的中文提示:

以上就是在 Laravel 中自定义错误消息的方法,通过这种方式,您可以方便地实现自定义的错误提示信息,提升用户体验。

原文链接:vien.tech/article/163

Laravel Validator 默认返回的是英文的提示消息,而大多数情况我们需要自定义错误返回提示消息,本文将介绍一下如何自定义错误消息,并在前端展示。

自定义错误消息

别怪我太直接,代码奉上

$messages = [ 'phone.unique' => '重复的电话号码', 'required' => '请将信息填写完整', ]; $this->validate($request, [ 'phone' => 'required|unique:table_name', 'name' => 'required', ], $messages);

是不是很简单呀,只需要在validate()方法参数里面加个提示信息数组就好了,数组的key就是字段.验证方式或者直接验证方式,很显然,后者是应用于所有的,前者是应用于某一字段。

当然,你可能不是用的这种方式,其他验证器的也是一样的,看这里:

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

是的,这样写也是传第三个参数,跟上面的方式是一样的。

错误提示前端展示

Laravel自定义验证错误,如何优雅地转换成前端长尾?

接下来讲一下前端如何展示这些错误提示呢,来让我们挨个打印一下

@if ($errors->any()) @foreach ($errors->all() as $error) <div class="center alert alert-danger alert-dismissible fade show" role="alert"> <strong>遇到错误: </strong> {{ $error }} <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> @endforeach @endif

原谅我前端太差,所以用了bootstrap的样式,为了突出主要部分,扒了这段代码的衣服是这样的:

@if ($errors->any()) @foreach ($errors->all() as $error) {{ $error }} @endforeach @endif

嗯,瞬间清爽了很多。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对易盾网络的支持。

本文共计882个文字,预计阅读时间需要4分钟。

Laravel自定义验证错误,如何优雅地转换成前端长尾?

原文链接:https://vien.tech/article/163

前 言:Laravel Validator 默认返回的是英文的提示消息,而大部分情况下我们需要自定义错误返回提示信息。本文将介绍如何自定义错误消息。

自定义错误消息:

1. 定义错误消息常量在 application/translations 目录下创建一个语言文件,例如 en.php,并在其中添加自定义错误消息常量:

return [ 'validation'=> [ 'custom'=> [ 'max_length'=> 'The :attribute must not be greater than :max characters.', // 其他自定义错误消息 ], ],];

2. 在验证器中使用自定义错误消息在验证器类中,使用 $message 属性来指定自定义错误消息:

public function rules(){ return [ 'name'=> 'required|max:255', 'email'=> 'required|email|max:255', ];}

public function messages(){ return [ 'name.max'=> trans('validation.custom.max_length', ['attribute'=> 'name', 'max'=> 255]), // 其他自定义错误消息 ];}

3. 在前端使用自定义错误消息在 Vue 或其他前端框架中,使用 Laravel 的验证库进行验证时,错误消息将自动显示为自定义的中文提示:

以上就是在 Laravel 中自定义错误消息的方法,通过这种方式,您可以方便地实现自定义的错误提示信息,提升用户体验。

原文链接:vien.tech/article/163

Laravel Validator 默认返回的是英文的提示消息,而大多数情况我们需要自定义错误返回提示消息,本文将介绍一下如何自定义错误消息,并在前端展示。

自定义错误消息

别怪我太直接,代码奉上

$messages = [ 'phone.unique' => '重复的电话号码', 'required' => '请将信息填写完整', ]; $this->validate($request, [ 'phone' => 'required|unique:table_name', 'name' => 'required', ], $messages);

是不是很简单呀,只需要在validate()方法参数里面加个提示信息数组就好了,数组的key就是字段.验证方式或者直接验证方式,很显然,后者是应用于所有的,前者是应用于某一字段。

当然,你可能不是用的这种方式,其他验证器的也是一样的,看这里:

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

是的,这样写也是传第三个参数,跟上面的方式是一样的。

错误提示前端展示

Laravel自定义验证错误,如何优雅地转换成前端长尾?

接下来讲一下前端如何展示这些错误提示呢,来让我们挨个打印一下

@if ($errors->any()) @foreach ($errors->all() as $error) <div class="center alert alert-danger alert-dismissible fade show" role="alert"> <strong>遇到错误: </strong> {{ $error }} <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> @endforeach @endif

原谅我前端太差,所以用了bootstrap的样式,为了突出主要部分,扒了这段代码的衣服是这样的:

@if ($errors->any()) @foreach ($errors->all() as $error) {{ $error }} @endforeach @endif

嗯,瞬间清爽了很多。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对易盾网络的支持。