Skip to content

Self-validating, secure and smart models for Laravel 4's Eloquent ORM using Model Observer and advanced Validation service combined with the Laravel's built-in Validator class.

Notifications You must be signed in to change notification settings

elbarto83/orm-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

!!! DEPRECATED !!!

This package is no longer actively maintained. Use laravelbook/ardent instead

ORM validator

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.

Status

Build Status Total Downloads Latest Stable Version

Documentation

##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-validator

Getting Started

EllipseSynergie\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\BaseModel descendants. 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.

Effortless Validation

EllipseSynergie\OrmValidator\Eloquent\BaseModel models use Model Observer, advanced Validation Service combined with the Laravel's built-in Validator class.

Models Observers

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\Observer will validate data on creating and updating event.

Models Validations Services

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')
	);
}

Retrieving Validation Errors

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)
	{
		//
	}
}

About

Self-validating, secure and smart models for Laravel 4's Eloquent ORM using Model Observer and advanced Validation service combined with the Laravel's built-in Validator class.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages