+Error handling
This commit is contained in:
@@ -33,6 +33,12 @@ abstract class AbstractField extends AbstractHtml
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* Сообщения об ошибках
|
||||
* @var string[]
|
||||
*/
|
||||
private $errors = [];
|
||||
|
||||
protected function __construct($form, $type, $name)
|
||||
{
|
||||
self::checkName($name);
|
||||
@@ -128,6 +134,51 @@ abstract class AbstractField extends AbstractHtml
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает массив сообщений об ошибках
|
||||
* @return string[]
|
||||
*/
|
||||
public function errors()
|
||||
{
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверяет, есть ли у поля сообщения об ошибках
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasErrors()
|
||||
{
|
||||
return count($this->errors) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Очищает сообщения об ошибках
|
||||
* @return $this
|
||||
*/
|
||||
public function cleanErrors()
|
||||
{
|
||||
$this->errors = [];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Добавляет новое сообщение об ошибке в поле
|
||||
* @param string $message
|
||||
* @return $this
|
||||
*/
|
||||
public function addError($message)
|
||||
{
|
||||
$cleanedMessage = trim(strval($message));
|
||||
|
||||
if (!in_array($cleanedMessage, $this->errors)) {
|
||||
$this->errors[] = $cleanedMessage;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return array
|
||||
|
25
src/Form.php
25
src/Form.php
@@ -59,6 +59,31 @@ class Form extends AbstractVisibleHtml
|
||||
}
|
||||
}
|
||||
|
||||
public function loadErrors()
|
||||
{
|
||||
$errors = session()->get('errors');
|
||||
|
||||
if (empty($errors)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$messageBag = $errors->getBag('default');
|
||||
|
||||
if (empty($messageBag)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($messageBag->messages() as $key => $messages) {
|
||||
if (!array_key_exists($key, $this->fields)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($messages as $message) {
|
||||
$this->fields[$key]->addError($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Количество полей формы
|
||||
* @return int
|
||||
|
@@ -49,6 +49,7 @@ class FormRenderer
|
||||
|
||||
Blade::directive('form_label', function ($expression) { return '<?php echo \Artmark\Forms\FormRenderer::instance()->renderLabel(' . $expression . '); ?>'; });
|
||||
Blade::directive('form_field', function ($expression) { return '<?php echo \Artmark\Forms\FormRenderer::instance()->renderField(' . $expression . '); ?>'; });
|
||||
Blade::directive('form_error', function ($expression) { return '<?php echo \Artmark\Forms\FormRenderer::instance()->renderError(' . $expression . '); ?>'; });
|
||||
}
|
||||
|
||||
public function beginForm(Form $form)
|
||||
@@ -69,6 +70,25 @@ class FormRenderer
|
||||
]);
|
||||
}
|
||||
|
||||
public function renderError(AbstractField $field, $template = '')
|
||||
{
|
||||
if (!$field->hasErrors()) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (is_string($template) && !empty($template)) {
|
||||
$correctTemplate = $template;
|
||||
}
|
||||
else {
|
||||
$correctTemplate = 'error';
|
||||
}
|
||||
|
||||
return view('accessories.forms.' . $correctTemplate, [
|
||||
'renderer' => $this,
|
||||
'field' => $field,
|
||||
]);
|
||||
}
|
||||
|
||||
public function endForm()
|
||||
{
|
||||
return "</form>\n";
|
||||
|
Reference in New Issue
Block a user