An open API service indexing awesome lists of open source software.

https://github.com/ZioTino/Our.Umbraco.ValidationAttributes

Contains validation attributes to decorate your classes, but using Umbraco dictionary as the resource.
https://github.com/ZioTino/Our.Umbraco.ValidationAttributes

umbraco umbraco-cms umbraco-package umbraco-v9

Last synced: about 1 month ago
JSON representation

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

Awesome Lists containing this project

README

        

# 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](http://github.com/rasmuseeg/Our.Umbraco.DataAnnotations) by [rasmuseeg](https://github.com/rasmuseeg).

[Looking for Umbraco 8?](https://github.com/rasmuseeg/Our.Umbraco.DataAnnotations/tree/dev-v8)
[Looking for Umbraco 7?](https://github.com/rasmuseeg/Our.Umbraco.DataAnnotations/tree/dev-v7)

## 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:

```

@RenderBody()




</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:
```cshtml
@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:**
```C#
[UmbracoRequired]
public string MyProperty { get; set; }
```

### UmbracoCompare

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

Example:
```C#
[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:
```C#
[UmbracoDisplayName(nameof(Username))]
[UmbracoRequired]
public string Username { get; set; }
```

### UmbracoEmailAddress

| Key | Default |
| -- | -- |
| EmailError | Must be created by yourself. |

Example:
```C#
[UmbracoEmailAddress]
public string Email { get; set; }
```

### UmbracoIFormFileExtensions

| Key | Default |
| -- | -- |
| FormFileExtensionsError | Must be created by yourself. |

Example:
```C#
[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:
```C#
[UmbracoMaxFileSize(5 * 1024 * 1024)] // Max size in bytes
public IFormFile UmbracoMaxFileSize { get; set; }
```

### UmbracoMinLength

| Key | Default |
| -- | -- |
| MinLengthError | Must be created by yourself. |

Example:
```C#
[UmbracoMinLength(20)]
public string MyProperty { get; set; }
```

### UmbracoMaxLength

| Key | Default |
| -- | -- |
| MaxLengthError | Must be created by yourself. |

Example:
```C#
[UmbracoMaxLength(120)]
public string MyProperty { get; set; }
```

### UmbracoStringLength

| Key | Default
| -- | -- |
| MinMaxLengthError | Must be created by yourself. |

Examples:
```C#
[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:
```C#
[UmbracoMustBeTrue]
public bool Consent { get; set; }
```

### UmbracoRegularExpression

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

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

### UmbracoRequired

Example:
```C#
[UmbracoRequired]
public string MyProperty { get; set; }
```

## Custom dictionary keys
Each Attribute has a public property `DictionaryKey` which can be set like this:
```C#
[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.**