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

问题描述 投票:0回答:1

我想知道如何为你的规则(例如正则表达式规则)创建你自己的错误信息,以便被服务器的PHP和客户端的javascript重复使用(使用了 jqueryvalidation 通过 laravel-jsvalidation )

我试过了,但不能使其成功,下面举一个小例子来说明我想做的事情,但没有成功。

我到底做错了什么?

我的小例子:

在文件 "路由/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') !!}
...

在使用URL时 http:/localhost:8000minimal_example_laravel_jsvalidation。浏览器,然后 "查看源码",我可以看到下面的javascript代码已经由上面的"$validator->选择器 "生成。

    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调用.

我使用的是这些版本。

laravelframework v7. 4. 0

proengsoftlaravel-jsvalidation 3.0.0。

php laravel message laravel-validation jsvalidation
1个回答
1
投票

试着像这样改变消息数组。

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

请注意,我也给信息键添加了规则。('three_digits.required')

© www.soinside.com 2019 - 2024. All rights reserved.