user4383363
user4383363

Reputation:

AdonisJS - Cannot update unique field

AdonisJS is a Laravelish framework for Node.js, but even following Laravel solutions, I didn't work.

I have some rules setting two field as unique, but when updating it, it either fails accusing of having an existing in the database or it allows me to duplicate.

These are the rules:

get rules() {
    return {
      name: 'required',
      username: 'required|alpha_numeric|unique:users,username',
      email: 'required|email|unique:users,email',
      firstName: 'required',
      lastName: 'required',
      password: 'required|min:8',
      confirmPassword: 'required|min:8|same:password'
    }
  }

By following the examples I found, I tried to like this:

username: 'required|alpha_numeric|unique:users,username,id,${this.id}',

But it's still isn't 100%. It doesn't allow me to update the user that already had such username or email, and doing

username: 'required|alpha_numeric|unique:users,id,${this.id}',

It duplicates the data of other users that was suppose to be unique.

Upvotes: 1

Views: 943

Answers (1)

Romain Lanz
Romain Lanz

Reputation: 885

If you want to use template literals your need to use backticks (`) instead of single/double quotes.

username: `required|alpha_numeric|unique:users,username,id,${this.id}`,

Documentation: Template literals

Note: Unique example in the AdonisJs documentation use backticks (AdonisJs Validation - Unique)

Upvotes: 2

Related Questions