Frontend form validation library with validatorjs API for Laravel
onemoreblock/laravel-validatorjs is a Laravel package for frontend form validation library with validatorjs api for laravel.
It currently has 0 GitHub stars and 0 downloads on Packagist.
Install it with composer require onemoreblock/laravel-validatorjs.
Discover more Laravel packages by onemoreblock
or browse all Laravel packages to compare alternatives.
Last updated
This library generates the frontend validation rules for a form and displaying the errors for respected input/options all of this is done by just defining your Laravel Form Requests, and it also supports laravel's custom attributes names and error message customization
$ composer require onemoreblock/laravel-validatorjs
$ php artisan vendor:publish --provider="OneMoreBlock\Validatorjs\ValidatorJsServiceProvider"
Currently this package only supports validator js rules generation by using only laravel form requests. So start by creating a laravel Form Request eg: PostRequest
$ php artisan make:request PostRequest
Now open the generated PostRequest file and add make sure to do trhe following
<?php
namespace App\Http\Requests;
use OneMoreBlock\Validatorjs\Validatorjs;
class PostRequest extends Validatorjs
{
protected $tableID = 'entries';
protected $successCallback = 'successCallback';
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'f_name' => 'required|min:20|max:30',
'l_name' => 'required|min:20|max:30',
'email' => 'required|email',
];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes()
{
return [
'f_name' => 'First name',
'l_name' => 'Last Name',
];
}
/**
* Get the error messages for the defined validation rules.
*
* @return array
*/
public function messages()
{
return [
'f_name.required' => 'A :attribute is required',
'l_name.required' => 'A :attribute is required',
];
}
}
<?php
namespace App\Http\Controllers;
use App\Http\Requests\PostRequest;
class PostController extends Controller
{
public function index()
{
return (new PostRequest)->render('validatorjs.index'); // blade file
}
}
view file example
<form method="post" id="{{ $table_id }}">
@csrf
<div class="col-md-4">
<label for="validationCustom01" class="form-label">First name</label>
<input type="text" class="form-control" id="validationCustom01" name="f_name" value="Mark">
</div>
<div class="col-md-4">
<label for="validationCustom02" class="form-label">Last name</label>
<input type="text" class="form-control" id="validationCustom02" name="l_name" value="Otto" >
</div>
<div class="col-md-4">
<label for="validationCustom02" class="form-label">Email</label>
<input type="email" class="form-control" id="validationCustom02" name="email" >
</div>
<input type="submit" value="Sumit" class="btn btn-sm btn-primary">
</form>
{!! $scripts !!}
<script type="text/javascript">
function successCallback(data) { // same name as $successCallback defined in the PostRequest
// Stuff to do after the validation is passed
}
</script>
The Laravel-Validatorjs is open-sourced software licensed under the MIT license.