Voyager: Problem with updating a post with "unique" validation

Created on 22 Jun 2017  路  9Comments  路  Source: the-control-group/voyager

  • Laravel Version: 5.4.27
  • Voyager Version: 0.11.14
  • PHP Version: 5.6.28
  • Database Driver & Version: MariaDB 10.1.19

Description:

I added a validation in the field "slug" so that it is unique but I had problems updating the posts, I think it would be ideal to create a way to differentiate validation from the registration and the update.

Maybe something like "rule": "unique: posts, slug, :id" where :id would be a request parameter.

What do you think? Is there any solution for this case?

Steps To Reproduce:

Add the validation "rule": "unique: posts, slug" to the "slug" field in the posts module and try to updating some post...

bug

Most helpful comment

Watch your tone please.
This is open-source and we need the community to help fixing bugs.
If you can fix it, open a PR.

All 9 comments

Have you tried placing the validation in BREAD options?

@akazorg This, I put in the "Optional Details" field following the documentation.

For now I will create a custom controller to solve my problem.

that's what i did #483
````
public function update(Request $request, $id)
{
$slug = $this->getSlug($request);

    $dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();

    // Check permission
    Voyager::canOrFail('edit_'.$dataType->name);

    //Validate fields with ajax
    $val = $this->updateValidateBread($request->all(), $dataType->editRows,$slug,$id);

    if ($val->fails()) {
        return response()->json(['errors' => $val->messages()]);
    }

    if (!$request->ajax()) {
        $data = call_user_func([$dataType->model_name, 'findOrFail'], $id);

        $this->insertUpdateData($request, $slug, $dataType->editRows, $data);

        return redirect()
            ->route("voyager.{$dataType->slug}.edit", ['id' => $id])
            ->with([
                'message'    => "Successfully Updated {$dataType->display_name_singular}",
                'alert-type' => 'success',
            ]);
    }
}

public function updateValidateBread($request, $data, $slug,$id)
{
    $rules = [];
    $messages = [];

    foreach ($data as $row) {
        $options = json_decode($row->details);

        if (isset($options->validation)) {
            if (isset($options->validation->rule)) {
                if (!is_array($options->validation->rule)) {
                    if( strpos(strtoupper($options->validation->rule), 'UNIQUE') !== false )
                    {
                        $options->validation->rule = str_replace('unique:'.$slug, '', $options->validation->rule);

                        $explodedRules = explode('|', $options->validation->rule);

                        $explodedRules[] = \Illuminate\Validation\Rule::unique($slug)->ignore($id);

                        $rules[$row->field] = $explodedRules;


                    }
                    else {
                        $rules[$row->field] = explode('|', $options->validation->rule);
                    }
                } else {
                    $rules[$row->field] = $options->validation->rule;
                }
            }

            if (isset($options->validation->messages)) {
                foreach ($options->validation->messages as $key => $msg) {
                    $messages[$row->field.'.'.$key] = $msg;
                }
            }
        }
    }

    return Validator::make($request, $rules, $messages);
}

````

Labels: bug, enhancement

Close due inactivity.

wtf?! why this issue close but not solved after more then one year? How solve this issue? create every time a new controller is nonsense...

Watch your tone please.
This is open-source and we need the community to help fixing bugs.
If you can fix it, open a PR.

Hello @emptynick, @vaggelis2018
I have unique validation issue when update.
I have used @vaggelis2018 's code but it didn't help me.
I am using voyager 1.1.7.
How can I fix this issue?

@freshcoderchan, this has been closed for nearly a year, and was untouched for months before that. Please don't necro old issues, just open your own

Was this page helpful?
0 / 5 - 0 ratings

Related issues

popica80 picture popica80  路  3Comments

raoasifraza1 picture raoasifraza1  路  3Comments

vaggelis2018 picture vaggelis2018  路  3Comments

wp-src picture wp-src  路  3Comments

iwasherefirst2 picture iwasherefirst2  路  3Comments