Validate the combination of two columns in the record are unique - c#
Validate the combination of two columns in the record are unique - c#
david.j.meyer2@boeing.com
Posts: 54Questions: 15Answers: 0
in Editor
Description of problem: I have a table in our Oracle DB that has two columns requirement and diameter. I would like to validate the combination of these two fields is unique when inserting or editing the table. I want them to be able to edit the fields individually just validate the combination is unique when saving. If its not unique, i want to return a custom message.
using (var db = new Database("oracle", connectionString))
{
var response = new Editor(db, "TABLE", "TABLE.THEKEY")
.Model<PDS_BASE_HPF_DIAMETER>()
.Field(new Field("REQUIREMENT")
.Validator(Validation.NotEmpty())
).Field(new Field("DIAMETER")
.Validator(Validation.NotEmpty())
).Field(new Field("THEKEY"))
.Process(request)
.Data();
return Json(response);
}
Answers
I have modified the code to create a combined field. It displays the combined field correctly in the table but it does not validate the uniqueness. I think I'm close
I have an oracle constraint preventing it and that message displays fine in the dialog. However, I don't want it to exception out at the database and i want a more user friendly message other than the oracle exception number and message.
Our unique validator doesn't currently handle multiple values. You could either create a custom validator that would do this, or since you already have the DB constraint, you could use
postSubmit
to check for the error message from the DB, and if present replace it with something more user friendly. Not as nice a solution, but definitely fast!Allan
I ended up using presubmit to do the check. I set an error for both items with a message saying the combination most be unique. I needed to check this constraint for both create and update. It works fine and exists the search if it finds something that violates uniqueness.
Not sure i like this solution either but it works.
Could you show me an example on how i would create a custom validator for my scenario?
I provided the preSubmit solution code below encase anyone else runs into this problem and wants to validate on the client side.
With a single field you can use a custom validators like this (querying the database to check for duplicates), or if you have two or more fields that interact a global validation function would be the way to do it.
Regards,
Allan
Hi Allan, I have looked at those links a number of times and have not been able to successfully get it to work for my specific use case. I was hoping for an example that targeted my problem. If you could provide one, it would be greatly appreciated.
In the meantime i was having issues with the postSubmit event. When i made the error message modification worked fine. However, when the data was corrected to submit without the unique issue. The dialog would not close and the data was not updating.
I decided to catch the error in C# response object. It works fine but I would really like an example of how to do this with a custom validator for my situation.
Here is the solution i'm using now. Thanks, Dave