laravel 如何在刀片中使用 jQuery 验证?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42691353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:31:07  来源:igfitidea点击:

How to use jQuery validation in blade?

jquerylaravellaravel-blade

提问by mySun

I create form with blade in Laravel project,

我在 Laravel 项目中用刀片创建表单,

But for example in this form i use name="adult[{{ $i }}][first_name]"in Inputtag.

但例如在这种形式中,我name="adult[{{ $i }}][first_name]"Input标签中使用。

I add .passenger-validationin Form tag:

.passenger-validation在表单标签中添加:

<form method="" route="" class="passenger-validation"></form>

I use jQuery validation in other project:

我在其他项目中使用 jQuery 验证:

$(".passenger-validation").validate({
  rules: {
    // simple rule, converted to {required:true}
    name: "required",
    // compound rule
    email: {
        required: true,
        email: true
    }
  }
});

Now, How to use jQuery validation in form with this name name="adult[{{ $i }}][first_name]"?

现在,如何在具有此名称的表单中使用 jQuery 验证name="adult[{{ $i }}][first_name]"

I use this website: https://jqueryvalidation.org

我使用这个网站:https: //jqueryvalidation.org

Thank you.

谢谢你。

回答by Qammar Feroz

Just include the JQuery validator js file in your blade temp. then use it as you were using before. for example, if js file is in public/js folder, you can add it in blade template like

只需在您的刀片临时文件中包含 JQuery 验证器 js 文件。然后像以前一样使用它。例如,如果 js 文件在 public/js 文件夹中,您可以将其添加到刀片模板中,如

<script src="{{ asset ("/js/validator.js") }}" type="text/javascript"></script>

and then create a js function validateFm

然后创建一个js函数validateFm

<script>
function validateFm(){
  $(".passenger-validation").validate({
  rules: {
  // simple rule, converted to {required:true}
name: "required",
// compound rule
email: {
    required: true,
    email: true
    }
}
  });

}

Finally, call the function validateFm on submit.

最后,在提交时调用函数 validateFm。

<input type="submit" onClick="validateFm();">

Change the name of js file, file location and validator function, as per your need.

根据需要更改 js 文件的名称、文件位置和验证器功能。

回答by V16

First change this:

先改这个:

<form method="" route="" class="passenger-validation">

to:

到:

<form method="" route="" class="passenger-validation" name="passenger-validation">

For JQuery Validation you can stick with your previous method. You just need to call myvalidation.js file to blade template like:

对于 JQuery 验证,您可以坚持使用以前的方法。你只需要调用 myvalidation.js 文件到刀片模板,如:

<script src="{{ asset ("/js/myvalidation.js") }}" type="text/javascript"></script>

And put validation rules there:

并将验证规则放在那里:

 $(".passenger-validation").validate({
        rules: {
           // simple rule, converted to {required:true}
           name: "required",
           // compound rule
           email: {
                  required: true,
                  email: true
                  }
        }
  });

In Laravel Blade you will use HTML 5 Validation rules only like:

在 Laravel Blade 中,您将仅使用 HTML 5 验证规则,例如:

{{ Form:: text('title', null, array('class' => 'form-control', 'required' => '')) }}

you can change "text" with "email" or something that your form need.

您可以使用“电子邮件”或表单需要的内容更改“文本”。

see more here

在这里查看更多

回答by Onix

For validation you will always check the same field, so you should have something static to refer to, for example if you can add an idand then just refer to the id instead of the name would work just fine.

对于验证,您将始终检查相同的字段,因此您应该有一些静态的东西可以引用,例如,如果您可以添加一个id然后只引用 id 而不是名称就可以了。

Change: $(".passenger-validation").validate({

改变: $(".passenger-validation").validate({

To : $("#your_input_id").validate({

到 : $("#your_input_id").validate({