Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
Created June 14, 2018 04:22
Show Gist options
  • Save danielplawgo/eb611e9b400e90b11e2373a8e584dff1 to your computer and use it in GitHub Desktop.
Save danielplawgo/eb611e9b400e90b11e2373a8e584dff1 to your computer and use it in GitHub Desktop.
Integracja Fluent Validation z ASP.NET MVC
[Validator(typeof(CreateUserViewModelValidator))]
public class CreateUserViewModel
{
public string Email { get; set; }
public bool CreateInvoice { get; set; }
public string Nip { get; set; }
}
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);
}
}
FluentValidationModelValidatorProvider.Configure();
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; }
}
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);
}
}
}
public class User
{
public string Email { get; set; }
public bool CreateInvoice { get; set; }
public string Nip { get; set; }
}
[HttpGet]
public ActionResult ViewModel()
{
return View(new CreateUserViewModel());
}
[HttpPost]
public ActionResult ViewModel(CreateUserViewModel viewModel)
{
if(ModelState.IsValid == false)
{
return View(viewModel);
}
return Content("Dodano");
}
[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");
}
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);
}
}
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