BG MVC sisteminde, core/validators dizinindeki dosyalarda tanımlanmış toplam 8 adet doğrulama sınıfı kullanılmaktadır.
BG MVC doğrulama sınıf tanımlamaları, core/validators dizinindeki .php uzantılı dosyalarda yapılmaktadır.
<?php
namespace Core\Validators;
abstract class Validator {
public $success = true, $msg = '', $field, $additional_field_data=[], $rule, $include_deleted = false;
protected $_obj;
public function __construct($obj, $params) {
$this->_obj = $obj;
if(!array_key_exists('field', $params)) {
throw new \Exception("params dizisine bir alan eklemelisiniz!");
}
$this->field = $params['field'];
if(is_array($params['field'])) {
$this->field = $params['field'][0];
array_shift($params['field']);
$this->additional_field_data = $params['field'];
}
if(!property_exists($this->_obj, $this->field)) {
throw new \Exception("Alan değeri model nesnesi üzerinde bir özellik olarak yer almalıdır!");
}
if(!array_key_exists('msg', $params)) {
throw new \Exception("params dizisine bir msg değeri eklemelisiniz!");
}
$this->msg = $params['msg'];
if(array_key_exists('rule', $params)) {
$this->rule = $params['rule'];
}
if(array_key_exists('include_deleted', $params) && $params['include_deleted']) {
$this->include_deleted = true;
}
try {
$this->success = $this->run_validation();
}
catch(\Exception $e) {
echo get_class() . " ile ilgili doğrulama sorunu: " . $e->getMessage() . "<br />";
}
}
abstract public function run_validation();
}
<?php
namespace Core\Validators;
use Core\Validators\Validator;
class EmailValidator extends Validator {
public function run_validation() {
$email = $this->_obj->{$this->field};
$pass = true;
if(!empty($email)) {
$pass = filter_var($email, FILTER_VALIDATE_EMAIL);
}
return $pass;
}
}
<?php
namespace Core\Validators;
use Core\Validators\Validator;
class MatchesValidator extends Validator {
public function run_validation() {
$value = $this->_obj->{$this->field};
return $value == $this->rule;
}
}
<?php
namespace Core\Validators;
use Core\Validators\Validator;
class MaxValidator extends Validator {
public function run_validation(){
$value = $this->_obj->{$this->field};
$pass = strlen($value) <= $this->rule;
return $pass;
}
}
<?php
namespace Core\Validators;
use Core\Validators\Validator;
class MinValidator extends Validator {
public function run_validation(){
$value = $this->_obj->{$this->field};
$pass = strlen($value) >= $this->rule;
return $pass;
}
}
<?php
namespace Core\Validators;
use Core\Validators\Validator;
class RequiredValidator extends Validator {
public function run_validation() {
$value = trim($this->_obj->{$this->field});
$passes = $value != '' && isset($value);
return $passes;
}
}
<?php
namespace Core\Validators;
use Core\Validators\Validator;
class UniqueValidator extends Validator {
public function run_validation(){
$value = $this->_obj->{$this->field};
if($value == '' || !isset($value)) {
return true;
}
$conditions = "{$this->field} = :{$this->field}";
$bind = [$this->field => $value];
if(!$this->_obj->is_new()) {
$conditions .= " AND id != :id";
$bind['id'] = $this->_obj->id;
}
foreach($this->additional_field_data as $adds) {
$conditions .= " AND {$adds} = :{$adds}";
$bind[$adds] = $this->_obj->{$adds};
}
$queryParams = ['conditions' => $conditions, 'bind' => $bind];
$exists = $this->_obj::find_first($queryParams);
return !$exists;
}
}
<?php
namespace Core\Validators;
use Core\Validators\Validator;
class NumericValidator extends Validator {
public function run_validation(){
$value = $this->_obj->{$this->field};
$pass = true;
if(!empty($value)) {
$pass = is_numeric($value);
}
return $pass;
}
}