TKDataForm: Validation

Validation Modes

TKDataForm supports three validation mdoes:

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

self.dataForm.validationMode = TKDataFormValidationModeImmediate;
self.dataForm.validationMode = TKDataFormValidationMode.immediate
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 = [[TKDataFormMinimumLengthValidator alloc] initWithMinimumLength:6];
passwordValidator.errorMessage = @"Password must be at least 6 characters!";
_dataSource[@"password"].validators = @[passwordValidator];
let passwordValidator:TKDataFormMinimumLengthValidator = TKDataFormMinimumLengthValidator(minimumLength: 6)
passwordValidator.errorMessage = "Password must be at least 6 characters!";
dataSource["password"].validators = [passwordValidator]
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:

- (BOOL)dataForm:(TKDataForm *)dataForm validateProperty:(TKEntityProperty *)property editor:(TKDataFormEditor *)editor
override func dataForm(_ dataForm: TKDataForm, validate property: TKEntityProperty, editor: TKDataFormEditor) -> Bool {
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:

BOOL hasValidationError = [self.dataForm hasValidationErrors];
let hasValidationError = self.dataForm.hasValidationErrors();