1 vote

Validation Attributes for Custom Forms

BranchStatus
master-v7
dev-v7

 

Our.Umbraco.DataAnotations

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

This branch is for Umbraco 7. Looking for Umbraco 8?

Installation

During installation the keys will be created nested below DataAnnotions dictionary key.

NuGet:

PM > Install-Package Our.Umbraco.DataAnnotations

Build the project and start website. On first run, a migration will check foreach dictionary key used in the application and added it to umbraco dictionary items. Only default en-US keys and translations are added.

Client Validation

Include the following scripts in your layout .cshtml file

<body>
    @RenderBody()

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.18.0/jquery.validate.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.11/jquery.validate.unobtrusive.min.js"></script>
</body>

or using ClientDependency like so:

@using ClientDendency.Core.Mvc;

...

<body>
    @RenderBody()

    @{
        Html.RequiresJs("https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.18.0/jquery.validate.min.js")
        Html.RequiresJs("https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.11/jquery.validate.unobtrusive.min.js")
    }
    @Html.RenderJsHere()
</body>

The above is just samples, you may use any method you like to include the scripts.

Then on each page with a form enable validation

Html.EnableClientValidation();
Html.EnableUnobtrusiveJavaScript();

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

@inherits UmbracoViewPage<LoginModel>
@using UmbracoBootstrap.Web.Models;
@using UmbracoBootstrap.Web.Controllers;
@{ 
    Html.EnableClientValidation();
    Html.EnableUnobtrusiveJavaScript();
}
@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
  • UmbracoMinLength
  • UmbracoMaxLength
  • UmbracoStringLength
  • UmbracoMustBeTrue
  • UmbracoPassword
  • UmbracoRegularExpression
  • UmbracoRequired

How to use:

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

UmbracoCompareAttribute

 

KeyDefault
EqualToError The '{0}' and '{1}' field fields must match.

 

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

 

KeyDefault
Provied key Must be created by your self.

 

Example:

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

UmbracoEmailAddress

 

KeyDefault
EmailError Doesn't look like a valid e-mail.

 

Example:

[UmbracoRequired]
[UmbracoEmailAddress]
[UmbracoDisplayName(nameof(Email))]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }

UmbracoMinLength

 

KeyDefault
MinLengthError Must not exceed {1} characters.

 

Example:

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

UmbracoMaxLength

 

KeyDefaultDescription
MaxLengthError Must not exceed {1} characters.  

 

Example:

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

UmbracoStringLength

 

KeyDefaultDescription
MaxLengthError Must not exceed {1} characters. Used when only MaximumLength is defined.
MinMaxLengthError Must not be less than {1} and greather than {1} characters. Used when both MaximumLength and MinimumLength is defined.

 

Example:

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

UmbracoMustBeTrue

 

KeyDefault
MustBeTrueError {0} is required.

 

Example:

[UmbracoMustBeTrue]
property boool Consent { get; set; }

UmbracoPassword

 

KeyDefaultDescription
PasswordError Must be at least {1} characters long and contain {2} symbol (!, @, #, etc). Used when only MaximumLength is defined.
MinPasswordLengthError Must be at least {1} characters. Used when only MaximumLength is defined.
MinNonAlphanumericCharactersError Must contain at leat {2} symbol (!, @, #, etc).  
PasswordStrengthError Must be at least {1} characters long and contain {2} symbol (!, @, #, etc).  

 

Example:

[UmbracoPassword]
property string Password { get; set; }

UmbracoRegularExpression

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

Example:

[UmbracoRegularExpression("^([0-9]{4})$", ResourceName="InvalidDKZipCode")]
property string Password { get; set; }

UmbracoRequired

Example:

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

 

 Download package
version 1.2.2

NuGet install instructions for Umbraco 7.4.0-7.13.0

Install-Package Our.Umbraco.DataAnnotations

Package owner

Rasmus Eeg

Rasmus Eeg

Rasmus has 457 karma points

Package Compatibility

This package is compatible with the following versions as reported by community members who have downloaded this package:
Untested or doesn't work on Umbraco Cloud
Version 8.18.x (untested)

You must login before you can report on package compatibility.

Previously reported to work on versions:

Package Information

  • Package owner: Rasmus Eeg
  • Created: 08/04/2019
  • Current version 1.2.2
  • .NET version 4.5
  • License MIT
  • Downloads on Our: 185
  • Downloads on NuGet: 38.1K
  • Total downloads : 38.3K