public bool Validate(BusinessPersonViewModel viewModel, ModelStateDictionary modelState)
        {
            var validationRules = new List <ValidationRuleItem <BusinessPersonViewModel> >()
            {
                new ValidationRuleItem <BusinessPersonViewModel> {
                    Field = nameof(BusinessPersonViewModel.FirstName), Rule = model => !string.IsNullOrEmpty(model.FirstName), ErrorMessage = () => "validation.required".AsWebsiteText()
                },
                new ValidationRuleItem <BusinessPersonViewModel> {
                    Field = nameof(BusinessPersonViewModel.LastName), Rule = model => !string.IsNullOrEmpty(model.LastName), ErrorMessage = () => "validation.required".AsWebsiteText()
                },
                new ValidationRuleItem <BusinessPersonViewModel> {
                    Field = nameof(BusinessPersonViewModel.Email), Rule = model => !string.IsNullOrEmpty(model.Email), ErrorMessage = () => "validation.required".AsWebsiteText()
                },
                new ValidationRuleItem <BusinessPersonViewModel> {
                    Field = nameof(BusinessPersonViewModel.Email), Rule = model => _userValidationService.IsValidEmail(model.Email), ErrorMessage = () => "validation.email".AsWebsiteText()
                },
                new ValidationRuleItem <BusinessPersonViewModel> {
                    Field = nameof(BusinessPersonViewModel.Email), Rule = IsValidUserName, ErrorMessage = () => "validation.unique".AsWebsiteText()
                },
                new ValidationRuleItem <BusinessPersonViewModel> {
                    Field = nameof(BusinessPersonViewModel.Phone), Rule = model => !string.IsNullOrEmpty(model.Phone), ErrorMessage = () => "validation.required".AsWebsiteText()
                },
                new ValidationRuleItem <BusinessPersonViewModel> {
                    Field = nameof(BusinessPersonViewModel.Phone), Rule = model => _userValidationService.IsValidPhone(model.Phone), ErrorMessage = () => "validation.phone".AsWebsiteText()
                },
            };

            return(viewModel.IsValid(validationRules, modelState));
        }
        public bool Validate(AddressViewModel viewModel, ModelState modelState)
        {
            var validationRules = new List <ValidationRuleItem <AddressViewModel> >
            {
                new ValidationRuleItem <AddressViewModel> {
                    Field = nameof(AddressViewModel.Address), Rule = model => !string.IsNullOrEmpty(model.Address), ErrorMessage = "validation.required".AsWebSiteString()
                },
                new ValidationRuleItem <AddressViewModel> {
                    Field = nameof(AddressViewModel.ZipCode), Rule = model => !string.IsNullOrEmpty(model.ZipCode), ErrorMessage = "validation.required".AsWebSiteString()
                },
                new ValidationRuleItem <AddressViewModel> {
                    Field = nameof(AddressViewModel.City), Rule = model => !string.IsNullOrEmpty(model.City), ErrorMessage = "validation.required".AsWebSiteString()
                },
                new ValidationRuleItem <AddressViewModel> {
                    Field = nameof(AddressViewModel.Country), Rule = model => !string.IsNullOrEmpty(model.Country), ErrorMessage = "validation.required".AsWebSiteString()
                },
                new ValidationRuleItem <AddressViewModel> {
                    Field = nameof(AddressViewModel.PhoneNumber), Rule = model => !string.IsNullOrEmpty(model.PhoneNumber), ErrorMessage = "validation.required".AsWebSiteString()
                },
                new ValidationRuleItem <AddressViewModel> {
                    Field = nameof(AddressViewModel.PhoneNumber), Rule = model => _userValidationService.IsValidPhone(model.PhoneNumber), ErrorMessage = "validation.phone".AsWebSiteString()
                }
            };

            return(viewModel.IsValid(validationRules, modelState));
        }
        public bool IsValidBusinessCustomerDetailsForm(ModelState modelState, BusinessCustomerDetailsViewModel businessCustomerDetailsForm)
        {
            var firstNameField = nameof(businessCustomerDetailsForm.FirstName);
            var lastNameField  = nameof(businessCustomerDetailsForm.LastName);
            var phoneField     = nameof(businessCustomerDetailsForm.Phone);

            var validationRules = new List <ValidationRuleItem <BusinessCustomerDetailsViewModel> >()
            {
                new ValidationRuleItem <BusinessCustomerDetailsViewModel> {
                    Field = firstNameField, Rule = model => !string.IsNullOrEmpty(model.FirstName), ErrorMessage = "validation.required".AsWebSiteString()
                },
                new ValidationRuleItem <BusinessCustomerDetailsViewModel> {
                    Field = lastNameField, Rule = model => !string.IsNullOrEmpty(model.LastName), ErrorMessage = "validation.required".AsWebSiteString()
                },
                new ValidationRuleItem <BusinessCustomerDetailsViewModel> {
                    Field = phoneField, Rule = model => !string.IsNullOrEmpty(model.Phone), ErrorMessage = "validation.required".AsWebSiteString()
                },
                new ValidationRuleItem <BusinessCustomerDetailsViewModel> {
                    Field = phoneField, Rule = model => _userValidationService.IsValidPhone(model.Phone), ErrorMessage = "validation.phone".AsWebSiteString()
                },
            };

            return(businessCustomerDetailsForm.IsValid(validationRules, modelState));
        }
        public override bool Validate(ModelState modelState, CheckoutViewModel viewModel)
        {
            var validationRules = new List <ValidationRuleItem <CheckoutViewModel> >()
            {
                new ValidationRuleItem <CheckoutViewModel>
                {
                    Field        = ToModelStateField(nameof(viewModel.CustomerDetails), nameof(viewModel.CustomerDetails.PhoneNumber)),
                    Rule         = model => !string.IsNullOrWhiteSpace(viewModel.CustomerDetails?.PhoneNumber),
                    ErrorMessage = "validation.required".AsWebSiteString()
                },
                new ValidationRuleItem <CheckoutViewModel>
                {
                    Field        = ToModelStateField(nameof(viewModel.CustomerDetails), nameof(viewModel.CustomerDetails.PhoneNumber)),
                    Rule         = model => _userValidationService.IsValidPhone(viewModel.CustomerDetails?.PhoneNumber),
                    ErrorMessage = "validation.phone".AsWebSiteString()
                },
                new ValidationRuleItem <CheckoutViewModel>
                {
                    Field        = ToModelStateField(nameof(viewModel.CustomerDetails), nameof(viewModel.CustomerDetails.FirstName)),
                    Rule         = model => !string.IsNullOrWhiteSpace(model.CustomerDetails?.FirstName),
                    ErrorMessage = "validation.required".AsWebSiteString()
                },
                new ValidationRuleItem <CheckoutViewModel>
                {
                    Field        = ToModelStateField(nameof(viewModel.CustomerDetails), nameof(viewModel.CustomerDetails.LastName)),
                    Rule         = model => !string.IsNullOrEmpty(model.CustomerDetails?.LastName),
                    ErrorMessage = "validation.required".AsWebSiteString()
                },
                new ValidationRuleItem <CheckoutViewModel>
                {
                    Field        = nameof(viewModel.SelectedDeliveryMethod),
                    Rule         = model => model.SelectedDeliveryMethod.HasValue && model.SelectedDeliveryMethod.Value != Guid.Empty,
                    ErrorMessage = "validation.required".AsWebSiteString()
                },
                new ValidationRuleItem <CheckoutViewModel>
                {
                    Field        = ToModelStateField(nameof(viewModel.CustomerDetails), nameof(viewModel.CustomerDetails.Email)),
                    Rule         = model => !string.IsNullOrEmpty(model.CustomerDetails?.Email),
                    ErrorMessage = "validation.required".AsWebSiteString()
                },
                new ValidationRuleItem <CheckoutViewModel>
                {
                    Field        = ToModelStateField(nameof(viewModel.CustomerDetails), nameof(viewModel.CustomerDetails.Email)),
                    Rule         = model => _userValidationService.IsValidEmail(model.CustomerDetails?.Email),
                    ErrorMessage = "validation.email".AsWebSiteString()
                },
            };

            if (viewModel.IsBusinessCustomer)
            {
                validationRules.Add(new ValidationRuleItem <CheckoutViewModel>
                {
                    Field        = nameof(viewModel.SelectedCompanyAddressId),
                    Rule         = model => viewModel.SelectedCompanyAddressId.HasValue && viewModel.SelectedCompanyAddressId.Value != Guid.Empty,
                    ErrorMessage = "validation.required".AsWebSiteString()
                });

                return(viewModel.IsValid(validationRules, modelState));
            }

            validationRules.AddRange(new List <ValidationRuleItem <CheckoutViewModel> >()
            {
                new ValidationRuleItem <CheckoutViewModel>
                {
                    Field        = ToModelStateField(nameof(viewModel.CustomerDetails), nameof(viewModel.CustomerDetails.Address)),
                    Rule         = model => !string.IsNullOrWhiteSpace(model.CustomerDetails?.Address),
                    ErrorMessage = "validation.required".AsWebSiteString()
                },
                new ValidationRuleItem <CheckoutViewModel>
                {
                    Field        = ToModelStateField(nameof(viewModel.CustomerDetails), nameof(viewModel.CustomerDetails.ZipCode)),
                    Rule         = model => !string.IsNullOrWhiteSpace(model.CustomerDetails?.ZipCode),
                    ErrorMessage = "validation.required".AsWebSiteString()
                },
                new ValidationRuleItem <CheckoutViewModel>
                {
                    Field        = ToModelStateField(nameof(viewModel.CustomerDetails), nameof(viewModel.CustomerDetails.City)),
                    Rule         = model => !string.IsNullOrWhiteSpace(model.CustomerDetails?.City),
                    ErrorMessage = "validation.required".AsWebSiteString()
                },
                new ValidationRuleItem <CheckoutViewModel>
                {
                    Field        = ToModelStateField(nameof(viewModel.CustomerDetails), nameof(viewModel.CustomerDetails.Country)),
                    Rule         = model => !string.IsNullOrWhiteSpace(model.CustomerDetails?.Country),
                    ErrorMessage = "validation.required".AsWebSiteString()
                },
            });

            if (viewModel.SignUp && !_securityContextService.GetIdentityUserSystemId().HasValue)
            {
                validationRules.Add(new ValidationRuleItem <CheckoutViewModel>
                {
                    Field = ToModelStateField(nameof(viewModel.CustomerDetails), nameof(viewModel.CustomerDetails.Email)),
                    Rule  = model =>
                    {
                        var existingUserId = _securityContextService.GetPersonSystemId(viewModel.CustomerDetails.Email);
                        return(existingUserId == null || !existingUserId.HasValue);
                    },
                    ErrorMessage = "validation.emailinused".AsWebSiteString()
                });
            }

            if (viewModel.ShowAlternativeAddress && !(string.IsNullOrEmpty(viewModel.AlternativeAddress.FirstName) && string.IsNullOrEmpty(viewModel.AlternativeAddress.LastName) && string.IsNullOrEmpty(viewModel.AlternativeAddress.Address) &&
                                                      string.IsNullOrEmpty(viewModel.AlternativeAddress.ZipCode) && string.IsNullOrEmpty(viewModel.AlternativeAddress.City) && string.IsNullOrEmpty(viewModel.AlternativeAddress.PhoneNumber)))
            {
                validationRules.AddRange(new List <ValidationRuleItem <CheckoutViewModel> >()
                {
                    new ValidationRuleItem <CheckoutViewModel>
                    {
                        Field        = ToModelStateField(nameof(viewModel.AlternativeAddress), nameof(viewModel.AlternativeAddress.PhoneNumber)),
                        Rule         = model => !string.IsNullOrWhiteSpace(viewModel.AlternativeAddress?.PhoneNumber),
                        ErrorMessage = "validation.required".AsWebSiteString()
                    },
                    new ValidationRuleItem <CheckoutViewModel>
                    {
                        Field        = ToModelStateField(nameof(viewModel.AlternativeAddress), nameof(viewModel.AlternativeAddress.PhoneNumber)),
                        Rule         = model => _userValidationService.IsValidPhone(viewModel.AlternativeAddress?.PhoneNumber),
                        ErrorMessage = "validation.phone".AsWebSiteString()
                    },
                    new ValidationRuleItem <CheckoutViewModel>
                    {
                        Field        = ToModelStateField(nameof(viewModel.AlternativeAddress), nameof(viewModel.AlternativeAddress.FirstName)),
                        Rule         = model => !string.IsNullOrWhiteSpace(model.AlternativeAddress?.FirstName),
                        ErrorMessage = "validation.required".AsWebSiteString()
                    },
                    new ValidationRuleItem <CheckoutViewModel>
                    {
                        Field        = ToModelStateField(nameof(viewModel.AlternativeAddress), nameof(viewModel.AlternativeAddress.LastName)),
                        Rule         = model => !string.IsNullOrEmpty(model.AlternativeAddress?.LastName),
                        ErrorMessage = "validation.required".AsWebSiteString()
                    },
                    new ValidationRuleItem <CheckoutViewModel>
                    {
                        Field        = ToModelStateField(nameof(viewModel.AlternativeAddress), nameof(viewModel.AlternativeAddress.Address)),
                        Rule         = model => !string.IsNullOrWhiteSpace(model.AlternativeAddress?.Address),
                        ErrorMessage = "validation.required".AsWebSiteString()
                    },
                    new ValidationRuleItem <CheckoutViewModel>
                    {
                        Field        = ToModelStateField(nameof(viewModel.AlternativeAddress), nameof(viewModel.AlternativeAddress.ZipCode)),
                        Rule         = model => !string.IsNullOrWhiteSpace(model.AlternativeAddress?.ZipCode),
                        ErrorMessage = "validation.required".AsWebSiteString()
                    },
                    new ValidationRuleItem <CheckoutViewModel>
                    {
                        Field        = ToModelStateField(nameof(viewModel.AlternativeAddress), nameof(viewModel.AlternativeAddress.City)),
                        Rule         = model => !string.IsNullOrWhiteSpace(model.AlternativeAddress?.City),
                        ErrorMessage = "validation.required".AsWebSiteString()
                    },
                    new ValidationRuleItem <CheckoutViewModel>
                    {
                        Field        = ToModelStateField(nameof(viewModel.AlternativeAddress), nameof(viewModel.AlternativeAddress.Country)),
                        Rule         = model => !string.IsNullOrWhiteSpace(model.AlternativeAddress?.Country),
                        ErrorMessage = "validation.required".AsWebSiteString()
                    },
                });
            }

            return(viewModel.IsValid(validationRules, modelState));
        }