I've created two unique indexes on fields in the same table, and want to validate them using unique_constraint/3, however, the error message on the front-end only displays for either field, not both if they are not unique. How can I make both errors show up if neither field is unique?
Asked
Active
Viewed 882 times
0
-
2Show us the code, please. – Aleksei Matiushkin Nov 23 '17 at 07:38
-
You might be looking for this: https://stackoverflow.com/a/37859294/3142192 – Shashidhar Mayannavar Nov 23 '17 at 09:16
1 Answers
1
You can use Changeset.unsafe_validate_unique/4 to find all the unique validation errors and report them to the user. unique_constraint
must also be used to handle potential race condition of the data changing before the new record is inserted.
changeset
|> unsafe_validate_unique([:email], MyApp.Repo, message: "email is already in use")
|> unsafe_validate_unique([:phone], MyApp.Repo, message: "phone number is already registered")
|> unique_constraint(:email)
|> unique_constraint(:phone)

Mike Buhot
- 4,790
- 20
- 31