iyoworks / Validation by iyoworks

Provides a base class to implement validation as a service. Also adds a little sugar to Laravel's validator
125
1
2
Package Data
Maintainer Username: iyoworks
Maintainer Contact: iyoworks@gmail.com (Blessing Osakue)
Package Create Date: 2013-09-23
Package Last Update: 2015-02-08
Home Page:
Language: PHP
License: Unknown
Last Refreshed: 2024-04-25 03:01:59
Package Statistics
Total Downloads: 125
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 1
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Validation

Provides a base class to implement validation as a service.

BaseValidator

Extend the BaseValidator to create your own validation classes.

use Iyoworks\Validation\BaseValidator;

class FieldValidator extends BaseValidator{

	protected $rules = [
	'name' => 'required',
	'handle' => 'required|unique:fields,handle',
	'fieldtype' => 'required',
	'id' => 'exists:fields,id'
	];
		
	//executed before validation
	protected function preValidate() {
       	if($fieldtype = $this->get('fieldtype'))
		{
			$this->runner->addDynamic('fieldtype', 'exists', function($value){
			  return app('fieldtypes')->exists($value);
			}, 'Invalid fieldtype given for :attribute attribute');
		}
	}

	//executed before validation on insert mode	
	protected function preValidateOnInsert() {}

	//executed before validation on update mode
	protected function preValidateOnUpdate() {
		if($this->get('handle'))
            $this->setUnique('handle'); // the id will be appended to the rule
	}

	//executed before validation on delete mode
	protected function preValidateOnDelete() {}
}

Next instantiate your validator

$validator = new FieldValidator;

$data = ['name' => 'Title', 'order' => 2];

//set the mode and pass your data object
if($validator->validForInsert($data)){
    //your logic
} else {
	$this->errors = $this->validator->errors();
    //more logic
}