public static ModelErrorCollection Validar(Usuario usuario, Boolean validarSenha = true) { var mensagens = new ModelErrorCollection(); if (String.IsNullOrWhiteSpace(usuario.Email)) { mensagens.Add("É obrigatório fornecer o e-mail do usuário"); } if (validarSenha && String.IsNullOrWhiteSpace(usuario.Senha)) { mensagens.Add("É obrigatório fornecer a senha do usuário"); } if (validarSenha && !String.IsNullOrWhiteSpace(usuario.Senha) && usuario.Senha.Length < 6) { mensagens.Add("A senha deve ter no mínimo 6 caracteres"); } if (String.IsNullOrWhiteSpace(usuario.Nome)) { mensagens.Add("É obrigatório fornecer o nome do usuário"); } if (usuario.Perfis == null || usuario.Perfis.Count() == 0) { mensagens.Add("É obrigatório relacionar ao menos um perfil ao usuário"); } return(mensagens); }
public void OnActionExecuting_Should_Update_Result_When_Model_Is_Invalid() { var actionContext = new ActionContext { HttpContext = new DefaultHttpContext(), RouteData = new RouteData(), ActionDescriptor = new ActionDescriptor() }; var context = new ActionExecutingContext(actionContext, Substitute.For <IList <IFilterMetadata> >(), Substitute.For <IDictionary <string, object> >(), null); var field = "field"; var message = "validation message"; actionContext.ModelState.AddModelError(field, message); var errors = new ModelErrorCollection(); errors.Add(message); var data = new [] { new { Key = field, Errors = errors } }; _filter.OnActionExecuting(context); context.Result.Should().BeAssignableTo <ObjectResult>(); context.Result.As <ObjectResult>().StatusCode.Should().Be(StatusCodes.Status400BadRequest); context.Result.As <ObjectResult>().Value.Should().BeAssignableTo <ApiResponse>(); context.Result.As <ObjectResult>().Value.As <ApiResponse>().Success.Should().BeFalse(); context.Result.As <ObjectResult>().Value.As <ApiResponse>().Message.Should().Be("Invalid request format."); context.Result.As <ObjectResult>().Value.As <ApiResponse>().Data.Should().BeEquivalentTo(data); }
public static ModelErrorCollection Validar(Usuario usuario) { var mensagens = new ModelErrorCollection(); if (String.IsNullOrWhiteSpace(usuario.Email)) { mensagens.Add("E-mail não informado"); } if (String.IsNullOrWhiteSpace(usuario.Senha)) { mensagens.Add("Senha não informada"); } return(mensagens); }
public static ModelErrorCollection AsModelErrorCollection(this string message) { var collection = new ModelErrorCollection(); collection.Add(message); return(collection); }
public static ModelErrorCollection AsModelErrorCollection(this Exception ex) { var collection = new ModelErrorCollection(); collection.Add(ex); return(collection); }
protected ModelErrorCollection GetModelErrors() { ModelErrorCollection modelError = new ModelErrorCollection(); foreach (ModelError error in ViewData.ModelState.Values.SelectMany(entry => entry.Errors)) { modelError.Add(error); } return modelError; }
protected ModelErrorCollection GetModelErrors() { ModelErrorCollection modelError = new ModelErrorCollection(); foreach (ModelError error in ViewData.ModelState.Values.SelectMany(entry => entry.Errors)) { modelError.Add(error); } return(modelError); }
public static ModelErrorCollection AsModelErrorCollection(this ModelStateDictionary modelState) { var collection = new ModelErrorCollection(); modelState.Values.SelectMany(v => v.Errors) .Select(e => e.ErrorMessage) .ToList() .ForEach(message => collection.Add(message)); return(collection); }
public static ModelErrorCollection Validar(Peca peca) { var mensagens = new ModelErrorCollection(); if (String.IsNullOrWhiteSpace(peca.Nome)) { mensagens.Add("É obrigatório fornecer o nome da peça"); } if (String.IsNullOrWhiteSpace(peca.Descricao)) { mensagens.Add("É obrigatório fornecer a descrição da peça"); } if (peca.NormasEControlesRelacionados == null || peca.NormasEControlesRelacionados.Count() == 0) { mensagens.Add("É obrigatório relacionar ao menos uma norma ou controle à peça"); } return(mensagens); }
public void AddWithExceptionArgument() { // Arrange ModelErrorCollection collection = new ModelErrorCollection(); Exception ex = new Exception("some message"); // Act collection.Add(ex); // Assert ModelError modelError = Assert.Single(collection); Assert.Same(ex, modelError.Exception); }
public void AddWithStringArgument() { // Arrange ModelErrorCollection collection = new ModelErrorCollection(); // Act collection.Add("some message"); // Assert ModelError modelError = Assert.Single(collection); Assert.Equal("some message", modelError.ErrorMessage); Assert.Null(modelError.Exception); }
public void AddWithExceptionArgument() { // Arrange ModelErrorCollection collection = new ModelErrorCollection(); Exception ex = new Exception("some message"); // Act collection.Add(ex); // Assert Assert.AreEqual(1, collection.Count); ModelError modelError = collection[0]; Assert.AreSame(ex, modelError.Exception); }
public void AddWithStringArgument() { // Arrange ModelErrorCollection collection = new ModelErrorCollection(); // Act collection.Add("some message"); // Assert Assert.AreEqual(1, collection.Count); ModelError modelError = collection[0]; Assert.AreEqual("some message", modelError.ErrorMessage); Assert.IsNull(modelError.Exception); }
//error related extensions public static ModelErrorCollection GetModelErrors(this Controller controller) { ModelErrorCollection errors = new ModelErrorCollection(); foreach (ModelState modelState in controller.ViewData.ModelState.Values) { foreach (ModelError error in modelState.Errors) { errors.Add(error); } } return errors; }
//error related extensions public static ModelErrorCollection GetModelErrors(this Controller controller) { ModelErrorCollection errors = new ModelErrorCollection(); foreach (ModelState modelState in controller.ViewData.ModelState.Values) { foreach (ModelError error in modelState.Errors) { errors.Add(error); } } return(errors); }
public ActionResult LogOn(LogOnModel model) { if (!ModelState.IsValid) { ModelErrorCollection captchaErr = ModelState["CaptchaCode"].Errors; if (captchaErr.Any()) { captchaErr.Clear(); captchaErr.Add("Вы должны указать символы, изображенные на картинке."); } return(ContextDependentView()); } if (Membership.ValidateUser(model.UserName, model.Password)) { FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); if (!Request["ReturnURL"].IsNullOrEmpty()) { return(new RedirectResult(Request["ReturnURL"])); } return(RedirectToAction("Index", AccessHelper.getStartUserController(model.UserName), new { lang = AccessHelper.CurrentLang.ShortName })); } return(ContextDependentView()); }
protected ModelErrorCollection GetErrors(ModelStateDictionary modelStateDictionary) { ModelErrorCollection errors = new ModelErrorCollection(); foreach (KeyValuePair<string, ModelState> pair in modelStateDictionary) { foreach (ModelError error in pair.Value.Errors) { errors.Add(error); } } return errors; }