LocalizableValidationRole class

An abstract class representing a base localizable validation rule.

Inherits from

Neurodot.Forms.Silverlight.Controls. LocalizableValidationRole

 

Syntax

public abstract class LocalizableValidationRole : ValidationRole

Properties

Name Description
ResStringName Gets or sets the name of a custom localized string to use as a validation error message.
ValidationError Gets or sets the validation error message.

 

Methods

Name Description
Validate(object) Validates assigned control and raises or hides validation error on the control.

 

Remarks

All existing custom validation classes (e.g. CheckBoxValidationRole, ComboBoxValidationRole etc.) inherit from this class. If need be, write a custom validation class inheriting this one.

Example

Here is an example of how to write custom validation class. The class validates group of check boxes whether at enough of them is checked – in this case at least two must be check.

public static class Clas01
{
	public static void OnFormLoaded(object sender, RoutedEventArgs e)
	{
		CheckBox[] cboxes = new CheckBox[3];
		cboxes[0] = ThisForm.FindElement<CheckBox>("CheckBox1");
		cboxes[1] = ThisForm.FindElement<CheckBox>("CheckBox2");
		cboxes[2] = ThisForm.FindElement<CheckBox>("CheckBox3");

		for (int i = 0; i < cboxes.Length; i++)
		{
			ThisForm.RegisterValidationRole(cboxes[i], new CheckBoxGroupValidationRole(){
				ResStringName = "$CheckBoxGroupValidationErr",
				CheckBoxes = cboxes, MinCheckedBoxes = 2
			});
		}
	}
}

public class CheckBoxGroupValidationRole : LocalizableValidationRole
{
	/// <summary>
	/// Gets or sets an array of checkboxes in the validated group.
	/// </summary>
	public CheckBox[] CheckBoxes { get; set; }

	/// <summary>
	/// Gets or sets a value of how many checkboxes must be checked.
	/// </summary>
	public int MinCheckedBoxes { get; set; }


	public override void Validate(object control)
	{
		CheckBox chk = control as CheckBox;
		if (chk != null)
		{
			int countChecked = 0;
			foreach (CheckBox chkbox in this.CheckBoxes)
			{
				chkbox.ClearValidationError();
				if (chkbox.IsChecked == true)
					countChecked++;
			}

			if (countChecked < this.MinCheckedBoxes)
			{
				foreach (CheckBox chkbox in this.CheckBoxes)
				{
					if (chkbox.Visibility == Visibility.Visible)
					{
						chkbox.SetValidation(this.ValidationError);
						chkbox.RaiseValidationError();
					}
				}
				// always call base version of the method!
				base.Validate(control);
			}
		}
		else
		{
			throw new NotSupportedException("Validation error. Not supported control.");
		}
	}
}