In a form to add a new row, how do you handle a UNIQUE constraint?

In a form to add a new row, how do you handle a UNIQUE (database) constraint?

Like, I add team member example@example.com. Then I try the form again, it should give feedback that e-mail already exists.

How do I provide feedback to the user? Should I use some Validate step? Or should I rely on the error thrown by the Database Insert when it attempts to insert a duplicate record? What if debug mode is off? Then I don't know which error occurred and can't precisely tell "Duplicate e-mail" to the user, I only know it's a HTTP 500 status code.

Hi Apple,

You can use a validate step to check whether an email already exists in the database. The validate steps provides the feedback to the user when adding a new team member. Here's an example of the validate step to check whether the new email address already exists in the database.

EDIT: The email field in the Expression column is coming from a Set Value step that has a formatter applied to it $_POST.email.lowercase().

3 Likes

To add to @guptast's reply, the Linked Field value ensures that the error message is displayed under the email field.

2 Likes

Thank you both! I had to be a little creative because this is a composite UNIQUE constraint (it needs to fail if there are duplicate email and project_id combination, not just email):

Just realized I need to add one more Validate Data, to validate just if the e-mail is correctly written

2 Likes

You don't HAVE to use validate data step, if you are already running a query to check if email exists.
The only benefit is that it shows a validation text below the linked input field.
But on the downside, its hitting your DB again to run the validate step.

We usually just do a response step with 400 status and some message.
Then show that message either as a notfication or toast or as an on-page alert near the submit button.
Also in some cases, we keep our own hidden error label near the input, when we validate the input as the user types in the data.

Indeed! I was particularly interested in that!

The Validate Data in my last screenshot doesn't hit the DB again, it's just doing a dummy "Equal To" comparison to trigger the form error label

Interesting. Haven't used validate data before.. sounds like a good use case.