phplibrary/src/Validator/Rule/Code.php

61 lines
1.8 KiB
PHP

<?php
/**
* Проверка формата электронной почты
*/
namespace ctiso\Validator\Rule;
use ctiso\Validator\Rule\AbstractRule,
ctiso\Collection;
class Code extends AbstractRule
{
public function getErrorMsg(): string {
return "Неправильно указан персональный код";
}
function checkCode($code): bool {
foreach($code as $c) {
if (empty($c)) {
return false;
}
}
return true;
}
public function isValid(Collection $container, $status = null): bool
{
if ($status == 'update') return true;
$name = $this->field;
if (is_array($_POST[$name . '_code_genre'])) {
$count = count($_POST[$name . '_code_genre']);
for($n = 0; $n < $count; $n++) {
$code = [
$_POST[$name . '_code_genre'][$n],
$_POST[$name . '_code_f'][$n],
$_POST[$name . '_code_i'][$n],
$_POST[$name . '_code_o'][$n],
$_POST[$name . '_code_year'][$n],
$_POST[$name . '_code_month'][$n],
$_POST[$name . '_code_day'][$n]
];
if (!$this->checkCode($code)) {
return false;
}
}
return true;
} else {
$code = [
$_POST[$name . '_code_genre'],
$_POST[$name . '_code_f'],
$_POST[$name . '_code_i'],
$_POST[$name . '_code_o'],
$_POST[$name . '_code_year'],
$_POST[$name . '_code_month'],
$_POST[$name . '_code_day']
];
return $this->checkCode($code);
}
}
}