简体   繁体   English

你如何使用 laravel-jsvalidation 显示你自己的错误信息?

[英]How do you display your own error messages with laravel-jsvalidation?

I would like to figure out how to create your own error messages for your rules (eg regular expression rules) to be reused by both server PHP and client javascript (using jqueryvalidation through laravel-jsvalidation )我想弄清楚如何为您的规则(例如正则表达式规则)创建自己的错误消息,以供服务器 PHP 和客户端 javascript 重用(通过laravel -jsvalidation 使用 jqueryvalidation

I have tried and can not make it work, and below is a small example to show what I am trying to do but it does not work.我已经尝试过但无法让它工作,下面是一个小例子来展示我正在尝试做的事情,但它不起作用。

What am I doing wrong?我究竟做错了什么?

My small example:我的小例子:

In the file "routes\web.php":在文件“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);
    });    

In the file "resources\views\minimal_example_laravel_jsvalidation.blade.php":在文件“resources\views\minimal_example_laravel_jsvalidation.blade.php”中:

...
{!! $validator->selector('#myForm') !!}
...

When using the URL http://localhost:8000/minimal_example_laravel_jsvalidation with the web browser and then "view source" I can see that the following javascript code has been generated by the above "$validator->selector": When using the URL http://localhost:8000/minimal_example_laravel_jsvalidation with the web browser and then "view source" I can see that the following javascript code has been generated by the above "$validator->selector":

    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]]}}            });
        });
    });

Indeed, the error message I get when not writing three digits in the field through my web browser, is as above "The three digits format is invalid."事实上,当我没有通过我的 web 浏览器在字段中写入三位数时收到的错误消息如上“三位数格式无效”。 while I expect that it instead should be "Must be exactly three digits" as I defined in the "$messages" array.而我希望它应该是我在“$messages”数组中定义的“必须恰好是三位数”。

I have seen that it is possible with Laravel to create PHP classes with "Custom Validation Rules" where you also can define custom messages, but as far as I understand, if you use those custom rules with laravel-jsvalidation then the validation must be done with AJAX instead of javascript validation directly within the browser, which is what I want to do instead of doing AJAX calls.我已经看到 Laravel 可以使用“自定义验证规则”创建 PHP 类,您也可以在其中定义自定义消息,但据我了解,如果您将这些自定义规则与 laravel-jsvalidation 一起使用,则必须完成验证直接在浏览器中使用 AJAX 而不是 javascript 验证,这是我想要做的,而不是做 AJAX 调用。

I am using these versions:我正在使用这些版本:

laravel/framework v7.4.0 laravel/框架 v7.4.0

proengsoft/laravel-jsvalidation 3.0.0 proengsoft/laravel-jsvalidation 3.0.0

Try changing messages array like this,尝试像这样更改消息数组,

$messages = [
     'three_digits.required' => 'Three Digits field is required',
     'three_digits.regex' => 'Must be exactly three digits'
];

Note I have added rule also to the message key.注意我还向消息键添加了规则。 ( 'three_digits.required' ) ( 'three_digits.required' )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM