Please note the way the library handles conditional validation changed dramatically in version 2.0. Code written for 1.x will not be compatible
Out of the box, the TMT Validator doesn't provide a way to perform conditional validation. This was a design choice. Creating a flexible and generic solution would require a lot of code and would undoubtedly fail to cover all contingencies.
Whenever we need to perform conditional validation, all we need to do is write a custom JavaScript function and use its name as the value of the tmt:required attribute.
In this example we want to validate a text field only if the associated checkbox is checked. The XHTML code will look like this:
<input type="text" name="newmeal" id="newmeal" tmt:required="dependonbox" tmt:message="Please enter the special meal's name" />
Now we need to write the JavaScript code that will enforce conditional validation, based on whatever custom condition we need to handle. The function should expect the field's DOM node as its only argument and return a boolean:
function dependonbox (fieldNode){
var relatedBox = document.getElementById("specialmeal");
// The field is required only if the box is checked
var isRequired = relatedBox.checked;
if(!isRequired && fieldNode.value == ""){
return false;
}
return true;
}