php - 你如何使用 laravel-jsvalidation 显示你自己的错误信息?
问题描述
我想弄清楚如何为您的规则(例如正则表达式规则)创建自己的错误消息,以供服务器 PHP 和客户端 javascript 重用(通过laravel -jsvalidation 使用 jqueryvalidation)
我已经尝试过但无法让它工作,下面是一个小例子来展示我正在尝试做的事情,但它不起作用。
我究竟做错了什么?
我的小例子:
在文件“routes\web.php”中:
Route::get('/minimal_example_laravel_jsvalidation', function() {
// Of course these rules should not really be defined here since
// the purpose of the rules is to also reuse them from PHP Laravel code
// but my problem is now how to generate javascript that can
// reuse the same rules and therefore I just put the rules and messages
// here in this minimalistic example illustrating the problem
$rules = [
'three_digits' => 'required|regex:/^\d{3}$/'
];
$messages = [
'three_digits' => 'Must be exactly three digits'
];
$validator = JsValidator::make($rules, $messages);
return view('minimal_example_laravel_jsvalidation')->with("validator", $validator);
});
在文件“resources\views\minimal_example_laravel_jsvalidation.blade.php”中:
...
{!! $validator->selector('#myForm') !!}
...
当在 Web 浏览器中使用 URL http://localhost:8000/minimal_example_laravel_jsvalidation 然后“查看源代码”时,我可以看到上面的“$validator->selector”生成了以下 javascript 代码:
jQuery(document).ready(function(){
$("#myForm").each(function() {
$(this).validate({
errorElement: 'span',
errorClass: 'invalid-feedback',
errorPlacement: function (error, element) {
if (element.parent('.input-group').length ||
element.prop('type') === 'checkbox' || element.prop('type') === 'radio') {
error.insertAfter(element.parent());
// else just place the validation message immediately after the input
} else {
error.insertAfter(element);
}
},
highlight: function (element) {
$(element).closest('.form-control').removeClass('is-valid').addClass('is-invalid'); // add the Bootstrap error class to the control group
},
unhighlight: function(element) {
$(element).closest('.form-control').removeClass('is-invalid').addClass('is-valid');
},
success: function (element) {
$(element).closest('.form-control').removeClass('is-invalid').addClass('is-valid'); // remove the Boostrap error class from the control group
},
focusInvalid: true,
rules: {"three_digits":{"laravelValidation":[["Required",[],"The three digits field is required.",true],["Regex",["\/^\\d{3}$\/"],"The three digits format is invalid.",false]]}} });
});
});
事实上,当我没有通过我的网络浏览器在字段中写入三位数时,我收到的错误消息是如上“三位数格式无效”。而我希望它应该是我在“$messages”数组中定义的“必须恰好是三位数”。
我已经看到 Laravel 可以使用“自定义验证规则”创建 PHP 类,您还可以在其中定义自定义消息,但据我了解,如果您将这些自定义规则与 laravel-jsvalidation 一起使用,则必须完成验证直接在浏览器中使用 AJAX 而不是 javascript 验证,这是我想要做的,而不是做 AJAX 调用。
我正在使用这些版本:
laravel/框架 v7.4.0
proengsoft/laravel-jsvalidation 3.0.0
解决方案
尝试像这样更改消息数组,
$messages = [
'three_digits.required' => 'Three Digits field is required',
'three_digits.regex' => 'Must be exactly three digits'
];
注意我还向消息键添加了规则。( 'three_digits.required'
)
推荐阅读
- twitter - 使用 Twitter V2 API 如何获取转发消息列表?
- python-3.x - 阅读类方法定义——什么是Callable?
- python-requests - 运行 id 产生 POST_ERRORS
- firebase - Firebase 手机身份验证短信不使用 Playstore 中的应用名称
- flask - Flask,WTForms - 创建字段以接受多个电子邮件并进行电子邮件验证
- github - 在 Github 中提交后如何配置和执行 Azure Devops 经典编辑器管道?
- javascript - 无法记录响应,但我可以在 Postman 和网络选项卡中看到它
- php - 将数据从 json 保存/更新到具有关系的 Laravel 模型的最佳方法
- servicestack - ValidateHasRole 与 RequireRole 属性
- mongodb - 需要有关 Mongo Db Query 的帮助以获取数据