Giter VIP home page Giter VIP logo

autogenerated-messages's Introduction

Auto-generated default messages for Laravel Validation

This project will help to generate automatically error messages based on your validation rules in the Laravel project.

How it works?

You install the package:

composer require setnemo/autogenerated-messages

Then, in your Request class (which inherits from FormRequest), add the trait:

<?php
declare(strict_types=1);
namespace Awesome\Requests;
use Illuminate\Foundation\Http\FormRequest;
class NewRequest extends FormRequest
{
    use DefaultMessages;  /** Add use trait */
    public function rules(): array
    {
        return [
            'name' => 'required|string|max:120',
            'start_date' => 'required|integer',
            'price' => 'nullable|numeric|min:0 ',
            'pay_link' => 'nullable|string|url|max:256',
            'video' => 'nullable|mimes:mp4,mov,avi',
            'confidentiality' => 'required|string|in:public,personal',
        ];
    }
}

It's all! Error messages will be generated based on your rules.

Example:

<?php

var_export($newRequest->message());

Result:

[
  'name.required' => 'name is required',
  'name.string' => 'Value for name must be string',
  'name.max' => 'Maximal value for name is 120',
  'start_date.required' => 'start_date is required',
  'start_date.integer' => 'Value for start_date must be integer',
  'price.numeric' => 'Key price must be numeric',
  'price.min' => 'Minimal value for price is 0',
  'pay_link.string' => 'Value for pay_link must be string',
  'pay_link.url' => 'Key pay_link must be valid url',
  'pay_link.max' => 'Maximal value for pay_link is 256',
  'video.mimes' => 'Allowed formats for video: mp4, mov, avi',
  'confidentiality.required' => 'confidentiality is required',
  'confidentiality.string' => 'Value for confidentiality must be string',
  'confidentiality.in' => 'Allowed values for confidentiality: public, personal',
];

Supported types

Right now project supported next validation types:

[
    'required',
    'integer',
    'numeric',
    'string',
    'url',
    'in',
    'min',
    'max',
    'mimes',
];

Also, you can add another Laravel Validation types, just create fork this project and create pull request.

Or add in your project:

<?php
$this->addRuleNames(['test_rule']);
$this->addRulesToMessages(['test_rule' => 'test_message']);

Contributing Guide

Please check Contributing Guide

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.