PontoSistemas / LaravelFormAjaxValidationPonto by rodrigoo.sfc@hotmail.com

Make ajax validation with Laravel Requests for forms with bootstrap
27
0
1
Package Data
Maintainer Username: rodrigoo.sfc@hotmail.com
Maintainer Contact: rodrigoo.sfc@hotmail.com (Rodrigo de Souza Fernandes)
Package Create Date: 2017-06-13
Package Last Update: 2017-07-19
Language: PHP
License: MIT
Last Refreshed: 2024-04-18 15:12:01
Package Statistics
Total Downloads: 27
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 0
Total Watchers: 1
Total Forks: 0
Total Open Issues: 0

laravel-form-ajax-validation-ponto

##Installation

1. Composer

Add to the composer of your project

composer require ponto-sistemas/laravel-form-ajax-validation-ponto

Or edit your composer.json

"require": {
    "ponto-sistemas/laravel-form-ajax-validation-ponto": "dev-master"
},

2. Add the ServiceProvider

Open the file config/app.php

"providers": {
    ...
    'PontoSistemas\LaravelFormAjaxValidationPonto\LaravelFormAjaxValidationPontoServiceProvider',
    ...
},

3. Publish vendor resources

You need to publish the necessary views for create the scripts of jQuery

$ php artisan vendor:publish

4. Laravel Request

Create a request

$ php artisan make:Request TestRequest

Add the rules

public function rules()
{
	return [
          'name'=>'required|max:5',
          'description'=>'required',
          'tags'=>'required|min:3',
	];
}

You also can add to the request custom error messages and change de attributes name

public function messages()
{
	return [
          'name.required'=>'Do not forget your name',
          'description.required'=>'You need the description',
          'name.max'=>'Your name have less than 5 letters?',
	];
}

public function attributes(){
        return [
            'name'=>'Your name',
            'tags'=>'The tags',
        ];
    }

5. Add to the view

Create your form

<form method="post" action="<?=url('save_form')?>" id="myform">
    <input type="hidden" name="_token" value="<?=csrf_token()?>">
    <div class="form-group">
        <label for="label_name">Name</label>
		<div class="div-error">
			<input type="text" name="name" id="name" class="form-control">
		</div>
    </div>
    <div class="form-group">
        <label for="label_description">Description</label>
		<div class="div-error">
			<textarea type="text" name="description" id="description" rows="5" class="form-control">
			</textarea>
		</div>
    </div>
    <div class="form-group">
        <label for="label_tags">Tags</label>
		<div class="div-error">
			<input type="text" name="tags" id="tags" class="form-control">
		</div>
    </div>
    <input type="submit" value="Save" class="btn btn-success">
</form>

Add the jQuery and include the view that have the ajax script

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
@include('vendor.lfavp.ajax_script', ['form' => '#myform','request'=>'App/Http/Requests/TestRequest','on_start'=>true])

You need jQuery 1.11.2 or higher