iyoworks/validation

此包最新版本(v1.0)没有提供许可证信息。

提供一个基础类以实现作为服务的验证。还为Laravel的验证器添加了一些小功能。

v1.0 2015-01-14 21:35 UTC

This package is not auto-updated.

Last update: 2024-09-28 14:12:36 UTC


README

提供一个基础类以实现作为服务的验证。

BaseValidator

通过扩展BaseValidator来创建自己的验证类。

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

接下来实例化你的验证器

$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
}