Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unique validation error while updating using form request #10

Closed
AjithLalps opened this issue May 5, 2020 · 4 comments
Closed

unique validation error while updating using form request #10

AjithLalps opened this issue May 5, 2020 · 4 comments

Comments

@AjithLalps
Copy link

AjithLalps commented May 5, 2020

I form for saving the category. The name field is unique. I'm using FormRequest for validating the form. Create works fine. but while updating getting error showing The name.en has already been taken.

form fields

                <input class="form-control" name="name[en]" type="text" id="name-en" value="{{ old('name[en]', optional($serviceCategory)->getTranslation('name','en')) }}" minlength="1" maxlength="255" placeholder="{{ __('preferencesCategory.name_en__placeholder') }}">
                <input class="form-control" name="name[ar]" type="text" id="name-ar" value="{{ old('name[ar]', optional($serviceCategory)->getTranslation('name','ar')) }}" minlength="1" maxlength="255" placeholder="{{ __('preferencesCategory.name_ar__placeholder') }}">

FormRequest.php

  public function rules()
    {
        return [
            'code' => 'string|min:1|nullable',
            'name.*' => 'required|string|min:1|max:255|unique_translation:service_categories,name,{$this->serviceCategory->id}',
            'status' => 'string|min:1|nullable',
        ];

web.php (route file)

Route::put('service_category/{serviceCategory}', 'ServiceCategoriesController@update')
            ->name('service_categories.service_category.update')->where('id', '[0-9]+');

controller.php

public function update(ServiceCategory $serviceCategory, ServiceCategoriesFormRequest $request)
    {
        try {
            $data = $request->getData();
            $data['updated_by'] = \Auth::guard('admin')->user()->id;

            $serviceCategory->update($data);

            return redirect()->route('service_categories.service_category.index')
                ->with('success_message', 'Service Category was successfully updated.');
        } catch (Exception $exception) {
            return back()->withInput()
                ->withErrors(['unexpected_error' => 'Unexpected error occurred while trying to process your request.']);
        }
    }
@ivanvermeyen
Copy link
Contributor

Hi,

Before I start bug hunting: any chance it has something to do with using {$this->serviceCategory->id} within single quotes instead of double quotes?

If not, I'll try to reproduce it in a test. 👍

@AjithLalps
Copy link
Author

AjithLalps commented May 6, 2020

like this 'name.*' => 'required|string|min:1|max:255|unique_translation:service_categories,name,' . '{$this->serviceCategory->id}',?

@AjithLalps
Copy link
Author

AjithLalps commented May 6, 2020

It worked after updating the rule,route and function like below.
FormRequest

'name.*' => 'required|string|min:1|max:255|unique_translation:service_categories,name,' . $this->id,

web.php(route file)

Route::put('service_category/{id}', 'ServiceCategoriesController@update')
            ->name('service_categories.service_category.update')->where('id', '[0-9]+');

controller

public function update($id, ServiceCategoriesFormRequest $request)
{
....
}

but why your example not working?

@ivanvermeyen
Copy link
Contributor

Hi,

In PHP you can do this:

$str = "A string containing a {$variable}";

But not this (single quotes around the string):

$str = 'A string containing a {$variable}';

So this should work for you:

'name.*' => "required|string|min:1|max:255|unique_translation:service_categories,name,{$this->serviceCategory->id}",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants