DataForm for Xamarin.iOS: Validation
Validation Modes
TKDataForm supports three validation modes:
- 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 when
Commit
method ofTKDataForm
is called. This option is used only with commit modeTKDataFormCommitModeDelayed
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. ValidationMessage
method should return a feedback message that will be displayed to the user of your application. After you implement a validator you should set the Validators
property of the TKDataFormEntityProperty
that will be validated.
In addition, you can take advantage of a few predefined validators, for example TKDataFormEmailValidator
, TKDataFormMaximumLengthValidator
, TKDataFormNonEmptyValidator
, TKDataFormPhoneValidator
and TKDataFormRangeValidator
.
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 ValidateProperty
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 its HasValidationErrors
method.
var hasValidationErrors = dataForm.HasValidationErrors();