Giter Site home page Giter Site logo

rasmuseeg / our.umbraco.dataannotations Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 2.0 3.59 MB

Contains validation attributes to decorate your classes, but using umbraco dictionary as the resource.

License: Other

C# 50.96% JavaScript 13.09% HTML 23.75% PowerShell 0.98% ASP.NET 11.22%
validation jquery-validation cldr-data attributes umbraco-cms umbraco-v7 umbraco-packages umbraco-dictionary

our.umbraco.dataannotations's Introduction

Branch Status
master-v7 Build status
dev-v7 Build status

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

Key Default
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

Key Default
Provied key Must be created by your self.

Example:

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

UmbracoEmailAddress

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

Example:

[UmbracoEmailAddress(DictionaryKey = "MyCustomKey")]
public string Email { get; set; }

UmbracoMinLength

Key Default
MinLengthError Must not exceed {1} characters.

Example:

[UmbracoMinLength(20, DictionaryKey = "MyCustomKey")]
property string MyProperty { get; set; }

UmbracoMaxLength

| Key | Default | Description | | -- | -- | | MaxLengthError | Must not exceed {1} characters.

Example:

[UmbracoMaxLength(120, DictionaryKey = "MyCustomKey")]
property string MyProperty { get; set; }

UmbracoStringLength

Key Default Description
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, DictionaryKey = "MyCustomKey")]
property string Message { get; set; }

UmbracoMustBeTrue

| Key | Default | | -- | -- | -- | | MustBeTrueError | {0} is required. |

Example:

[UmbracoMustBeTrue(DictionaryKey = "MyCustomKey")]
property boool Consent { get; set; }

UmbracoPassword

Key Default Description
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(DictionaryKey = "CustomPasswordKey", 
    MinPasswordLengthDictionaryKey = "CustomMinPasswordLengthKey", 
    MinNonAlphanumericCharactersDictionaryKey = "MyCustomMinNonAlphanumericCharactersKey", 
    PasswordStrengthDictionaryKey = "MyCustomPasswordStrengtKey",
    PasswordStrengthRegexTimeout = 360)]
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})$", DictionaryKey = "MyCustomRegexKey")]
property string Password { get; set; }

UmbracoRequired

Example:

[UmbracoRequired(DictionaryKey = "MyCustomRequiredKey")]
property string MyProperty { get; set; }

Custom dictionary keys

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

[UmbracoReguired(DictionaryKey = "MyCustomKey")]
[UmbracoRegularExpression(DictionaryKey = "MyCustomRegexKey")]
[UmbracoRegularExpression(DictionaryKey = "MyCustomRegexKey")]
property string MyProperty { get; set; }

Not setting a custom key, will fallback to the default dictionary key.

our.umbraco.dataannotations's People

Contributors

giannidpc avatar rasmuseeg avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

our.umbraco.dataannotations's Issues

UmbracoRegularExpression not working as expected

If I use the "UmbracoRegularExpression" data annotation attribute it always defaults to the default MVC error for this validation, even when I use the ResourceName property.

The expected outcome would be for the validationmessage to display the value that has been set on the key in the Umbraco dictionary.

Other annotations like UmbracoRequired and UmbracoDisplayName work fine even with the ResourceName property set.

Example:

        [UmbracoRequired]
        [UmbracoRegularExpression(@"^(?:\+\d{2,15})*", ResourceName = "test")]
        public string Phone { get; set; }

Did I forget an extra step to get this working or is this a bug? I'm using Umbraco version 7.13.2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.