-1

it is necessary for me to validate whether a value is already in the database or not. Therefore I wrote an own ValidationAttribute that checks it. The problem is I add this to the attribute in the model:

[UniqueAttribute(ErrorMessage = "This title already exists")]

That works fine in the add view but when editing this object I don't know how to handle this, because, if the user doesn't change the title, there will also be an error message because the validation is executed. I could use a modelview without this ValidationAttribute but if the user changes the title to another title that still exists, it would breach the uniqueness.

Do you have any idea how I can validate for the edit view if the title would be unique after updating the object or not?

Thank you.

example:

public class UniqueAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        if (value == null)
        {
            return true;
        }

        MyDataContext db = new MyDataContext();
        foreach (var item in db.AnyObjects.ToList())
            if (item.Title.Equals(value))
                return false;
        return true;
    }
}

This works good when adding an object but not when editing.

Underfaker
  • 97
  • 2
  • 12
  • 1
    What is the code for your `UniqueAttribute` attribute? –  Jun 12 '15 at 12:58
  • Call database, checking if value is in the table and return true if yes and false if not. – Underfaker Jun 12 '15 at 13:07
  • My Code is correct, I don't need to correct it. I am looking for the right idea then I may implement it for myself. – Underfaker Jun 12 '15 at 13:14
  • The only and straightforward solution is to know id of the item in edit view and you should pass the id to UniqueAttribute so that it can check the title is being used or not – brtb Jun 12 '15 at 13:20
  • if you can share UniqueAttribute class codes we someone can help more easily – brtb Jun 12 '15 at 13:21
  • How can I pass the id to the ValidationAttribute? – Underfaker Jun 12 '15 at 13:28

1 Answers1

0

In your model create a new property that stores the previous (unchanged or existing) title. So in case of an Add this will be string.empty, in case of edit it will be the unmodified title value. Use reflection inside your validation to compare this property with the title property. If they match then (and are not both string.empty), then that means user edited the record but did not change the title, and your validation simply passes. If they don't match that means user changed the title, and you can check the db if the new title entered by the user already exists.

Harsh Shah
  • 667
  • 5
  • 22
  • How can I get the title property in the Validation Class, because I use the form like above and the IsValid method only gets one parameter. – Underfaker Jun 12 '15 at 13:35
  • using validationcontext you can use reflection to get the other property. check this post: http://stackoverflow.com/questions/6075339/mvc-form-validation-on-multiple-fields – Harsh Shah Jun 12 '15 at 13:44
  • I'm sorry I don't understand the article. I don't know what to do to fix my problem – Underfaker Jun 12 '15 at 14:39