Is RequiredIfNotEmpty Functioning Correctly?

Nov 25, 2011 at 12:29 AM

Hello,

I created a very simple test project in VS2010 MVC3 to illustrate the problem here.

My model class looks like below, the intention is to make Property1 mandatory; if Property3 is not empty then Property2 is required; if Property2 is not empty then Property3 is required. In a simple word Property2 and Property3 must be either both blank or both with values.

	public class HomeModel {
		[Required]
		public string Property1 {
			get;
			set;
		}

		[RequiredIfNotEmpty("Property3")]
		public string Property2 {
			get;
			set;
		}

		[RequiredIfNotEmpty("Property2")]
		public string Property3 {
			get;
			set;
		}
	}
My controller looks like:
	public class HomeController : Controller {
		public ActionResult Index() {
			HomeModel model = new HomeModel();
			return View(model);
		}
	}
My view looks like:
@model FoolproofTest.Models.HomeModel
@{
	Layout = null;
}
<!DOCTYPE html>
<html>
<head>
	<title>Index</title>
	<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
	<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
	<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
	<script src="@Url.Content("~/Scripts/mvcfoolproof.unobtrusive.js")" type="text/javascript"></script>
</head>
<body>
	@using (Html.BeginForm()) {
		@Html.TextBoxFor(x => x.Property1)
		@Html.ValidationMessageFor(x => x.Property1)
		<br />

	 	@Html.TextBoxFor(x => x.Property2)
		@Html.ValidationMessageFor(x => x.Property2)
		<br />

		@Html.TextBoxFor(x => x.Property3)
		@Html.ValidationMessageFor(x => x.Property3)
		<br />
	 
		<input type="submit" value="Submit" />
	 }
</body>
</html>
 
When I load the page can see 3 textboxes and a button, when I click the submit button without entering any single character and I see error messages appear next to each of the textbox,
  • The Property1 field is required.
  • Property2 is required due to Property3 not being empty. 
  • Property3 is required due to Property2 not being empty.
 
Is it the expected behaviour of RequiredIfNotEmpty? I think if I don't enter anything I should only see the validation error of first text box.
Coordinator
Nov 29, 2011 at 2:31 PM
Yes, this is expected behavior. All of the properties are validated when you hit submit and will display their error if they are invalid.

One option is to create a custom valiadator and place it on only one of your properties.

On Thu, Nov 24, 2011 at 6:29 PM, hardywang <notifications@codeplex.com> wrote:

From: hardywang

Hello,

I created a very simple test project in VS2010 MVC3 to illustrate the problem here.

My model class looks like below, the intention is to make Property1 mandatory; if Property3 is not empty then Property2 is required; if Property2 is not empty then Property3 is required. In a simple word Property2 and Property3 must be either both blank or both with values.

	public class HomeModel {
		[Required]
		public string Property1 {
			get;
			set;
		}

		[RequiredIfNotEmpty("Property3")]
		public string Property2 {
			get;
			set;
		}

		[RequiredIfNotEmpty("Property2")]
		public string Property3 {
			get;
			set;
		}
	}
My controller looks like:
	public class HomeController : Controller {
		public ActionResult Index() {
			HomeModel model = new HomeModel();
			return View(model);
		}
	}
My view looks like:
@model FoolproofTest.Models.HomeModel
@{
	Layout = null;
}
<!DOCTYPE html>
<html>
<head>
	<title>Index</title>
	<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
	<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
	<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
	<script src="@Url.Content("~/Scripts/mvcfoolproof.unobtrusive.js")" type="text/javascript"></script>
</head>
<body>
	@using (Html.BeginForm()) {
		@Html.TextBoxFor(x => x.Property1)
		@Html.ValidationMessageFor(x => x.Property1)
		<br />

	 	@Html.TextBoxFor(x => x.Property2)
		@Html.ValidationMessageFor(x => x.Property2)
		<br />

		@Html.TextBoxFor(x => x.Property3)
		@Html.ValidationMessageFor(x => x.Property3)
		<br />
	 
		<input type="submit" value="Submit" />
	 }
</body>
</html>
 
When I load the page can see 3 textboxes and a button, when I click the submit button without entering any single character and I see error messages appear next to each of the textbox,
  • The Property1 field is required.
  • Property2 is required due to Property3 not being empty.
  • Property3 is required due to Property2 not being empty.
 
Is it the expected behaviour of RequiredIfNotEmpty? I think if I don't enter anything I should only see the validation error of first text box.

Read the full discussion online.

To add a post to this discussion, reply to this email (foolproof@discussions.codeplex.com)

To start a new discussion for this project, email foolproof@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com




--
Nick Riggs
Nov 30, 2011 at 12:25 AM

Hi Nick

I know all fields are validated, but my concern is that if I don't enter anything to Property3 I should not see any error of Property2, right? Is it the purpose of RequiredIfNotEmpty?

Coordinator
Nov 30, 2011 at 1:30 PM
Sorry, I miss understood. No, that would be the expected behavior if you were using RequiredIfEmpty, not RequiredIfNotRempty.

I'll look into this later today.

On Tue, Nov 29, 2011 at 6:25 PM, hardywang <notifications@codeplex.com> wrote:

From: hardywang

Hi Nick

I know all fields are validated, but my concern is that if I don't enter anything to Property3 I should not see any error of Property2, right? Is it the purpose of RequiredIfNotEmpty?

Read the full discussion online.

To add a post to this discussion, reply to this email (foolproof@discussions.codeplex.com)

To start a new discussion for this project, email foolproof@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com




--
Nick Riggs
Dec 1, 2011 at 6:36 PM

I created defect for you.

Jun 28, 2012 at 1:14 PM

Hi, is this issue fixed?