Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jeffreyway/laravel-model-validation
This Laravel package auto-validates a model on save.
https://github.com/jeffreyway/laravel-model-validation
Last synced: 5 days ago
JSON representation
This Laravel package auto-validates a model on save.
- Host: GitHub
- URL: https://github.com/jeffreyway/laravel-model-validation
- Owner: JeffreyWay
- Created: 2013-04-12T18:43:45.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2016-09-21T11:08:35.000Z (over 8 years ago)
- Last Synced: 2024-07-31T22:35:17.035Z (5 months ago)
- Language: PHP
- Homepage: https://packagist.org/packages/way/database
- Size: 299 KB
- Stars: 275
- Watchers: 14
- Forks: 54
- Open Issues: 21
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Auto-Validate On Save
This package takes care of the model validation process for you. Rather than manually going through the tedious `Validator::make(...)` process, just save the model, and this package will handle the rest.## Usage
Install the package through Composer.```js
{
"require": {
"laravel/framework": "4.0.*",
"way/database": "dev-master"
}
}
```Then, rather than extending `Eloquent` from your model, extend `Way\Database\Model`, like so:
```php
'Way\Database\Model'
```
Now, your models can simply extend `Model`.```php
'required'
];//Use this for custom messages
protected static $messages = [
'name.required' => 'My custom message for :attribute required'
];
}
```Now, simply save the model as you normally would, and let the package worry about the validation. If it fails, then the model's `save` method will return false.
Here's an example of storing a new dog.
```php
public function store()
{
$dog = new Dog(Input::all());if ($dog->save())
{
return Redirect::route('dogs.index');
}return Redirect::back()->withInput()->withErrors($dog->getErrors());
}
```If using Eloquent's static `create` method, you can use the `hasErrors()` methods to determine if validation errors exist.
```php
$dog = Dog::create(Input::all());if ($dog->hasErrors()) ...
```
That's it! Have fun.