0

I want to know if there's a way to add a validator where an author needs to be selected on a combobox otherwise it will display an error.

In my application I have three models, Books, Authors and the join table Rel_Book_Author.

public class Book
{
    [Key]
    [Display(Name = "Id")]
    [Column("book_id")]
    public int book_id { get; set; }

    [Display(Name = "Title")]
    [Column("liv_title")]
    [Required(ErrorMessage = "Every book needs a title")]
    public string liv_title { get; set; }
}

public class Author
{
    [Key]
    [Display(Name = "Id")]
    [Column("aut_id")]
    public int aut_id { get; set; }

    [Display(Name = "Author's Name")]
    [Column("aut_name")]
    public string aut_name { get; set; }

    public ICollection<Rel_Book_Author> BookAuthors { get; set; }
}

public class Rel_Book_Author
{
    [Column("hla_aut_id")]
    public int aut_id { get; set; }
    public Author Author { get; set; }

    [Column("hla_book_id")]
    public int book_id { get; set; }
    public Book Book { get; set; }

}
tomRedox
  • 28,092
  • 24
  • 117
  • 154
  • What validation? –  Mar 25 '18 at 22:38
  • Do you mean to validate data model properties? Maybe this answer helps, from [tomRedox](https://stackoverflow.com/questions/43426175/entity-framework-core-doesnt-validate-data-when-saving#answers-header) on a maybe similar question. – eja Mar 25 '18 at 22:53
  • @StephenMuecke edited the question. The validation is to display an error message whenever an user didn't select a author on a combobox – Samuel Monteiro Mar 25 '18 at 23:47
  • On what combobox? (you have not shown you view, or even what the model in the view is). But I assume its for `Rel_Book_Author`, and if you binding to `aut_id`, then add a `[Required]` attribute to that property. –  Mar 25 '18 at 23:48

1 Answers1

0

I'm making the assumption here that your view's model will be the Rel_Book_Author class, or a list of those classes, and so you the view shows a book (or books) and allows the user to pick the author from a list for each book?

If so, then validation should work as per any other model with data annotations.

EF Core does not perform any validations itself, it expects you to do have already validated the object using the client-side validation and on the server (typically in the controller), so the fact that the objects are related by a particular type of relationship (e.g. many-to-many) doesn't matter here.

There is one gotcha to watch out for with the Required attribute for an integer; which is that a non-nullable integer (obviously) cannot be null, it will just default to zero, which means that setting Required won't actually ever return a validation error for an integer in a select list (as the property will always have a value of zero or the value of whatever is selected in the list).

To get round that, declare the aut_id property as nullable (int?):

[Required]
public int? aut_id { get; set; }

or add a Range attribute, e.g.

[Range(1, int.MaxValue)]
public int? aut_id { get; set; }
tomRedox
  • 28,092
  • 24
  • 117
  • 154