GithubHelp home page GithubHelp logo

willyw2k / laravel-form-ajax-validation Goto Github PK

View Code? Open in Web Editor NEW

This project forked from guicho0601/laravel-form-ajax-validation

0.0 2.0 0.0 15 KB

Make ajax validation with Laravel Requests for forms with bootstrap

PHP 100.00%

laravel-form-ajax-validation's Introduction

laravel-form-ajax-validation

Make ajax validation with Laravel Requests for forms with bootstrap

See the full documentation.

O la Documentación en español

##Installation

1. Composer

Add to the composer of your project

composer require lrgt/laravel-form-ajax-validation

Or edit your composer.json

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

2. Add the ServiceProvider

Open the file config/app.php

"providers": {
    ...
    'Lrgt\LaravelFormAjaxValidation\LaravelFormAjaxValidationServiceProvider',
    ...
},

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="nombre">Name</label>
        <input type="text" name="name" id="name" class="form-control">
    </div>
    <div class="form-group">
        <label for="descripcion">Description</label>
        <textarea type="text" name="description" id="description" rows="5" class="form-control">
        </textarea>
    </div>
    <div class="form-group">
        <label for="tags">Tags</label>
        <input type="text" name="tags" id="tags" class="form-control">
    </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.lrgt.ajax_script', ['form' => '#myform',
'request'=>'App/Http/Requests/TestRequest','on_start'=>true])

You need jQuery 1.11.2 or higher

Where #myform is the id of your form and request is the namespace of your Request. Onstart is just if you want that the validation work from the load of the page. Note: Put the namespace of the request separate with /

Preview validation


Author

License

The laravel-form-ajax-validation library is open-sourced software licensed under the MIT license

laravel-form-ajax-validation's People

Contributors

akiu avatar damu112 avatar gmartha07 avatar guicho0601 avatar kamaroly avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.