change your code to:
<Taxonomy:TaxonomyWebTaggingControl runat="server" ID="taxProjectOwnerDepartment" Visible="true" IsMulti="false" IsDisplayPickerButton="true">
</Taxonomy:TaxonomyWebTaggingControl>
<asp:CustomValidator ID="rfvProjectOwnerDepartment" runat="server" OnServerValidate="validator_ServerValidate" />
now in your codebehind add the new method validator_ServerValidate
:
protected void validator_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
string errorMessage = "Please Specfiy Owner's Department";
if (!taxProjectOwnerDepartment.Validate(out errorMessage))
{
args.IsValid = false;
rfvProjectOwnerDepartment.ForeColor = ConsoleColor.Red;
rfvProjectOwnerDepartment.Display = ValidatorDisplay.Dynamic;
rfvProjectOwnerDepartment.ErrorMessage = errorMessage;
}
}
the value of taxonomy is stored in a hidden input field so its not really a input field and throws an error when calling ControlToValidate="taxProjectOwnerDepartment"
unfortunatly
Is it possible to have a required field validator for Taxonomy webtagging control
EDIT
For issues with empty input you need to add:
if(e.Value.Length == 8)
{
e.IsValid = true;
}
else
{
e.IsValid = false;
}
so code will look like this:
protected void validator_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
string errorMessage = "Please Specfiy Owner's Department";
if(e.Value.Length == 0)
{
args.IsValid = false;
rfvProjectOwnerDepartment.ForeColor = ConsoleColor.Red;
rfvProjectOwnerDepartment.Display = ValidatorDisplay.Dynamic;
rfvProjectOwnerDepartment.ErrorMessage = errorMessage;
}
else
{
if (!taxProjectOwnerDepartment.Validate(out errorMessage))
{
args.IsValid = false;
rfvProjectOwnerDepartment.ForeColor = ConsoleColor.Red;
rfvProjectOwnerDepartment.Display = ValidatorDisplay.Dynamic;
rfvProjectOwnerDepartment.ErrorMessage = errorMessage;
}
}
}
so now we have changed the codebehind to first check if the length is correct (not 0) if its isnt 0 then do check what was entered to validate! code is a bit bulky, you could change it so that the if function is in one checking with an or statment like this:
protected void validator_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
string errorMessage = "Please Specfiy Owner's Department";
if (e.Value.Length == 0 || !taxProjectOwnerDepartment.Validate(out errorMessage))
{
args.IsValid = false;
rfvProjectOwnerDepartment.ForeColor = ConsoleColor.Red;
rfvProjectOwnerDepartment.Display = ValidatorDisplay.Dynamic;
rfvProjectOwnerDepartment.ErrorMessage = errorMessage;
}
}