Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/usmanhalalit/dryval
Simplify validation in Laravel.
https://github.com/usmanhalalit/dryval
Last synced: about 2 months ago
JSON representation
Simplify validation in Laravel.
- Host: GitHub
- URL: https://github.com/usmanhalalit/dryval
- Owner: usmanhalalit
- License: mit
- Created: 2014-05-07T18:10:29.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-02-14T08:53:32.000Z (almost 9 years ago)
- Last Synced: 2024-10-04T18:37:42.355Z (3 months ago)
- Language: PHP
- Size: 7.81 KB
- Stars: 39
- Watchers: 5
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# DRYVal
A lightweight library to make your data validation easy, with Laravel PHP Framework.
Make your models self validating, just define the validation rules in your Eloquent model. Whenever you try to create or update data with your model and if validation fails, an exception will be thrown.
## Installation
Add this to require section (in your composer.json):
"usmanhalalit/dryval": "1.*@dev"
And run: `composer update`. Library on [Packagist](https://packagist.org/packages/usmanhalalit/dryval).
## Usage Example
```PHP
use Dryval\ValidationTrait;class Campaign extends Eloquent {
use ValidationTrait;protected static $rules = [
'title' => 'required|min:5|max:50', // `title` is the field in your table
'description' => 'required|min:5|max:500',
];}
```Now every time a validation rule breaks a `Dryval\ValidationException` will be thrown.
### Laravel 5
To catch this put the code below in `render()` method of your `app/Exceptions/Handler.php`.
It will look like this:```PHP
public function render($request, Exception $e)
{
try {
return \Redirect::back()->with('error', $e->getMessages()->first())->withInput(\Input::except('password'));
} catch (Exception $exception) {
// Fallback if referer doesn't exist
return \Redirect::to('/')->with('error', $e->getMessages()->first())->withInput(\Input::except('password'));
}
}
```### Laravel 4
Put the code below in your `app/start/global.php` :```PHP
App::error(function(\Dryval\ValidationException $e)
{
try {
return \Redirect::back()->with('error', $e->getMessages()->first())->withInput(\Input::except('password'));
} catch (Exception $exception) {
// Fallback if referer doesn't exist
return \Redirect::to('/')->with('error', $e->getMessages()->first())->withInput(\Input::except('password'));
}
});
```This will set a flush message on session with key `error`. You're free to do whatever you want. Simple as that.
Now you don't have to validate input everywhere. Just save the data and it will be automatically validated.
DRYVal utilizes PHP 5.4 Traits to make this happen. So you are not forced to extend a class.## More on Usage
### Custom Validation Messages
If you want custom validation messages then you can put a `static $messages` array in your model. Example:
```PHP
protected static $messages = [
'unique' => 'This :attribute is already registered.'
];
```### Base Model
If all of your Eloquent models extend a Base Model then you can `use` the trait in just your Base Model and you're good to go.### Updating with Unique Rule
When you validate using `unique` rule and you want skip certain id then you can just use `:id:` placeholder.Let me clarify, you don't want to allow duplicate email addresses in your users table. You add this rule in your model:
```PHP
protected static $rules = [
'email' => 'required|email|unique:users,email'
];
```Now a user signsup with [email protected] which is not already in database, that's fine. But now what if the user is trying to update his profile with some changes but same email address? Yes, validation will fail.
To solve this problem Laravel accepts the 3rd param for unique rule. DRYVal makes it even easier just use `:id:` placeholder as 3rd param, its like a dynamic id. Example:```PHP
protected static $rules = [
'email' => 'required|email|unique:users,email,:id:'
];
```For this feature, credit goes to [Role Model](https://github.com/betawax/role-model).
### ValidationException
DRYVal throws `Dryval\ValidationException` when validation fails.
You can catch this exception app wise as shown in the first example or you can catch this in your controller/class. The `getMessages()`(plural) method of this exception class will return the validation error messages (MessageBag) same as Laravel's `$validator->messages()`. [More here](http://laravel.com/docs/validation#working-with-error-messages).You can also throw this exception anywhere you need.
___
© 2015 [Muhammad Usman](http://usman.it/). Licensed under MIT license.