public bool isValid(Part entity) { var validator = new PartValidator(); var result = validator.Validate(entity); return(result.IsValid); }
public IList <ErrorMessage> Errors(Part entity) { var validator = new PartValidator(); var result = validator.Validate(entity); var errors = new List <ErrorMessage>(); foreach (var item in result.Errors) { errors.Add(new ErrorMessage { Key = item.PropertyName, Error = item.ErrorMessage }); } return(errors); }
private void insertFunc(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(pname.Text) || string.IsNullOrEmpty(pcountry.Text) || string.IsNullOrEmpty(pphone.Text) || string.IsNullOrEmpty(pmail.Text) || string.IsNullOrEmpty(pcity.Text) || string.IsNullOrEmpty(numofemp.Text) || string.IsNullOrEmpty(pdistr1.Text) || string.IsNullOrEmpty(phoroo.Text) || string.IsNullOrEmpty(pwebpage.Text) || pstate.SelectedItem == null || ptype.SelectedItem == null || spetype.SelectedItem == null || string.IsNullOrEmpty(pstreet.Text) ) { MessageBox.Show("Талбарууд дутуу байна"); return; } try { using (var context = new demoEntities10()) { var std = new Participant(); std.name = pname.Text; std.country = pcountry.Text; std.phone = pphone.Text; std.email = pmail.Text; std.state = Convert.ToInt16(pstate.SelectedIndex - 1); std.pcity = pcity.Text; std.pdistr = pdistr1.Text; std.phoroo = phoroo.Text; std.pstreet = pstreet.Text; std.pwebpage = pwebpage.Text; std.numofemp = numofemp.Text; std.specialType = Convert.ToInt16(spetype.SelectedIndex + 1); std.companyType = Convert.ToInt16(ptype.SelectedIndex + 1); std.modified = DateTime.Now; PartValidator validator = new PartValidator(); FluentValidation.Results.ValidationResult result = validator.Validate(std); if (result.IsValid == false) { foreach (ValidationFailure failure in result.Errors) { //errors.Add(failure.ErrorMessage); ((MainWindow)Window.GetWindow(this)).btmstat.Foreground = Brushes.Red; ((MainWindow)Window.GetWindow(this)).btmstat.Text = failure.ErrorMessage; return; } } context.Participants.Add(std); try { context.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); // raise a new exception nesting // the current instance as InnerException raise = new InvalidOperationException(message, raise); } } throw raise; } } } catch (System.Data.Entity.Infrastructure.DbUpdateException ex) { MessageBox.Show("Бүртгэгдсэн байна та өөр "); return; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return; } FillDataGrid(); newData(null, null); }