laravel-jsvalidation不使用所有规则

时间:2016-02-22 13:59:11

标签: javascript php laravel laravel-5.1 laravel-validation

我正在使用laravel-jsValidation,https://github.com/proengsoft/laravel-jsvalidation我的问题是只使用了我的规则列表中的第一条规则。

在下面的示例中,仅当'title'字段为空时,js才会暂停提交。

我做错了什么?

没有任何错误,后端或javascript。

Laravel v.5.1

/基督教

规则:

    protected $rules = [
        'title' => 'required|max:255',
        'body' => 'required',
        'company_id' => 'required|int',
        'primary_site' => 'required|int',
        'application_type' => 'required|int',
        ];

在控制器方法中:

        $validator = \JsValidator::make($this->rules);

    return view('form', array(
        'person' => $person,
        'validator' => $validator,
        ));

在我的视图中呈现的javascript似乎没问题:

    jQuery(document).ready(function(){

    $("form").validate({
        errorElement: 'span',
        errorClass: 'help-block error-help-block',

        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 immediatly after the input
            } else {
                error.insertAfter(element);
            }
        },
        highlight: function(element) {
            $(element).closest('.form-group').addClass('has-error'); // add the Bootstrap error class to the control group
        },


        /*
         // Uncomment this to mark as validated non required fields
         unhighlight: function(element) {
         $(element).closest('.form-group').removeClass('has-error').addClass('has-success');
         },
         */
        success: function(element) {
            $(element).closest('.form-group').removeClass('has-error').addClass('has-success'); // remove the Boostrap error class from the control group
        },

        focusInvalid: false, // do not focus the last invalid input
                    invalidHandler: function(form, validator) {

            if (!validator.numberOfInvalids())
                return;

            $('html, body').animate({
                scrollTop: $(validator.errorList[0].element).offset().top
            }, 1000);
            $(validator.errorList[0].element).focus();

        },

        rules: {"title":{"laravelValidation":[["Required",[],"F\u00e4ltet title \u00e4r obligatoriskt.",true],["Max",["255"],"title f\u00e5r max inneh\u00e5lla 255 tecken.",false]]},"body":{"laravelValidation":[["Required",[],"F\u00e4ltet body \u00e4r obligatoriskt.",true]]},"company_id":{"laravelValidation":[["Required",[],"F\u00e4ltet company id \u00e4r obligatoriskt.",true],["Integer",[],"company id m\u00e5ste vara en siffra.",false]]},"primary_site":{"laravelValidation":[["Required",[],"F\u00e4ltet primary site \u00e4r obligatoriskt.",true],["Integer",[],"primary site m\u00e5ste vara en siffra.",false]]},"application_type":{"laravelValidation":[["Required",[],"F\u00e4ltet application type \u00e4r obligatoriskt.",true],["Integer",[],"application type m\u00e5ste vara en siffra.",false]]}}        })
})

1 个答案:

答案 0 :(得分:1)

我看到的第一件事就是你的$ rules部分。 ' INT'它在Laravel中写成整数。也许这会导致您无法看到的错误?

关于Laravel中可用和正确验证规则的文档: https://laravel.com/docs/5.1/validation#available-validation-rules