安装bllim / laravel-to-jquery-validation

时间:2014-11-26 10:42:11

标签: php laravel laravel-4 composer-php

在使用composer安装此软件包时遇到问题......

bllim / laravel到jquery的验证

在composer.json中我有

"require": {
    "php": ">=5.3.7",
    "laravel/framework": "4.1.*",

Composer update返回..

enter image description here

1 个答案:

答案 0 :(得分:0)

bllim / laravel-to-jquery-validation的替代方法是包Laravel 5 Javascript Validation。此软件包可根据JQuery Validation Plugin在您的视图中启用透明的Javascript Valditaion,而无需编写任何JavaScript代码

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

<强> PostController.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
    }
}

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

之前加入

<强> edit_post.balde.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 !!}