A
A
Abc Edc2015-11-17 20:55:04
Laravel
Abc Edc, 2015-11-17 20:55:04

How to do tree validation in 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);
            }
        }
    }

And validation
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());
        }

    }

doesn't work for some reason

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Bilyk, 2015-11-17
@gleber1

private function rules($list)
It returns a redirect for you...
Whereas in the private function recursive($list, $parentId) function - it is simply called and nothing is returned.
If I were you, I would do this:

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'
            ]);

    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question