gleber1
@gleber1

Как сделать валидацию дерева в laravel/php?

public function store($tags)
    {
        DB::transaction(function () use ($tags) {
            $this->recursive($tags, null);
        });
    }
private function recursive($list, $parentId)
    {
        if ($this->checkToFillable($list)) {
            $this->rules($list);
            $id = DB::table('tags')->insertGetId(
                array(
                    'name' => $list['name'],
                    'description' => $list['description'],
                    'parent_id' => $parentId
                )
            );
            if ($parentId) {
                $query = "INSERT INTO `tags_closure` (`ancestor_id`, `descendant_id`, `depth`)
                    SELECT `ancestor_id`, $id, `depth`+1
                    FROM `tags_closure`
                    WHERE `descendant_id` =" . $parentId;
                DB::statement($query);
            } else {
                DB::table('tags_closure')->insert(
                    [
                        'ancestor_id' => $id,
                        'descendant_id' => $id,
                        'depth' => 0
                    ]
                );
            }
            if (isset($list['closureTag'])) {
                $this->recursive($list['closureTag'], $id);
            }
        } else {
            foreach ($list as $element) {
                $this->recursive($element, $parentId);
            }
        }
    }

И валидация
private function rules($list)
    {
        try {
            Validator::make([
                'name' => $list['name'],
                'description' => $list['description'],
            ], [
                'name' => 'required|scripts',
                'description' => 'required|scripts'
            ]);
        } catch (ValidationException $e) {
            return redirect('tags-closure')->withInput($e->getMessage());
        }

    }

не работает почему то
  • Вопрос задан
  • 243 просмотра
Решения вопроса 1
private function rules($list)
У вас она возвращает редирект...
Тогда как в функции private function recursive($list, $parentId) - она просто вызывается и ничего не возвращается
$this->rules($list);
Я бы на вашем месте сделал бы так:
public function store($tags)
    {
        try {
                DB::transaction(function () use ($tags) {
                    $this->recursive($tags, null);
                });
        } catch (ValidationException $e) {
            return redirect('tags-closure')->withInput($e->getMessage());
        }

    }
private function recursive($list, $parentId)
    {
        if ($this->checkToFillable($list)) {
            $this->validate($list);
            $id = DB::table('tags')->insertGetId(
                array(
                    'name' => $list['name'],
                    'description' => $list['description'],
                    'parent_id' => $parentId
                )
            );
            if ($parentId) {
                $query = "INSERT INTO `tags_closure` (`ancestor_id`, `descendant_id`, `depth`)
                    SELECT `ancestor_id`, $id, `depth`+1
                    FROM `tags_closure`
                    WHERE `descendant_id` =" . $parentId;
                DB::statement($query);
            } else {
                DB::table('tags_closure')->insert(
                    [
                        'ancestor_id' => $id,
                        'descendant_id' => $id,
                        'depth' => 0
                    ]
                );
            }
            if (isset($list['closureTag'])) {
                $this->recursive($list['closureTag'], $id);
            }
        } else {
            foreach ($list as $element) {
                $this->recursive($element, $parentId);
            }
        }
    }

private function validate($list)
    {
            Validator::make([
                'name' => $list['name'],
                'description' => $list['description'],
            ], [
                'name' => 'required|scripts',
                'description' => 'required|scripts'
            ]);

    }
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
29 мар. 2024, в 15:28
10000 руб./за проект
29 мар. 2024, в 15:11
50000 руб./за проект
29 мар. 2024, в 15:06
50000 руб./за проект