0
votes

I'm using Laravel 5.3's validation as follows:

In my model:

public static $validation = [
    'name' => 'required',
    'username' => 'required|alpha|unique:companies',
    'email' => 'required|email|unique:companies',
];

In my controller, I post to the same CompanyController@dataPost method when creating a new item or when editing one:

public function dataPost(Request $request) {

    // First validation
    $this->validate($request, Company::$validation);

    $id = $request->id;

    if ($id > 0) {
        // Is an edit!
        $company = Company::find($id);
        $company->update($request->all());
        $company->save();

        Session::flash('messageclass', 'success');
        Session::flash('message', trans('companies.editedsuccessfully'));

    } else {
        // Is a create
        $company = new Company($request->all());
        $company->save();

        Session::flash('messageclass', 'success');
        Session::flash('message', trans('companies.createdsuccessfully'));
    }

    return redirect()->route('companyindex');
}

The unique validation works ok when I create a new item, but causes an error (as in it flags the username as already existing) when editing an item.

Any idea how to avoid this? Even in an edit I'd still want to ensure the data is unique if it's changed, but if the value is the same as before then ignore the validation.

I know I could do this manually, but I wonder if there is a built-in way to avoid this.

Thanks!

4

4 Answers

1
votes

I think you can try this:

public static $validation = [
    'name' => 'required',
    'email'     => Auth::check()
                ? 'required|email|unique:companies,email,'.Auth::id()
                : 'required|email|unique:companies,email',
    'username'  => Auth::check()
                ? 'required|alpha|unique:companies,username,'.Auth::id()
                : 'required|alpha|unique:companies,username',
];

Hope this work for you !!!

1
votes

You can update email field with unique property as well.

Following rule will check uniqueness among all emails in other column except current one.

Try this one,

'email' => 'required|unique:users,email,' . $userId

here $userId refers to id of user currently updated.

You can see official docs here

0
votes

You can create different validation methods for insert or update

public static $validation_update = [
    'name' => 'required',
    'username' => 'required|alpha',
    'email' => 'required|email',
];

public static $validation_add = [
    'name' => 'required',
    'username' => 'required|alpha|unique:companies',
    'email' => 'required|email|unique:companies',
];

Then apply validation in condition

public function dataPost(Request $request) {

    // First validation


    $id = $request->id;

    if ($id > 0) {
        // Is an edit!
      $this->validate($request, Company::$validation_update);
        $company = Company::find($id);
        $company->update($request->all());
        $company->save();

        Session::flash('messageclass', 'success');
        Session::flash('message', trans('companies.editedsuccessfully'));

    } else {
        // Is a create
      $this->validate($request, Company::$validation_add);
        $company = new Company($request->all());
        $company->save();

        Session::flash('messageclass', 'success');
        Session::flash('message', trans('companies.createdsuccessfully'));
    }

    return redirect()->route('companyindex');
}
-2
votes

$id = $request->id;

if ($id > 0) {
    // Is an edit!
  $this->validate($request, Company::$validation_update);
    $company = Company::find($id);
    $company->update($request->all());
    $company->save();