TKDataForm: Validation

Validation Modes

TKDataForm supports three validation mdoes:

  • Immediate - validation will be performed every time the property value is changed.
  • OnLostFocus - validation will be performed when the editor focus is changed to another editor.
  • Delayed - validation will be performed explicitly whe commit method of TKDataForm is called. Note that this option is used only with commit mode TKDataFormCommitModeDelayed

Here is an example how to set a validation mode to TKDataForm:

this.DataForm.ValidationMode = TKDataFormValidationMode.Immediate;

Validating TKDataFormEntityProperty

There are 2 options to validate a property - using TKDataFormDelegate or using validators that adopt TKDataFormValidator protocol.

Adopting TKDataFormValidator

TKDataFormValidator protocol has 2 required methods - validateProperty: and validationMessage. validateProperty method is used to perform the actual validation and return a boolean value indicating if the property value is valid. The validationMessage method should return a feedback message that will be displayed to the user of your application. After you implement a vlidator you should set the validators property of the TKDataFormEntityProperty that will be validated:

TKDataFormMinimumLengthValidator passwordValidator = new TKDataFormMinimumLengthValidator (6);
passwordValidator.ErrorMessage = "Password must be at least 6 characters!";
password.Validators = new NSObject[] { passwordValidator };

Validating through TKDataFormDelegate

To validate a property through TKDataFormDelegate you should implement its dataForm:validateProperty:editor: that returns a boolean value indicating if the property value is valid:

public override bool ValidateProperty (TKDataForm dataForm, TKEntityProperty property, TKDataFormEditor editor)
{

Check for validation errors

When you want to simply check if there are any validation errors in the TKDataForm you can use the hasValidationErrors method. The next code snippet shows how to call the method from the TKDataFormViewController: