Skip to content

[ClientValidation]

The [IntelliTect.Coalesce.DataAnnotations.ClientValidation] attribute is used to control the behavior of client-side model validation and to add additional client-only validation parameters. Database validation is available via standard System.ComponentModel.DataAnnotations annotations.

These propagate to the client as validations in TypeScript via generated Metadata and ViewModel rules (for Vue) or Knockout-Validation rules (for Knockout). For both stacks, any failing validation rules prevent saves from going to the server.

WARNING

This attribute controls client-side validation only. To perform server-side validation, create a custom Behaviors class for your types and/or place C# validation attributes on your models. Read More.

Example Usage

c#
public class Person
{
    public int PersonId { get; set; }

    [ClientValidation(IsRequired = true, AllowSave = true)]
    public string FirstName { get; set; }

    [ClientValidation(IsRequired = true, AllowSave = false, MinLength = 1, MaxLength = 100)]
    public string LastName { get; set; }
}

Properties

Behavioral Properties

public bool AllowSave { get; set; } // Knockout Only

If set to true, any client validation errors on the property will not prevent saving on the client. This includes all client-side validation, including null-checking for required foreign keys and other validations that are implicit. This also includes other explicit validation from System.ComponentModel.DataAnnotations annotations.

Instead, validation errors will be treated only as warnings, and will be available through the warnings: KnockoutValidationErrors property on the TypeScript ViewModel.

Note

Use AllowSave = true to allow partially complete data to still be saved, protecting your user from data loss upon navigation while still hinting to them that they are not done filling out data.

public string ErrorMessage { get; set; }

Set an error message to be used if any client validations fail

Validation Rule Properties

In addition to the following properties, you also customize validation on a per-instance basis of the ViewModels using the Rules/Validation methods.

c#
public bool IsRequired { get; set; }
public double MinValue { get; set; } = double.MaxValue;
public double MaxValue { get; set; } = double.MinValue;
public double MinLength { get; set; } = double.MaxValue;
public double MaxLength { get; set; } = double.MinValue;
public string Pattern { get; set; }
public bool IsEmail { get; set; }
public bool IsPhoneUs { get; set; }

Coalesce is a free and open-source framework created by IntelliTect to fill our desire to create better apps, faster. IntelliTect is a high-end software architecture and development consulting firm based in Spokane, Washington.

If you're looking for help with your software project, whether it be a Coalesce application, other technologies, or even just an idea, reach out to us at info@intellitect.com — we'd love to start a conversation! Our clients range from Fortune 100 companies to local small businesses and non-profits.