Created
June 14, 2018 04:22
-
-
Save danielplawgo/eb611e9b400e90b11e2373a8e584dff1 to your computer and use it in GitHub Desktop.
Integracja Fluent Validation z ASP.NET MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Validator(typeof(CreateUserViewModelValidator))] | |
public class CreateUserViewModel | |
{ | |
public string Email { get; set; } | |
public bool CreateInvoice { get; set; } | |
public string Nip { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CreateUserViewModelValidator : AbstractValidator<CreateUserViewModel> | |
{ | |
public CreateUserViewModelValidator() | |
{ | |
RuleFor(u => u.Email) | |
.Cascade(CascadeMode.StopOnFirstFailure) | |
.NotEmpty() | |
.EmailAddress(); | |
RuleFor(u => u.Nip) | |
.NotEmpty() | |
.When(u => u.CreateInvoice); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FluentValidationModelValidatorProvider.Configure(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Result | |
{ | |
public bool Success { get; set; } | |
public IEnumerable<ErrorMessage> Errors { get; set; } | |
public static Result<T> Ok<T>(T value) | |
{ | |
return new Result<T>() | |
{ | |
Success = true, | |
Value = value | |
}; | |
} | |
public static Result<T> Failure<T>(IEnumerable<ValidationFailure> validationFailures) | |
{ | |
var result = new Result<T>(); | |
result.Success = false; | |
result.Errors = validationFailures.Select(v => new ErrorMessage() | |
{ | |
PropertyName = v.PropertyName, | |
Message = v.ErrorMessage | |
}); | |
return result; | |
} | |
} | |
public class Result<T> : Result | |
{ | |
public T Value { get; set; } | |
} | |
public class ErrorMessage | |
{ | |
public string PropertyName { get; set; } | |
public string Message { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ResultExtensions | |
{ | |
public static void AddErrorToModelState(this Result result, ModelStateDictionary modelState) | |
{ | |
if (result.Success) | |
{ | |
return; | |
} | |
foreach (var error in result.Errors) | |
{ | |
modelState.AddModelError(error.PropertyName, error.Message); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class User | |
{ | |
public string Email { get; set; } | |
public bool CreateInvoice { get; set; } | |
public string Nip { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpGet] | |
public ActionResult ViewModel() | |
{ | |
return View(new CreateUserViewModel()); | |
} | |
[HttpPost] | |
public ActionResult ViewModel(CreateUserViewModel viewModel) | |
{ | |
if(ModelState.IsValid == false) | |
{ | |
return View(viewModel); | |
} | |
return Content("Dodano"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpGet] | |
public ActionResult Model() | |
{ | |
return View(new CreateWithModelUserViewModel()); | |
} | |
[HttpPost] | |
public ActionResult Model(CreateWithModelUserViewModel viewModel) | |
{ | |
if(ModelState.IsValid == false) | |
{ | |
return View(viewModel); | |
} | |
var user = new User() //I normally use the automapper to map model <-> viewmodel | |
{ | |
Email = viewModel.Email, | |
CreateInvoice = viewModel.CreateInvoice, | |
Nip = viewModel.Nip | |
}; | |
var result = UsersLogic.Create(user); | |
if(result.Success == false) | |
{ | |
result.AddErrorToModelState(ModelState); | |
return View(viewModel); | |
} | |
return Content("Dodano"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UsersLogic | |
{ | |
protected UserValidator Validator { get; set; } | |
public UsersLogic() | |
{ | |
Validator = new UserValidator(); | |
} | |
public Result<User> Create(User user) | |
{ | |
var validationResult = Validator.Validate(user); | |
if(validationResult.IsValid == false) | |
{ | |
return Result.Failure<User>(validationResult.Errors); | |
} | |
//zapis danych do bazy | |
return Result.Ok<User>(user); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserValidator : AbstractValidator<User> | |
{ | |
public UserValidator() | |
{ | |
RuleFor(u => u.Email) | |
.Cascade(CascadeMode.StopOnFirstFailure) | |
.NotEmpty() | |
.EmailAddress(); | |
RuleFor(u => u.Nip) | |
.NotEmpty() | |
.When(u => u.CreateInvoice); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment