Our.Umbraco.ValidationAttributes 1.1.0

dotnet add package Our.Umbraco.ValidationAttributes --version 1.1.0
NuGet\Install-Package Our.Umbraco.ValidationAttributes -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Our.Umbraco.ValidationAttributes" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Our.Umbraco.ValidationAttributes --version 1.1.0
#r "nuget: Our.Umbraco.ValidationAttributes, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Our.Umbraco.ValidationAttributes as a Cake Addin
#addin nuget:?package=Our.Umbraco.ValidationAttributes&version=1.1.0

// Install Our.Umbraco.ValidationAttributes as a Cake Tool
#tool nuget:?package=Our.Umbraco.ValidationAttributes&version=1.1.0

Our.Umbraco.ValidationAttributes

Contains model validation attributes to for your properties, by using umbraco dictionary as the resource for error messages.

This branch is exclusively for Umbraco 9. This project is a port for Umbraco 9, taken from the original Our.Umbraco.DataAnnotations by rasmuseeg.

Looking for Umbraco 8?
Looking for Umbraco 7?

Installation

dotnet add package Our.Umbraco.ValidationAttributes

Build the project and start website.

Client Validation

Include the following scripts in your layout.cshtml file, or in your master page:

<body>
    @RenderBody()

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" referrerpolicy="no-referrer"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js" integrity="sha512-37T7leoNS06R80c8Ulq7cdCDU5MNQBwlYoy1TX/WUsLFC2eYNqtKlV0QjH7r8JpG/S0GUMZwebnVFLPd6SU5yg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.12/jquery.validate.unobtrusive.min.js" referrerpolicy="no-referrer"></script>

    <script src="~\App_Plugins\Our.Umbraco.ValidationAttributes\Scripts\jquery.validation.custom.js">
</body>

The above is just a sample, you may use any method you like to include the scripts.
NOTE: jquery.validation.custom.js is required to ensure that UmbracoIFormFileExtensions, UmbracoMaxFileSize and UmbracoMustBeTrue attributes are working correctly.
As an alternative you can include yourself its content with any method you like.

The end result for a page with validation could look like:

@model LoginModel
@using MyWebsite.Web.Models;
@using MyWebsite.Web.Controllers;
@using (Html.BeginUmbracoForm<MemberController>("HandleLogin", null, new { @role="form", @class="" }, FormMethod.Post))
{
    @Html.ValidationSummary("loginModel", true)

    <div class="form-group">
        @Html.LabelFor(m=> m.Username, new { @class="control-label" })
        @Html.TextBoxFor(m => m.Username, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Username)
    </div>

    <div class="form-group">
        @Html.LabelFor(m=> m.Password, new { @class="control-label" })
        @Html.PasswordFor(m => m.Password, new {
            @class = "form-control form-control-appended",
            @placeholder = Umbraco.GetDictionaryValue("EnterYourPassword", "Enter your password")
        })
        @Html.ValidationMessageFor(m => m.Password)
    </div>

    @Html.HiddenFor(m=> m.RedirectUrl)

    <button type="submit" role="button">@Umbraco.GetDictionaryValue("SignIn", "Sign in")</button>
}

Attributes

Decorate your properties with the following attributes

  • UmbracoCompare
  • UmbracoDisplayName
  • UmbracoEmailAddress
  • UmbracoIFormFileExtensions
  • UmbracoMaxFileSize
  • UmbracoMaxLength
  • UmbracoMinLength
  • UmbracoMustBeTrue
  • UmbracoRange
  • UmbracoRegularExpression
  • UmbracoRemote
  • UmbracoRequired
  • UmbracoStringLength

How to use:

[UmbracoRequired]
public string MyProperty { get; set; } 

UmbracoCompare

Umbraco Dictionary Key Default
EqualToError Must be created by your self.

Example:

[UmbracoDisplayName(nameof(Password))]
[DataType(DataType.Password)]
public string Password { get; set; }

[UmbracoDisplayName(nameof(ConfirmPassword))]
[UmbracoRequired]
[UmbracoCompare(nameof(Password))]
[DataType(DataType.Password)]
public string ConfirmPassword { get; set; }

UmbracoDisplayName

Key Default
Provied key Must be created by yourself.

Example:

[UmbracoDisplayName(nameof(Username))]
[UmbracoRequired]
public string Username { get; set; }

UmbracoEmailAddress

Key Default
EmailError Must be created by yourself.

Example:

[UmbracoEmailAddress]
public string Email { get; set; }

UmbracoIFormFileExtensions

Key Default
FormFileExtensionsError Must be created by yourself.

Example:

[UmbracoIFormFileExtensions("jpeg,png,jpg")] // List of comma-separated file extensions
public IFormFile UmbracoIFormFileExtensions { get; set; }

UmbracoMaxFileSize

Key Default
MaxFileSizeError Must be created by yourself.

Example:

[UmbracoMaxFileSize(5 * 1024 * 1024)] // Max size in bytes
public IFormFile UmbracoMaxFileSize { get; set; }

UmbracoMinLength

Key Default
MinLengthError Must be created by yourself.

Example:

[UmbracoMinLength(20)]
public string MyProperty { get; set; }

UmbracoMaxLength

Key Default
MaxLengthError Must be created by yourself.

Example:

[UmbracoMaxLength(120)]
public string MyProperty { get; set; }

UmbracoStringLength

Key Default
MinMaxLengthError Must be created by yourself.

Examples:

[UmbracoStringLength(120)]
public string Message { get; set; }

[UmbracoStringLength(120, MinimumLength = 30)]
public string Message { get; set; }

UmbracoMustBeTrue

Key Default
MustBeTrueError Must be created by yourself.

Example:

[UmbracoMustBeTrue]
public bool Consent { get; set; }

UmbracoRegularExpression

There are no default keys for this attribute, since each regex validation is unique.

Example:

[UmbracoRegularExpression("^([0-9]{4})$", DictionaryKey = "MyCustomKey")]
public string Password { get; set; }

UmbracoRequired

Example:

[UmbracoRequired]
public string MyProperty { get; set; }

Custom dictionary keys

Each Attribute has a public property DictionaryKey which can be set like this:

[UmbracoRequired(DictionaryKey = "MyCustomKey")]
public string MyProperty { get; set; }

Not setting a custom key, will fallback to the default dictionary key.
You have to create Dictionary Keys manually, as explained in this documentation.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 22,529 1/24/2022
1.0.2 948 10/22/2021
1.0.1 1,115 10/22/2021
1.0.0 1,054 10/22/2021