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; }

 

Package owner

Rasmus Eeg

Rasmus Eeg

Rasmus has 445 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
8.2.x (untested)
8.1.x (untested)
8.0.x (untested)
7.15.x (untested)
7.14.x (untested)
7.13.x (untested)
7.12.x (untested)
7.11.x (untested)
7.10.x (untested)
7.9.x (untested)
7.8.x (untested)
7.7.x (untested)
7.6.x (untested)
7.5.x (untested)
7.4.x (untested)
7.3.x (untested)
7.2.x (untested)
7.1.x (untested)
7.0.x (untested)
6.1.x (untested)
6.0.x (untested)

You must login before you can report on package compatibility.

Package Information

  • Package owner: Rasmus Eeg
  • Created: 08/04/2019
  • Current version 1.2.2
  • .net Version 4.5
  • License MIT
  • Downloads: 49