Ejemplo n.º 1
0
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var result = base.BindModel(controllerContext, bindingContext);
            if (result == null)
            {
                return result;
            }

            var runner = new ValidatorRunner(new CachedValidationRegistry());
            if (runner.IsValid(result))
            {
                return result;
            }

            var summary = runner.GetErrorSummary(result);
            var modelState = bindingContext.ModelState;
            foreach (var invalidProperty in summary.InvalidProperties)
            {
                foreach (var error in summary.GetErrorsForProperty(invalidProperty))
                {
                    modelState.AddModelError(invalidProperty, error);
                }
            }

            return result;
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var order = new Order
            {
                Product = new Product { Name = "Widget", MinQuantity = 5, MaxQuantity = 100 },
                Quantity = 50
            };

            var runner = new ValidatorRunner(new CachedValidationRegistry());
            if (runner.IsValid(order))
            {
                Console.WriteLine("The order is valid!");
            }
            else
            {
                ErrorSummary summary = runner.GetErrorSummary(order);
                foreach (var invalidProperty in summary.InvalidProperties)
                {
                    Console.WriteLine("{0} is invalid because:", invalidProperty);
                    foreach (var error in summary.GetErrorsForProperty(invalidProperty))
                    {
                        Console.WriteLine("\t * {0}", error);
                    }
                }
            }

            Console.ReadLine();
        }
Ejemplo n.º 3
0
		/// <errorSummary>
		/// Determines whether the specified instance is valid.
		/// </errorSummary>
		/// <param name="instance">The instance.</param>
		/// <param name="fieldValue">The field value.</param>
		/// <returns>
		/// 	<c>true</c> if the specified instance is valid; otherwise, <c>false</c>.
		/// </returns>
		public override bool IsValid(object instance, object fieldValue)
		{
			ValidatorRunner runner = new ValidatorRunner(validationRegistry.BaseRegistry);
			bool valid =  runner.IsValid(instance);

			if (!valid)
				errorSummary = runner.GetErrorSummary(instance);

			return valid;
		}
Ejemplo n.º 4
0
 public void Index([DataBind("ContactRequest")] ContactRequest request)
 {
     var runner = new ValidatorRunner(new CachedValidationRegistry());
     if (!runner.IsValid(request))
     {
         ErrorSummary summary = runner.GetErrorSummary(request);
         PropertyBag["ContactRequest"] = request;
         PropertyBag["error"] = summary;
     }
     else
     {
         var parameter = new Dictionary<string, object> { { "request", request } };
         var message = RenderMailMessage("contact", null, (IDictionary)parameter);
         message.Encoding = System.Text.Encoding.UTF8;
         DeliverEmail(message);
         RedirectToAction("Thanks");
     }
 }
Ejemplo n.º 5
0
        public ActionResult Login(string username, string password, string redirectUrl)
        {
            var loginData = new LoginData(username, password);

            var validationRunner = new ValidatorRunner(new CachedValidationRegistry());

            if (!validationRunner.IsValid(loginData))
            {
                AddErrorMessages(validationRunner.GetErrorSummary(loginData).ErrorMessages);
                return RedirectToAction("Login");
            }

            if (!authenticationService.SignIn(username, password))
            {
                AddErrorMessage("Invalid Login");
                return RedirectToAction("Login");
            }

            if (redirectUrl != null)
                return Redirect(redirectUrl);

            return RedirectToAction("Index", "Account");
        }
Ejemplo n.º 6
0
        public ActionResult Register(RegistrationData registrationData)
        {
            var validationRunner = new ValidatorRunner(new CachedValidationRegistry());

            if (!validationRunner.IsValid(registrationData))
            {
                // there were errors, report them back to the user
                foreach (var errorMessage in validationRunner.GetErrorSummary(registrationData).ErrorMessages)
                    AddErrorMessage(errorMessage);

                TempData.Add(registrationData);
                return RedirectToAction("Register");
            }

            Member member = memberService.GetByUsername(registrationData.UserName);

            if (member != null)
            {
                userSession.PushUserMessage(FlashMessage.MessageType.Error, "A user with that username already exists, please try again");
                TempData.Add(registrationData);
                return RedirectToAction("Register");
            }

            member = new Member
                         {
                             Username = registrationData.UserName,
                             Email = registrationData.Email,
                             PasswordSalt = cryptographer.CreateSalt(),
                             FirstName = registrationData.FirstName,
                             LastName = registrationData.LastName
                         };

            member.Password = cryptographer.Hash(registrationData.Password, member.PasswordSalt);

            memberService.Add(member);

            authenticator.SignIn(member);

            return RedirectToAction("Index");
        }
Ejemplo n.º 7
0
        protected void OnSaveButtonClicked (object sender, System.EventArgs e)
        {
            mycase.Name = nameEntry.Text;
            mycase.start_date = startDateSelector.SelectedDate ();
            mycase.StartDateType = startDateSelector.SelectedDateType ();
            mycase.AffectedPeople = System.Convert.ToInt32(affectedPeople.Text);

            mycase.end_date = endDateSelector.SelectedDate ();
            mycase.EndDateType = endDateSelector.SelectedDateType ();

            mycase.NarrativeDescription = description.Text;
            mycase.Summary = summary.Text;
            mycase.Observations = observations.Text;

            if (mycase.IsValid())
            {
                mycase.SaveAndFlush ();
                this.IsEditing = false;
                if (CaseSaved != null)
                    CaseSaved (mycase, e);
            } else {
                ResourceManager mgr = new ResourceManager("Castle.Components.Validator.Messages", Assembly.GetAssembly(typeof(Castle.Components.Validator.CachedValidationRegistry)));
                var runner = new ValidatorRunner(new CachedValidationRegistry(mgr));
                if (runner.IsValid(mycase) || !runner.HasErrors(mycase)) return;
                //var summary = runner.GetErrorSummary(mycase);

                Console.WriteLine( String.Join(",", mycase.ValidationErrorMessages) );
                new ValidationErrorsDialog (mycase.PropertiesValidationErrorMessages, (Gtk.Window)this.Toplevel);
            }
        }
Ejemplo n.º 8
0
 public void PersonalInformation([DataBind("Personal")] PersonalInformationViewModel viewModel)
 {
     Flash["categories"] = Category.All;
     var runner = new ValidatorRunner(new CachedValidationRegistry());
     if (runner.IsValid(viewModel))
     {
         Session["PersonalInfo"] = viewModel;
         RedirectToAction("Thanks");
     }
     else
     {
         Flash["Personal"] = viewModel;
         Flash["summary"] = runner.GetErrorSummary(viewModel);
     }
 }
Ejemplo n.º 9
0
 public void AccountInformation([DataBind("Register")] AccountInformationViewModel viewModel)
 {
     ValidatorRunner runner = new ValidatorRunner(new CachedValidationRegistry());
     if (runner.IsValid(viewModel))
     {
         Session["AccountInfo"] = viewModel;
         RedirectToAction("PersonalInformation");
     }
     else
     {
         var summary = runner.GetErrorSummary(viewModel);
         Flash["summary"] = summary;
         Flash["Register"] = viewModel;
     }
 }