exampleapp/modules/Src/TraitValidator.php

51 lines
1.1 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Modules\Src;
use Illuminate\Support\Facades\Validator as FacadeValidator;
trait TraitValidator
{
/**
* Сообщения об ошибках валидации
* @var array
*/
protected array $errors = [];
/**
* Проверенные и очищенные данные
* @var array
*/
protected array $cleanData = [];
/**
* Возвращает ошибки валидации
* @return array
*/
public function getErrors()
{
return $this->errors;
}
/**
* Возвращает проверенные и очищенные данные
* @return array
*/
public function getCleanData(): array
{
return $this->cleanData;
}
protected function validate(array $data, array $rules, array $messages): bool
{
$validator = FacadeValidator::make($data, $rules, $messages);
if ($validator->fails()) {
$this->errors = $validator->errors()->toArray();
return false;
}
$this->cleanData = $validator->validated();
return true;
}
}