Commit 2f0ce1e7 authored by Ильнур Табулдин's avatar Ильнур Табулдин
Browse files

Скорректирован принцип сохранения очередности. Добавлен тип ответа в getErrors()

parent ad36e832
Showing with 30 additions and 17 deletions
+30 -17
src/Form.php 100644 → 100755
......@@ -2,19 +2,15 @@
namespace deck\form;
use app\vendor\deck\form\src\ResponseTypeEnum;
use ReflectionProperty;
use yii\base\Model;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
abstract class Form extends Model
{
/**
* Для контейнера форм, необходимо ли оставить порядок для форм, которые пришли
* @var bool $keepOrder
*/
protected $keepOrder = false;
public function load($data, $formName = null): bool
public function load($data, $formName = null, bool $keepOrder = false): bool
{
$formData = $data;
foreach ($this->getInternalForms() as $form) {
......@@ -58,7 +54,7 @@ abstract class Form extends Model
if (count(array_filter(array_keys($data[$name]), 'is_string')) === 0) {
$this->$name = [];
foreach ($data[$name] as $key => $ignored) {
if ($this->keepOrder) {
if ($keepOrder) {
$this->$name[$key] = new $form->className();
} else {
$this->$name[] = new $form->className();
......@@ -141,7 +137,7 @@ abstract class Form extends Model
return false;
}
public function getErrors($attribute = null): array
public function getErrors($attribute = null, string $responseType = ResponseTypeEnum::Rest): array
{
$result = parent::getErrors($attribute);
foreach ($this->getInternalForms() as $form) {
......@@ -159,7 +155,19 @@ abstract class Form extends Model
}
if ($attribute === null) {
foreach ($errors as $error) {
$result[$errorAttr][] = $error;
switch ($responseType) {
case ResponseTypeEnum::Rest:
{
$result[$errorAttr][] = $error;
break;
}
case ResponseTypeEnum::ActiveForm:
{
$result[Html::getInputId($item, "[$i]" . $attr)][] = $error;
break;
}
}
}
} elseif ($errorAttr === $attribute) {
foreach ($errors as $error) {
......
......@@ -4,18 +4,14 @@ namespace deck\form;
abstract class FormContainer extends Form
{
/** @var string $internalForms */
protected $internalForms;
public $forms = [];
public function __construct(bool $keepOrder = false, array $config = [])
{
$this->keepOrder = $keepOrder;
}
public function load($data, $formName = null): bool
public function load($data, $formName = null, bool $keepOrder = false): bool
{
$data = ['forms' => $data];
return parent::load($data, 'forms');
return parent::load($data, 'forms', $keepOrder);
}
protected function internalForms(): array
......
<?php
namespace app\vendor\deck\form\src;
class ResponseTypeEnum
{
const Rest = 'rest';
const ActiveForm = 'activeForm';
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment