Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sandeep-daffodilsw/laravel-model-validator
Easily define and manage validation rules directly within your Eloquent models, streamlining your code by avoiding clutter in controllers and eliminating the need for separate form request classes.
https://github.com/sandeep-daffodilsw/laravel-model-validator
database eloquent eloquent-models laravel laravel-framework laravel-model-validator model model-validation php sandaffo sandaffo-laravel-model-validator sandeep-daffodil sandeep-daffodilsw validation
Last synced: about 1 month ago
JSON representation
Easily define and manage validation rules directly within your Eloquent models, streamlining your code by avoiding clutter in controllers and eliminating the need for separate form request classes.
- Host: GitHub
- URL: https://github.com/sandeep-daffodilsw/laravel-model-validator
- Owner: sandeep-daffodilsw
- Created: 2024-07-23T13:29:03.000Z (4 months ago)
- Default Branch: master
- Last Pushed: 2024-07-24T09:15:30.000Z (4 months ago)
- Last Synced: 2024-09-28T19:21:52.161Z (about 2 months ago)
- Topics: database, eloquent, eloquent-models, laravel, laravel-framework, laravel-model-validator, model, model-validation, php, sandaffo, sandaffo-laravel-model-validator, sandeep-daffodil, sandeep-daffodilsw, validation
- Language: PHP
- Homepage: https://github.com/sandeep-daffodilsw/laravel-model-validator
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Laravel Model Validator
A Laravel package to add validation rules and messages at the model level.
After install this package, you don't need to write the validation rules inside controller or form request, you can add them inside model and directly use methods to validate.
Adding validation rules inside model itself makes model readable for developers.
## Installation
Install the package via composer:
```bash
composer require sandaffo/laravel-model-validator
```## Usage
### Adding Validation to a Model
To add validation to a model, define the `rules` and `messages` properties in your model:
```php
namespace App\Models;use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
// define rules array
public static $rules = [
'title' => 'required|string|max:255',
'slug' => 'required|string|max:255|unique:posts',
'description' => 'required|string|min:8',
];// messages are optional, only provide when want customize message
public static $messages = [
'title.required' => 'The title field is required.',
'slug.required' => 'The slug field is required.',
'description.required' => 'The description field is required.',
];
}
```### Example
Below is an example demonstrating the usage of the package:
```php
$p = new Post();
$p->title = "Test Post One";
$p->slug = "test-post-one";
$p->description = "Test post one description";if ($p->isValid()) { // $p->isValid() returns true or false
echo "Post is valid";
$p->save();
} else {
echo "Post is not valid";
print_r($p->errors());
}
```### Tinker Example
```bash
$ php artisan tinker
``````php
$p = new Post();
$p->title = "Test Post One";
$p->slug = "test-post-one";
$p->description = "Test post one description";$p->isValid(); // true or false
$p->errors(); // []$p->errorMessages(); // [] single dimensional array of all error messages
$p->getRules(); // to get the defined rules if you need somewhere
// [
// "title" => "required|string|max:255",
// "slug" => "required|string|max:255|unique:posts",
// "description" => "required|string|min:8",
// ]$p->getMessages(); // [] of defined custom error messages
$p->getValidator(); // return Validator object if you need
// Illuminate\Validation\Validator { ... }$p->getValidator()->errors();
// Illuminate\Support\MessageBag { ... }$p->save(); // true
$p;
// App\Models\Post {
// title: "Test Post One",
// slug: "test-post-one",
// description: "Test post one description",
// updated_at: "2024-07-24 08:45:45",
// created_at: "2024-07-24 08:45:45",
// id: 1,
// }
```## Methods
- `isValid()`: Checks if the model is valid according to the defined rules.
- `errors()`: Returns an array of validation errors.
- `errorMessages()`: Returns an array of error messages.
- `getRules()`: Returns the validation rules.
- `getMessages()`: Returns the validation messages.
- `getValidator()`: Returns the validator instance.## License
This package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).