public void ValuesSet()
        {
            // Arrange
            ModelMetadata metadata = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length");
            var attribute = new RequiredAttribute();

            // Act
            var validator = new DataAnnotationsModelValidator(metadata, _noValidatorProviders, attribute);

            // Assert
            Assert.Same(attribute, validator.Attribute);
            Assert.Equal(attribute.FormatErrorMessage("Length"), validator.ErrorMessage);
        }
        public void ValuesSet() {
            // Arrange
            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => 15, typeof(string), "Length");
            ControllerContext context = new ControllerContext();
            RequiredAttribute attribute = new RequiredAttribute();

            // Act
            DataAnnotationsModelValidator validator = new DataAnnotationsModelValidator(metadata, context, attribute);

            // Assert
            Assert.AreSame(attribute, validator.Attribute);
            Assert.AreEqual(attribute.FormatErrorMessage("Length"), validator.ErrorMessage);
        }
 public static string GetRequiredErrorMessage(string field)
 {
     var attr = new RequiredAttribute();
     return attr.FormatErrorMessage(field);
 }
        public void Entity_Invoke_Validates_Required_Properties()
        {
            TestEntityContainer container = new TestEntityContainer();
            var invocableCity = new City();
            container.GetEntitySet<City>().Add(invocableCity);

            RequiredAttribute expectedAttribute = new RequiredAttribute();
            string expectedMember = "CityName";
            string expectedError = expectedAttribute.FormatErrorMessage(expectedMember);

            ExceptionHelper.ExpectValidationException(delegate
            {
                invocableCity.AssignCityZone("West");
            }, expectedError, expectedAttribute.GetType(), null);
        }
        public DateTime? getJoinedDate()
        {
            var yearField = Request["JoinedDateYear"];
            var monthField = Request["JoinedDateMonth"];
            var required = new RequiredAttribute();
            if (string.IsNullOrEmpty(yearField))
            {
                ModelState.AddModelError("JoinedDate", required.FormatErrorMessage(WebResources.JoiningDateYear));
            }
            else if (string.IsNullOrEmpty(monthField))
            {
                ModelState.AddModelError("JoinedDate", required.FormatErrorMessage(WebResources.JoiningDateMonth));
            }
            else
            {
                var dateRange = new RangeAttribute(
                    typeof(DateTime),
                    MinDate.ToShortDateString(),
                    DateTime.Now.ToShortDateString()
                );
                DateTime? joinedDate = null;
                try
                {
                    joinedDate = new DateTime(Convert.ToInt32(yearField), Convert.ToInt32(monthField), 1);
                }
                catch (FormatException ex)
                {
                    ModelState.AddModelError("JoinedDate", ex);
                }
                catch (ArgumentOutOfRangeException)
                {
                    ModelState.AddModelError("JoinedDate", dateRange.FormatErrorMessage(WebResources.JoiningDate));
                }
                catch (ArgumentException ex)
                {
                    ModelState.AddModelError("JoinedDate", ex);
                }

                if (joinedDate != null)
                {
                    var now = DateTime.Now;

                    if (!dateRange.IsValid(joinedDate.Value))
                    {
                        ModelState.AddModelError("JoinedDate", dateRange.FormatErrorMessage(WebResources.JoiningDate));
                    }
                    else
                    {
                        return joinedDate;
                    }
                }
            }

            this.ViewData["JoinedDateYear"] = yearField;
            this.ViewData["JoinedDateYear"] = monthField;
            return null;
        }