New to Telerik UI for Xamarin? Download free 30-day trial

Validation Annotations

Validators are special type of attributes that can be used to ensure that the users' input falls within an expected range of values. Validators should implement the IPropertyValidator interface defining the following members:

  • PositiveFeedback (string): The message that is displayed in the editor if the property value is valid.
  • NegativeFeedback (string): The message that is displayed in the editor if the property value is invalid.
  • bool Validate (object value): A method that decides whether the property value is valid or not.

The following list contains the available implementations that we provide and can be used out of the box:

  • DateRangeValidatorAttribute: Validates if a DateTime value falls within a given interval. Here are the validator properties:
    • MinYear (int)
    • MinMonth (int)
    • MinDay (int)
    • MaxYear (int)
    • MaxMonth (int)
    • MaxDay (int)
  • EmailValidatorAttribute: Validates if a string matches the e-mail format [string]@[string].[string].
  • NonEmptyValidatorAttribute: Validates if an editor has non null value.
  • NumericalRangeValidatorAttribute: Validates if a number falls within a given interval. Here are the validator properties:

    • Min (double)
    • Max (double)
    • Step (double)

    This attribute sets the Minimum, Maximum and Step properties of the created editor (e.g. RadSlider).

  • PhoneNumberValidatorAttribute: Validates if a string matches the phone number requirements.
  • RegularExpressionValidatorAttribute: Validates if a string matches a custom regex pattern.
    • Pattern (string)
    • Options (RegexOptions)
  • StringLengthValidatorAttribute: Validates if a string has specific length.
    • MinLength (int)
    • MaxLength (int)

Users can also define their custom validators. The only requirement is to implement the IPropertyValidator interface or to inherit from the base implementation - the ValidatorBaseAttribute class.

Example

This example will demonstrate how to create a custom validator and also how to use the default ones.

Here is a sample class that has an "Occupation" property:

public enum Occupation { Unspecified, Unemployed, Employed }

public class Person
{
    [NumericalRangeValidator(10, 15, "Age must be 10 and 15 years!")]
    public int Age { get; set; }

    [OccupationValidator]
    public Occupation Occupation { get; set; } = Occupation.Unspecified;

    [NonEmptyValidator("Name is required!")]
    public string Name { get; set; }
}

Let's create a validator that ensures that the person occupation is not "Unspecified".

public class OccupationValidatorAttribute : ValidatorBaseAttribute
{
    public OccupationValidatorAttribute()
    {
        this.NegativeFeedback = "Occupation must be specified";
    }

    protected override bool ValidateCore(object value)
    {
        return (Occupation)value != Occupation.Unspecified;
    }
}

And here is the data form setup:

dataForm.Source = new Person();
dataForm.RegisterEditor("Occupation", EditorType.SegmentedEditor);

See Also

In this article