CallIsValid() public method

public CallIsValid ( object value, System.ComponentModel.DataAnnotations.ValidationContext validationContext ) : System.ComponentModel.DataAnnotations.ValidationResult
value object
validationContext System.ComponentModel.DataAnnotations.ValidationContext
return System.ComponentModel.DataAnnotations.ValidationResult
Example #1
0
        public void IsValid_Object_ValidationContext()
        {
            var attr = new ValidateBarAttribute();

            try {
                // ...
                attr.CallIsValid(null, null);
                Assert.Fail("#A1");
            } catch (NullReferenceException) {
                // success
            }

            var vc = new ValidationContext("stuff", null, null);
            var vr = attr.CallIsValid(null, vc);

            Assert.IsNotNull(vr, "#A2-1");
            Assert.IsNotNull(vr.ErrorMessage, "#A2-2");
            Assert.AreEqual("The field String is invalid.", vr.ErrorMessage, "#A2-3");
            Assert.IsNotNull(vr.MemberNames, "#A2-4");

            int count = 0;

            foreach (string s in vr.MemberNames)
            {
                count++;
            }
            Assert.AreEqual(0, count, "#A2-5");

            vc.MemberName = "SomeMember";
            vr            = attr.CallIsValid(null, vc);
            Assert.IsNotNull(vr, "#A3-1");
            Assert.IsNotNull(vr.ErrorMessage, "#A3-2");
            Assert.AreEqual("The field String is invalid.", vr.ErrorMessage, "#A3-3");
            Assert.IsNotNull(vr.MemberNames, "#A3-4");

            var list = new List <string> ();

            foreach (string s in vr.MemberNames)
            {
                list.Add(s);
            }
            Assert.AreEqual(1, list.Count, "#A3-5");
            Assert.AreEqual("SomeMember", list [0], "#A3-6");
        }
Example #2
0
		public void IsValid_Object_ValidationContext ()
		{
			var attr = new ValidateBarAttribute ();

			try {
				// ...
				attr.CallIsValid (null, null);
				Assert.Fail ("#A1");
			} catch (NullReferenceException) {
				// success
			}

			var vc = new ValidationContext ("stuff", null, null);
			var vr = attr.CallIsValid (null, vc);
			Assert.IsNotNull (vr, "#A2-1");
			Assert.IsNotNull (vr.ErrorMessage, "#A2-2");
			Assert.AreEqual ("The field String is invalid.", vr.ErrorMessage, "#A2-3");
			Assert.IsNotNull (vr.MemberNames, "#A2-4"); 
			
			int count = 0;
			foreach (string s in vr.MemberNames)
				count++;
			Assert.AreEqual (0, count, "#A2-5");

			vc.MemberName = "SomeMember";
			vr = attr.CallIsValid (null, vc);
			Assert.IsNotNull (vr, "#A3-1");
			Assert.IsNotNull (vr.ErrorMessage, "#A3-2");
			Assert.AreEqual ("The field String is invalid.", vr.ErrorMessage, "#A3-3");
			Assert.IsNotNull (vr.MemberNames, "#A3-4");

			var list = new List <string> ();
			foreach (string s in vr.MemberNames)
				list.Add (s);
			Assert.AreEqual (1, list.Count, "#A3-5");
			Assert.AreEqual ("SomeMember", list [0], "#A3-6");
		}