TKDataForm supports three validation mdoes:
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
:
self.dataForm.validationMode = TKDataFormValidationModeImmediate;
self.dataForm.validationMode = TKDataFormValidationMode.immediate
this.DataForm.ValidationMode = TKDataFormValidationMode.Immediate;
There are 2 options to validate a property - using TKDataFormDelegate or using validators that adopt TKDataFormValidator protocol.
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 };
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)
{
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();