Coder Social home page Coder Social logo

laravel-pretty-validation-messages's Introduction

Laravel Pretty Validation Messages

Latest Version on Packagist Total Downloads

Laravel Pretty Validation Messages is a Laravel package providing a trait for FormRequests to use translation strings for validation messages.

Installation

You can install the package via composer:

composer require wessama/laravel-pretty-validation-messages

Or you can just add it to your composer.json file:

{
    "require": {
        "wessama/laravel-pretty-validation-messages": "^1.0"
    }
}

Otherwise, you can clone the repo and include the trait in your FormRequests manually.

Usage

After installation, use the HasPrettyValidationMessages trait in your FormRequest classes:

// app/Http/Requests/YourFormRequest.php

use WessamA\LaravelPrettyValidationMessages\HasPrettyValidationMessages;

class YourFormRequest extends FormRequest
{
    use HasPrettyValidationMessages;

    // Your rules and other FormRequest logic here...
}

Set up your localization strings with keys that match the expected pattern.

The whole point is to not have to write message() methods over and over again for every FormRequest you have in your project. Instead, you can plug this trait into any FormRequest and have it automatically use the translation strings you've already defined.

The trait will look for a translation string with the following pattern:

validation.{fqcn}.{field}.{rule}

Where {fqcn} is the fully qualified class name of the FormRequest, {field} is the name of the FormRequest being validated, and {rule} is the name of the validation rule.

For instance, take the following FormRequest:

class StoreUserDetailsRequest extends FormRequest
{
    // ...
}

Your translation strings would look like this:

// /lang/en/validation.php

return [
        \App\Http\Requests\Form\StoreUserDetails::class => [
            'email' => [
                'required' => 'Email address is required',
                'email'   => 'Email address looks weird, innit?',
                // add more rules here...
            ],
            'password' => [
                'required' => 'Password is required',
                // add more rules here...
            ],
        ],
];

Testing

You can run the tests with:

    composer test

Credits

  • Wessam Ahmed

License

The MIT License (MIT). Please see License File for more information.

laravel-pretty-validation-messages's People

Contributors

wessama avatar

Watchers

 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.