title: KendoUI, Knockout, conditional validation of a field
body: <p>I am using kendoui widgets with knockoutjs for datasource. I have a checkbox that is data bound to <code>StartClientFromWebEnabled</code> observable variable. An input text box is visible only when the checkbox ic checked (<code>StartClientFromWebEnabled</code> is true). The input has a required attribute. I want the required validation to be triggered only when the checkbox is checked.</p>
  
  <p>Here is my html: </p>
  
  <pre><code>&lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;label for="startClientFromWebEnabled"&gt;Client Launch From Web:&lt;/label&gt;&lt;/td&gt;
          &lt;td&gt;&lt;input type="checkbox" id="startClientFromWebEnabled" name="startClientFromWebEnabled" data-bind="checked: StartClientFromWebEnabled, enable: IsEditable" onchange="startClientFromWebToggleRequiredAttribute()" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr data-bind="visible: StartClientFromWebEnabled"&gt;
          &lt;td&gt;&lt;label for="mimeType"&gt;Protocol:&lt;/label&gt;&lt;/td&gt;
          &lt;td&gt;
              &lt;input  id="mimeType" name="mimeType" data-bind= "value: MimeType, enable: IsEditable" /&gt;
              &lt;span class="k-invalid-msg" data-for="mimeType"&gt;&lt;/span&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>I tried some scenarios including setting <code>onChange event</code> on the checkbox with the following javascript function adding and removing the required attribute:</p>
  
  <pre><code>startClientFromWebToggleRequiredAttribute = function () {
      var checkbox = document.getElementById("startClientFromWebEnabled");
      var mimeType = document.getElementById("mimeType");
      if (checkbox.checked) {
          mimeType.setAttribute("required", "required");
      }
      else {
          mimeType.removeAttribute("required");
      }
  }
  </code></pre>
  
  <p>The problem is I will need this functionality for many dependent properties in my application and my option is to make this function generic with some parameters and call it from the html with the corresponding paramater values like this:</p>
  
  <pre><code>toggleRequiredAttribute = function (checkboxElement, inputElement1, inputElement2 ... ) {
      var checkbox = document.getElementById(checkboxElement);
      var inputElement1 = document.getElementById(inputElement1);
      if (checkbox.checked) {
          inputElement1.setAttribute("required", "required");
      }
      else {
          inputElement1.removeAttribute("required");
      }
  }
  
  &lt;input type="checkbox" id="startClientFromWebEnabled" name="startClientFromWebEnabled" data-bind="checked: StartClientFromWebEnabled, enable: IsEditable" onchange="toggleRequiredAttribute('startClientFromWebEnable', 'mimeType')" /&gt;
  </code></pre>
  
  <p>I really do not like this scenario. I wonder is there something like a conditional validation in kendoui that trigger only when some condition is satisfied. Any other suggestions are also welcome. </p>