title: How to validate textbox based on checkbox value
body: <p>Iam trying to validate textbox based on checkbox value. please view my model class and IsValid override method.</p>
  
  <pre><code>public class Product
  {
      //Below property value(HaveExperiance)
      [MustBeProductEntered(HaveExperiance)] 
      public string ProductName { get; set; }
  
      public bool HaveExperiance { get; set; }
  }
  
  public class MustBeTrueAttribute : ValidationAttribute
  {
      //Here i need the value of HaveExperiance property which 
      //i passed from  [MustBeProductEntered(HaveExperiance)]  in product class above.
      public override bool IsValid(object value)
      {
          return value is bool &amp;&amp; (bool)value;
      }
  }
  </code></pre>
  
  <p>You can see above in <code>ProductName</code> property in <code>product</code> class where iam trying to pass the <code>HaveExperiance</code> class property value, If it checked then user must have to fill the <code>ProductName</code> textbox. </p>
  
  <p>So my orignal question is that how can i validate the <code>ProductName</code> textbox based on the <code>HaveExperiance</code> value, thanks in advance.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.Data.Entity;
  using System.Linq;
  using System.Reflection;
  using System.Web;
  using System.Web.Mvc;
  
  namespace Mvc.Affiliates.Models
  {
      public class MyProducts
      {
          [Key]
          [Required(ErrorMessage = "Please insert product id.")]
          public string ProductId { get; set; }
  
          [RequiredIf("HaveExperiance")]
          public string ProductName { get; set; }
  
          public bool HaveExperiance { get; set; }
          public List&lt;MyProducts&gt; prolist { get; set; }
      }
  
      public class RequiredIfAttribute : ValidationAttribute
      {
          private RequiredAttribute _innerAttribute = new RequiredAttribute();
  
          public string Property { get; set; }
  
          public object Value { get; set; }
  
          public RequiredIfAttribute(string typeProperty)
          {
              Property = typeProperty;
          }
  
          public RequiredIfAttribute(string typeProperty, object value)
          {
              Property = typeProperty;
              Value = value;
          }
  
          public override bool IsValid(object value)
          {
              return _innerAttribute.IsValid(value);
          }
      }
  
      public class RequiredIfValidator : DataAnnotationsModelValidator&lt;RequiredIfAttribute&gt;
      {
          public RequiredIfValidator(ModelMetadata metadata, ControllerContext context, RequiredIfAttribute attribute) : base(metadata, context, attribute) { }
  
          public override IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules()
          {
              return base.GetClientValidationRules();
          }
  
          public override IEnumerable&lt;ModelValidationResult&gt; Validate(object container)
          {
              PropertyInfo field = Metadata.ContainerType.GetProperty(Attribute.Property);
  
              if (field != null)
              {
                  var value = field.GetValue(container, null);
  
                  if ((value != null &amp;&amp; Attribute.Value == null) || (value != null &amp;&amp; value.Equals(Attribute.Value)))
                  {
                      if (!Attribute.IsValid(Metadata.Model))
                      {
                          yield return new ModelValidationResult { Message = ErrorMessage };
                      }
                  }
              }
          }
      }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public class HomeController : Controller
      {
          //
          // GET: /Home/
  
          MvcDbContext _db = new MvcDbContext();
          public ActionResult Index()
          {
            return View();
          }
  
          [HttpPost]
          public ActionResult Index(MyProducts model)
          {
              string ProductId = model.ProductId;
              string ProductName = model.ProductName;
              //bool remember = model.HaveExperiance;
              return View();
          }
      }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>@model   Mvc.Affiliates.Models.MyProducts
  @{
      ViewBag.Title = "Index";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  
  &lt;h2&gt;Details&lt;/h2&gt;
  &lt;br /&gt;
  
  &lt;div style="height:200px; width:100%"&gt;
    @using  (Html.BeginForm("Index","Home", FormMethod.Post))
      {
  
     &lt;h2&gt;Details&lt;/h2&gt;
  
          @Html.LabelFor(model =&gt; model.ProductId)
          @Html.TextBoxFor(model =&gt; model.ProductId)
  
          @Html.LabelFor(model =&gt; model.ProductName)
          @Html.EditorFor(model =&gt; model.ProductName)
          @Html.ValidationMessageFor(model =&gt; model.ProductName, "*")
  
          @Html.CheckBoxFor(model =&gt; model.HaveExperiance)
          @Html.ValidationMessageFor(model =&gt; model.HaveExperiance, "*")
         &lt;input type="submit" value="Submit" /&gt;
    }
  
  &lt;/div&gt;
  </code></pre>
  
  <p>So far i have tried the above code, Actually i needed when i click on checkbox then it should start validate my ProductName textbox, if uncheck then not. iam missing a little thing in above code, please help and rectify me.</p>