Skip to content

Commit

Permalink
Default static message allowed to be modified. README updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwaliszko committed May 22, 2016
1 parent 831aafc commit ea208dc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,4 @@ UpgradeLog*.XML

# Linked content script files (copied automatically before each build)
/src/ExpressiveAnnotations.MvcWebSample/Scripts/expressive.annotations.validate*.js
/src/.vs/config/applicationhost.config
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ A small .NET and JavaScript library which provides annotation-based conditional
- [How to control frequency of dependent fields validation?](#how-to-control-frequency-of-dependent-fields-validation) <sup>(re client-side)</sup>
- [Can I increase web console verbosity for debug purposes?](#can-i-increase-web-console-verbosity-for-debug-purposes) <sup>(re client-side)</sup>
- [How to fetch field value or display name in error message?](#how-to-fetch-field-value-or-display-name-in-error-message) <sup>(re client and server-side)</sup>
- [Is there any event raised when validation is done?](#is-there-any-event-raised-when-validation-is-done) <sup>(re client-side)</sup>
- [`RequiredIf` attribute is not working, what is wrong?](#requiredif-attribute-is-not-working-what-is-wrong) <sup>(re client and server-side)</sup>
- [Is there a possibility to perform asynchronous validation?](#is-there-a-possibility-to-perform-asynchronous-validation) <sup>(re client-side, experimental)</sup>
- [What if my question is not covered by FAQ section?](#what-if-my-question-is-not-covered-by-faq-section)
Expand Down Expand Up @@ -680,6 +681,22 @@ If you need more insightful overview of what client-side script is doing (includ

Notice that `{{` is treated as the escaped bracket character.

#####<a id="#is-there-any-event-raised-when-validation-is-done">Is there any event raised when validation is done?</a>

Each element validated by EA triggers an `eavalid` event, with the following extra parameters:

* type of the attribute for which validation was executed: `'requiredif'` or `'assertthat'`,
* state of the validation: `true` or `false`,
* expression which was evaluated.

Attach to it in the following manner:
```JavaScript
<script>
$('form').find('input, select, textarea').on('eavalid', function(e, type, valid, expr) {
console.log('event triggered by ' + e.currentTarget.name);
});
```

#####<a id="#requiredif-attribute-is-not-working-what-is-wrong">`RequiredIf` attribute is not working, what is wrong?</a>

Make sure `RequiredIf` is applied to a field which *accepts null values*.
Expand Down
19 changes: 17 additions & 2 deletions src/ExpressiveAnnotations/Attributes/AssertThatAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright (c) 2014 Jarosław Waliszko
* Licensed MIT: http://opensource.org/licenses/MIT */

using System;
using System.ComponentModel.DataAnnotations;

namespace ExpressiveAnnotations.Attributes
Expand All @@ -12,14 +13,28 @@ namespace ExpressiveAnnotations.Attributes
/// </summary>
public sealed class AssertThatAttribute : ExpressiveAttribute
{
private const string _defaultErrorMessage = "Assertion for {0} field is not satisfied by the following logic: {1}";
private static string _defaultErrorMessage = "Assertion for {0} field is not satisfied by the following logic: {1}";

/// <summary>
/// Gets or sets the default error message.
/// </summary>
public static string DefaultErrorMessage
{
get { return _defaultErrorMessage; }
set
{
if (value == null)
throw new ArgumentNullException(nameof(value), "Default error message cannot be null.");
_defaultErrorMessage = value;
}
}

/// <summary>
/// Initializes a new instance of the <see cref="AssertThatAttribute" /> class.
/// </summary>
/// <param name="expression">The logical expression based on which assertion condition is computed.</param>
public AssertThatAttribute(string expression)
: base(expression, _defaultErrorMessage)
: base(expression, DefaultErrorMessage)
{
}

Expand Down
22 changes: 18 additions & 4 deletions src/ExpressiveAnnotations/Attributes/RequiredIfAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,28 @@ namespace ExpressiveAnnotations.Attributes
/// </summary>
public sealed class RequiredIfAttribute : ExpressiveAttribute
{
private const string _defaultErrorMessage = "The {0} field is required by the following logic: {1}";
private static string _defaultErrorMessage = "The {0} field is required by the following logic: {1}";

/// <summary>
/// Gets or sets the default error message.
/// </summary>
public static string DefaultErrorMessage
{
get { return _defaultErrorMessage; }
set
{
if (value == null)
throw new ArgumentNullException(nameof(value), "Default error message cannot be null.");
_defaultErrorMessage = value;
}
}

/// <summary>
/// Initializes a new instance of the <see cref="RequiredIfAttribute" /> class.
/// </summary>
/// <param name="expression">The logical expression based on which requirement condition is computed.</param>
public RequiredIfAttribute(string expression)
: base(expression, _defaultErrorMessage)
: base(expression, DefaultErrorMessage)
{
AllowEmptyStrings = false;
}
Expand All @@ -45,7 +59,7 @@ public RequiredIfAttribute(string expression)
/// <exception cref="System.InvalidOperationException"></exception>
protected override ValidationResult IsValidInternal(object value, ValidationContext validationContext)
{
AssertNonValueType(value, validationContext);
AssertNonValueType(value);

var isEmpty = value is string && string.IsNullOrWhiteSpace((string) value);
if (value == null || (isEmpty && !AllowEmptyStrings))
Expand All @@ -60,7 +74,7 @@ protected override ValidationResult IsValidInternal(object value, ValidationCont
return ValidationResult.Success;
}

private void AssertNonValueType(object value, ValidationContext validationContext)
private void AssertNonValueType(object value)
{
if (PropertyType == null)
return;
Expand Down

0 comments on commit ea208dc

Please sign in to comment.