This package is no longer actively maintained. Use laravelbook/ardent instead
Self-validating, secure and smart models with caching for Laravel 4's Eloquent ORM using Model Observer and advanced Validation service combined with the Laravel's built-in Validator class.
##Installation
Begin by installing this package through Composer. Edit your project's composer.json file to require ellipsesynergie/orm-validator.
{
"require": {
"ellipsesynergie/orm-validator": "dev-master"
}
}Update your packages with composer update or install with composer install.
##Configurations
To configure the package to meet your needs, you must publish the configuration in your application before you can modify them. Run this artisan command.
php artisan config:publish ellipsesynergie/orm-validatorEllipseSynergie\OrmValidator\Eloquent\BaseModel aims to extend the Eloquent base class without changing its core functionality. Since EllipseSynergie\OrmValidator\Eloquent\BaseModel itself is a descendant of Illuminate\Database\Eloquent\Model, all your EllipseSynergie\OrmValidator\Eloquent\BaseModel models are fully compatible with Eloquent and can harness the full power of Laravels awesome ORM.
To create a new EllipseSynergie\OrmValidator\Eloquent\BaseModel model, simply make your model class derive from the EllipseSynergie\OrmValidator\Eloquent\BaseModel base class.
use EllipseSynergie\OrmValidator\Eloquent\BaseModel;
class Account extends BaseModel {
/**
* The validation service class name
*
* @var string
*/
protected static $validationService = 'Validation\\Account';
/**
* The observer class name
*
* @var string
*/
protected static $observer = 'Observer\\Account';
}Note: You can freely co-mingle your plain-vanilla Eloquent models with
EllipseSynergie\OrmValidator\Eloquent\BaseModeldescendants. If a model object doesn't rely upon user submitted content and therefore doesn't require validation - you may leave the Eloquent model class as it is.
EllipseSynergie\OrmValidator\Eloquent\BaseModel models use Model Observer, advanced Validation Service combined with the Laravel's built-in Validator class.
EllipseSynergie\OrmValidator\Eloquent\BaseModel automaticly register event observer on models boot() method. You MUST create a Observer for each of your EllipseSynergie\OrmValidator\Eloquent\BaseModel models. The observer will also cache data on saved, restored and delete automaticly the cache on delete event.
For example, if you have a Account model, you can create a observer in the file app/models/Observer/Account.php :
<?php namespace Observer;
use EllipseSynergie\OrmValidator\Observer;
class Account extends Observer {}Note: You can keep the Observer has-is if you want. By default, the
EllipseSynergie\OrmValidator\Observerwill validate data oncreatingandupdatingevent.
EllipseSynergie\OrmValidator\Eloquent\BaseModel will automaticly use Validation Service of your model when validate data from inside the model or from the Observer. You MUST create a Validation Service for each of your EllipseSynergie\OrmValidator\Eloquent\BaseModel models.
For example, if you have a Account model, you can create a validation service in the file app/models/Validation/Account.php :
<?php namespace Validation;
use EllipseSynergie\OrmValidator\Services\Validation as ValidationService;
/**
* Account Validation service
*/
class Account extends ValidationService {
/**
* Default array of rules.
*
* @var array
*/
public $rules = array(
'user_id' => array('required', 'integer'),
'company_name' => array('required', 'max:200')
);
}When an EllipseSynergie\OrmValidator\Eloquent\BaseModel model fails to validate, a EllipseSynergie\OrmValidator\Services\ValidateException is throw. You can catch the Exception like this :
try
{
$account = new Account;
$account->user_id = 'foo';
$account->save();
//Success !!!
}
catch (EllipseSynergie\OrmValidator\Services\ValidateException $e)
{
// Retrive the Illuminate\Validation\Validator object
// so you can use it exactly like http://laravel.com/docs/validation
$validator = $e->getValidator();
// Get messages
$messages = $validator->messages();
// Retrieving All Error Messages For All Fields
foreach ($messages->all() as $message)
{
//
}
}

