Skip to content

Instantly share code, notes, and snippets.

@jhollingworth
Created August 9, 2010 15:53
Show Gist options
  • Save jhollingworth/515608 to your computer and use it in GitHub Desktop.
Save jhollingworth/515608 to your computer and use it in GitHub Desktop.
protected void Application_Start()
{
XmlConfigurator.Configure();
_log.Debug("Starting the application");
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new AreaViewEngine());
ModelBinders.Binders.DefaultBinder = new SharpModelBinder();
InitializeServiceLocator();
AreaRegistration.RegisterAllAreas();
RouteRegistrar.RegisterRoutesTo(RouteTable.Routes);
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
ModelValidatorProviders.Providers.Add(
new FluentValidationModelValidatorProvider(new AttributedValidatorFactory()));
}
[Validator(typeof(UploadVenueViewModelValidator))]
public class UploadVenueViewModel
{
public int VenueId { get; set; }
public int MenuTitleId { get; set; }
public HttpPostedFileBase Menu { get; set; }
public PriceType PriceType { get; set; }
public double PriceRangeFrom { get; set; }
public double PriceRangeTo { get; set; }
public DateTime AvailableFrom { get; set; }
public DateTime AvailableTo { get; set; }
public int MinCovers { get; set; }
public int MaxCovers { get; set; }
public bool DepositRequired { get; set; }
public double DepositAmount { get; set; }
public bool DepositRefundable { get; set; }
public bool CancellationFee { get; set; }
public int CancellationPeriod { get; set; }
public bool BookingFormRequired { get; set; }
public bool DiscretionaryServiceCharge { get; set; }
public string OtherConditions { get; set; }
public List<SelectListItem> MenuTitles { get; set; }
public List<SelectListItem> PriceTypes { get; set; }
public UploadVenueViewModel()
{
MenuTitles = new List<SelectListItem>();
}
}
public enum PriceType
{
Fixed, Range
}
public class UploadVenueViewModelValidator : AbstractValidator<UploadVenueViewModel>
{
public UploadVenueViewModelValidator()
{
RuleFor(u => u.VenueId)
.GreaterThan(0).WithMessage(Resource.VenueIdInvalid);
RuleFor(u => u.MenuTitleId).GreaterThan(0)
.WithMessage(Resource.MenuTitleRequired);
RuleFor(u => u.PriceRangeFrom)
.GreaterThan(0).WithMessage("Please specify the starting price range");
RuleFor(u => u.PriceRangeTo).GreaterThan(0).WithMessage("Please specify the end of the price range");
RuleFor(u => u.PriceRangeTo).GreaterThan(c => c.PriceRangeFrom)
.WithMessage("The end of the price range must be greater than the start");
RuleFor(u => u.AvailableFrom).GreaterThanOrEqualTo(DateTime.UtcNow).WithMessage("Must not be in the past");
RuleFor(u => u.AvailableTo).GreaterThanOrEqualTo(DateTime.UtcNow).WithMessage("Must not be in the past");
RuleFor(u => u.AvailableTo).GreaterThan(u => u.AvailableFrom)
.WithMessage("The end of the avaliability must be after the begining of the avaliability");
RuleFor(u => u.MinCovers)
.GreaterThan(0)
.WithMessage("Please specify the minimum number of covers");
RuleFor(u => u.MaxCovers)
.GreaterThan(0)
.WithMessage("Please specify the maximum number of covers");
RuleFor(u => u.MaxCovers).GreaterThan(u => u.MinCovers)
.WithMessage("Max number of covers must be greater than min number of covers");
RuleFor(u => u.DepositAmount)
.GreaterThan(0)
.When(u => u.DepositRequired)
.WithMessage("Must specify the deposit amount");
RuleFor(u => u.CancellationPeriod)
.GreaterThan(0)
.When(u => u.CancellationFee)
.WithMessage("Please specify the cancellation period");
RuleFor(u => u.Menu).NotNull().WithMessage(Resource.MenuRequired);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment