Laravel Javascript Validation allows to reuse your Laravel Validation Rules, Messages, FormRequest and Validators to validate forms
transparently in client side using Javascript. You can validate forms automatically referencing it to your defined validations. The messages are loaded from your validators and translated according your Localization preferences.
This package ships with a simple, convenient facility for configuring rules error messages via the JsValidator
facade.
The JsValidator
created by the Facade inherits from Laravel Validation, so you can use all methods and procedures that Laravel provides to
setup your validations. Also class and rule syntax are the same that Laravel Validation class implements. When the instance is printed in a
view the Javascript code needed to validate your form is rendered to the page.
The Javascript validations are made using JQuery Validation Plugin, that is compiled into javascript in the package.
The MIT License (MIT). Please see License File for more information.
This is a basic example of how to reuse your validation rules in the controller .
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
}
}
In the view you simply should print the validator object passed to the view. Remember that this package depends of JQuery and you have to include before that 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 !!}
You can more examples in the github project wiki
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community