Laravel 5:使用 Javascript 验证表单的最佳方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28762376/
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-08 16:54:16  来源:igfitidea点击:

Laravel 5: Best way to validate form with Javascript

laravellaravel-5

提问by Victor

I want to validate forms in the client side in Laravel. It's there any easy way to do it?

我想在 Laravel 的客户端验证表单。有什么简单的方法可以做到吗?

回答by torrentalle

You can use the package Laravel 5 Javascript Validation. This package enables transparent Javascript Valditaion in your views based on JQuery Validation Plugin

您可以使用Laravel 5 Javascript Validation包。此包基于JQuery 验证插件在您的视图中启用透明的 Javascript Valditaion

This is a basic example of how to reuse your validation rules in the controller.

这是如何在控制器中重用验证规则的基本示例。

PostController.php

后控制器.php

namespace App\Http\Controllers;

class PostController extends Controller {

    /**
     * Define your validation rules in a property in 
     * the controller to reuse the rules.
     */
    protected $validationRules=[
                'title' => 'required|unique|max:255',
                'body' => 'required'
    ];

    /**
     * Show the edit form for blog post
     * We create a JsValidator instance based on shared validation rules
     * @param  string  $post_id
     * @return Response
     */
    public function edit($post_id)
    {
        $validator = JsValidator::make($this->validationRules);
        $post = Post::find($post_id);

        return view('edit_post')->with([
            'validator' => $validator,
            'post' => $post
        ]);
    }

    /**
     * Store the incoming blog post.
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        $v = Validator::make($request->all(), $this->validationRules]);

        if ($v->fails())
        {
            return redirect()->back()->withErrors($v->errors());
        }

        // do store stuff
    }
}

In the view you simply should print the validatorobject passed to the view. Rememberthat this package depends of JQuery and you have to include before that jsvalidation.js

在视图中,您只需打印传递给视图的验证器对象。请记住,这个包依赖于 JQuery,你必须在jsvalidation.js之前包含

edit_post.blade.php

edit_post.blade.php

 <div class="container">
     <div class="row">
         <div class="col-md-10 col-md-offset-1">
             <form class="form-horizontal" role="form" method="POST" action="" id="ddd">
                 <div class="form-group">
                     <label class="col-md-4 control-label">Title</label>
                     <div class="col-md-6">
                         <input type="text" class="form-control" name="title">
                     </div>
                 </div>
                 <div class="form-group">
                     <label class="col-md-4 control-label">Array</label>
                     <div class="col-md-6">
                         <textarea name="body"></textarea>
                     </div>
                 </div>
             </form>
         </div>
     </div>
 </div>
 <!-- Scripts -->
 <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
 <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>

 <!-- Laravel Javascript Validation -->
 <script type="text/javascript" src="{{ asset('vendor/jsvalidation/js/jsvalidation.js')}}"></script>
 {!! $validator !!}

回答by Jad Joubran

As discussed in the comments, since Laravel is a backend framework, it does not provide client side validationout of the box.
It shouldn't.

正如评论中所讨论的,由于 Laravel 是一个后端框架,因此它不提供client side validation开箱即用的功能。
它不应该。

You might choose a Javascript framework such as Angularjs, emberjs, etc. that will have validation baked in.
Or if you're using jQuery, you can just use any jquery validation plugin, here are a few:

您可以选择一个 Javascript 框架,例如 Angularjs、emberjs 等,它们将进行验证。
或者,如果您使用的是 jQuery,则可以使用任何 jquery 验证插件,这里有一些:

if you're using bootstrap, then you can use http://1000hz.github.io/bootstrap-validator/

如果您使用引导程序,那么您可以使用http://1000hz.github.io/bootstrap-validator/

回答by JGR

Laravel 5 Javascript Validation not working with Laravel 5.3 for now. You will get a "Method [getFiles] does not exist." error, you can sort it out by follow this issue https://github.com/proengsoft/laravel-jsvalidation/issues/190

Laravel 5 Javascript 验证目前不适用于 Laravel 5.3。您将收到“方法 [getFiles] 不存在”。错误,您可以按照此问题进行整理https://github.com/proengsoft/laravel-jsvalidation/issues/190

Also it doesn't work for array validation as it is new in Laravel 5.3

它也不适用于数组验证,因为它是 Laravel 5.3 中的新功能

回答by JGR

When I'm working with Laravel I use Vue.jsand can't be more happy about it for client side validation.
You can also use something else, just don't use jQuery (anymore), I strongly discourage it.

当我使用Laravel 时,我使用Vue.js并且对它进行客户端验证感到非常高兴。
您也可以使用其他东西,只是不要使用 jQuery(不再),我强烈建议不要使用它。