How to generate Validation Rules based on your database table schema

November 24, 2023
Zacharias Creutznacher
Zacharias Creutznacher
Chief Technology Officer (CTO)

In the fast-paced world of web development, efficiency and automation are key. One repetitive task in this process is defining Validation Rules for data. This is where the"Laravel Schema Rules" package comes into play, especially useful when tasked with defining Validation Rules post-facto for certain endpoints, or for just simplifying the process for defining Validation Rules. The package automatically generates basic Laravel Validation Rules based on your database table schema. This utility is a boon for developers, especially when dealing with complex tables and a multitude of fields, saving time and reducing the potential for error.

Installation

You can install the package via composer:

composer require laracraft-tech/laravel-schema-rules --dev

Then publish the config file with:

php artisan vendor:publish --tag="schema-rules-config"

Usage

Let's say you've migrated this fictional table:

Generate rules

Now if you run:

php artisan schema:generate-rules persons

You'll get:

Schema-based validation rules for table "persons" have been generated!
Copy & paste these to your controller validation or form request or where ever your validation takes place:
[
'first_name' => ['required', 'string', 'min:1', 'max:100'],
'last_name' => ['required', 'string', 'min:1', 'max:100'],
'email' => ['required', 'string', 'min:1', 'max:255'],
'address_id' => ['required', 'exists:addresses,id'],
'bio' => ['nullable', 'string', 'min:1'],
'gender' => ['required', 'string', 'in:m,f,d'],
'birth' => ['required', 'date'],
'graduated' => ['required', 'integer', 'min:1901', 'max:2155'],
'body_size' => ['required', 'numeric'],
'children_count' => ['nullable', 'integer', 'min:0', 'max:255'],
'account_balance' => ['required', 'integer', 'min:-2147483648', 'max:2147483647'],
'net_income' => ['required', 'integer', 'min:0', 'max:4294967295'],
'send_newsletter' => ['nullable', 'boolean']
]

Generate Form Request Class

Optionally, you can add a <code>--create-request<code> or <code>-c<code> flag, which will create a form request class with the generated rules for you!

For the full documentation checkout the README

code

What's a Rich Text element?

The rich text element allows you to create and format headings, paragraphs, blockquotes, images, and video all in one place instead of having to add and format them individually. Just double-click and easily create content.

Static and dynamic content editing

A rich text element can be used with static or dynamic content. For static content, just drop it into any page and begin editing. For dynamic content, add a rich text field to any collection and then connect a rich text element to that field in the settings panel. Voila!

How to customize formatting for each rich text

Headings, paragraphs, blockquotes, figures, images, and figure captions can all be styled after a class is added to the rich text element using the "When inside of" nested selector system.

H4

H5
H6

http://google.de

<code>test123<code>

<span style="color:red;">asdfasdf</span>

composer require spatie/laravel-artisan-dispatchable

Put your ideas into practice!
Today is the day to build the business of your dreams. Share your mission with the world - and inspire your customers.
Start now