MVC - Change Compare attribute error message - asp.net-mvc

In my MVC application I have the ability to get the error message from a text file instead of using the default error message. This works perfectly on the Required attribute (both Serverside and Clientside).
I now need to do the same with the Compare attribute, but I can't figure out how to override the Compare attribute.
For reference, this is how I am doing it with the Required attribute (I would like similar code to this to work with the Compare attribute)...
Public Class RequiredFieldAttribute
Inherits ValidationAttribute
Implements IClientValidatable
Private innerAttribute As New RequiredAttribute()
Private errormessagecontrolid As String
Public Sub New(ErrorMessageControlID As String)
Me.errormessagecontrolid = ErrorMessageControlID
End Sub
Protected Overrides Function IsValid(value As Object, validationContext As ValidationContext) As ValidationResult
If Not innerAttribute.IsValid(value) Then
Return New ValidationResult(ErrorMsgs.Text(Me.errormessagecontrolid))
End If
Return ValidationResult.Success
End Function
Public Function GetClientValidationRules(metadata As ModelMetadata, context As ControllerContext) As IEnumerable(Of ModelClientValidationRule) Implements IClientValidatable.GetClientValidationRules
Dim result = New List(Of ModelClientValidationRule)
Dim rule = New ModelClientValidationRule() With {.ErrorMessage = ErrorMsgs.Text(Me.errormessagecontrolid), .ValidationType = "required"}
result.Add(rule)
Return result
End Function
End Class
Above, ErrorMsgs.Text is the function that retieves the message from the text file. Against my model I then apply something like this...
<RequiredField("AccountDetailsPostcodeError")>
Public Property PostCode As String
The system then looks in the Text file for an entry called AccountDetailsPostcodeError.
How can I achieve the same with the Compare attribute. At the moment I have a hard coded error message like this...
<Compare("WebPassword", ErrorMessage:="The password and confirmation do not match.")>
Public Property ConfirmWebPassword As String
Edit: The suggested fix below may work in C#, but won't work in VB.NET, hence my more complex requirement to override the Compare attribute. I just don't know how to correctly override it.

Why use a text file for your translations and messages, .NET has build in options for translations. You can use resources. The advantage of using resources is that resources are type safe, they are checked as compile time. Where your textfile can become corrupt / missing.
The following guide helps you with setting up resources in a Mvc project:
Step one
Edit the default assembly language:
(C#) Properties > Assembly information > Neutral Language
(VB) My Project > Assembly information > Neutral Language
Set this language to your default language. (For this example I use English (United States))
Step two
Add a resource file to your project. Call this file Resource.resx. Open this file. Change the Access Modifier to Public and start adding resource strings. For example:
Step three
Add for each other language you want to support another resource file but name them Resource.LANGUAGE.resx where LANGUAGE is replaced by the other culture name. For culture names you can check this url: http://msdn.microsoft.com/en-us/goglobal/bb896001.aspx
Then fill the new resource file with the localized strings. For example:
Step four
Then you can in your Models use the default localization support of the attributes:
For example:
VB:
Imports System.ComponentModel.DataAnnotations
Public Class UserModel
<Display(Name:="UserNameField", ResourceType:=GetType(My.Resources.Resource))>
<Required(AllowEmptyStrings:=False, ErrorMessageResourceName:="RequiredUsername", ErrorMessageResourceType:=GetType(My.Resources.Resource))>
Public Property UserName As String
<Display(Name:="PasswordField", ResourceType:=GetType(My.Resources.Resource))>
<MinLength(6, ErrorMessageResourceName:="PasswordLengthError", ErrorMessageResourceType:=GetType(My.Resources.Resource))>
<Compare("PasswordAgain", ErrorMessageResourceName:="CompareError", ErrorMessageResourceType:=GetType(My.Resources.Resource))>
<Required(AllowEmptyStrings:=False, ErrorMessageResourceName:="RequiredPassword", ErrorMessageResourceType:=GetType(My.Resources.Resource))>
Public Property Password As String
<Display(Name:="PasswordAgainField", ResourceType:=GetType(My.Resources.Resource))>
<Required(AllowEmptyStrings:=False, ErrorMessageResourceName:="RequiredPasswordAgain", ErrorMessageResourceType:=GetType(My.Resources.Resource))>
Public Property PasswordAgain As String
End Class
C#
using System.ComponentModel.DataAnnotations;
public class UserModel
{
[Display(Name = "UserNameField", ResourceType = typeof(My.Resources.Resource))]
[Required(AllowEmptyStrings = False, ErrorMessageResourceName = "RequiredUsername", ErrorMessageResourceType = typeof(My.Resources.Resource))]
public string UserName;
[Display(Name = "PasswordField", ResourceType = typeof(My.Resources.Resource))]
[MinLength(6, ErrorMessageResourceName = "PasswordLengthError", ErrorMessageResourceType = typeof(My.Resources.Resource))]
[Compare("PasswordAgain", ErrorMessageResourceName = "CompareError", ErrorMessageResourceType = typeof(My.Resources.Resource))]
[Required(AllowEmptyStrings = False, ErrorMessageResourceName = "RequiredPassword", ErrorMessageResourceType = typeof(My.Resources.Resource))]
public string Password;
[Display(Name = "PasswordAgainField", ResourceType = typeof(My.Resources.Resource))]
[Required(AllowEmptyStrings = False, ErrorMessageResourceName = "RequiredPasswordAgain", ErrorMessageResourceType = typeof(My.Resources.Resource))]
public string PasswordAgain;
}
For localization the attribute needs to know the name of the static property and the type of the static class where to get the property from (as seen above).
Step five
Then in your view use the #Html.ValidationSummary() to get all the error messages, or use
VB #Html.ValidationMessageFor(Function(model) model.Property)
C# #Html.ValidationMessageFor(m => m.Property)
to get specific error messages.
For the display attribute you can use:
VB #Html.DisplayNameFor(Function(model) model.Property)
C# #Html.DisplayNameFor(m => m.Property)
Changing the language
And last but not least you can change the language of your app instead of your neutral language defined in step one by editing the Web.config and changing the globalization tag like so:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<globalization uiCulture="nl" />
</system.web>
</configuration>
If you want to change the language from code you should edit System.Threading.Thread.CurrentThread.CurrentUICulture for information about this I suggest google or another SO question.
Example project
For this question I quickly made an example project to provide an accurate answer. Project can be found here:
MvcVBTest.V1.zip
UPDATE
If you don't want to use Resources but a single text file you can use the same concept the resource framework uses. You need a class that has static properties you can reference.
For this purpose I did the following things:
I created a class called Resources (Resources.vb).
In this class I added a sub class called Resource
In the static constructor of this class I open resource.xml which I have mapped to an array of Resource
This array is then converted to an Dictionary(Of String, String)
I created an static get property for every item in the xml. And returned the right item from the Dictionary
I changed the ResourceType parameter in the UserModel class
And of course a little clean up. The old resources can be deleted and the globalization tag can be removed from the web.config.
Now all the text can be found in resource.xml as key value pairs. To add another line, add it to the XML and create a property for it in the Resource class.
Example project
For this update I updated my example project:
MvcVBTest.V2.zip

Why not use something like this?
<RequiredField(ErrorMessage=GetErrorMessage())>
Just create a static function that gets the error message for you. You can even take a parameter for your GetErrorMessage function so you can determine which message you'll want to return.

Related

F# reading custom configuration and representing it as ConfigurationSection with ConfigurationElements

I am rather new to F# but I have a question about creating and reading custom configuration file. I know how it would look in c# so for example I have a simple config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="customSection" type="Tool.Lib.Config, Tool.Lib" />
</configSections>
<communicationSection>
<responseTimeoutInMs value="900000" />
</communicationSection>
</configuration>
Basing on that in c# it is simple. I am creating model named Config with properties marked as ConfigurationProperty (witch relates to xml node name, in this case responseTimeoutInMs) something like:
[ConfigurationProperty("responseTimeoutInMs")]
public ResponseTimeoutConfigElement ResponseTimeoutInMs
{
get => (ResponseTimeoutConfigElement)base["responseTimeoutInMs"];
set => base["responseTimeoutInMs"] = value;
}
And of course value is set as ConfigurationElement so:
public class ResponseTimeoutConfigElement : ConfigurationElement
{
[ConfigurationProperty("value", IsRequired = true, IsKey = true, DefaultValue = 0)]
public int Value => (int)base["value"];
}
It is a nice mechanism, I can pin converters into it and create types I need while reading configuration.
I know i can read default config using ConfigurationManager and exe configuration map, but this is basic config reading using key and value.
So my question is, is there something similar in F# to this in C#?
I'm not sure if it's quite what you're after as you mention using a "custom configuration file", but in the past I've used the AppSettings Type Provider to get strongly typed access to app variables.
If that's not appropriate, there's also a more standard XML type provider that might help?
I find type providers really useful in avoiding having to write boilerplate code for simple access, and they're a really nice feature of F# development.
You can do pretty much the same thing in F# as in C#:
type ResponseTimeoutConfigElement() =
inherit ConfigurationElement()
[<ConfigurationProperty("value", IsRequired = true, IsKey = true, DefaultValue = 0)>]
member this.Value = base.["value"] :?> int
type Config() =
inherit ConfigurationSection()
[<ConfigurationProperty("responseTimeoutInMs")>]
member this.ResponseTimeInMs
with get() = base.["responseTimeoutInMs"] :?> ResponseTimeoutConfigElement
and set (value: ResponseTimeoutConfigElement) = base.["responseTimeoutInMs"] <- value

Utilize Message Template for Message Property Using Serilog

I've adopted Serilog for my logging needs.
I (do my best to) follow the SOLID principles and have thus adopted Steven's adapter which is an excellent implementation.
For the most part, this is great. I have a class called LogEntryDetail which contains certain properties:
class LogEntryDetail
{
public string Message {get;set;}
public string MessageTemplate {get;set;}
public string Properties {get;set;}
// etc. etc.
}
I will log the LogEntryDetail like this:
public void Log(LogEntryDetail logEntryDetail)
{
if (ReferenceEquals(null, logEntryDetail.Layer))
{
logEntryDetail.Layer = typeof(T).Name;
}
_logger.Write(ToLevel(logEntryDetail.Severity), logEntryDetail.Exception, logEntryDetail.MessageTemplate, logEntryDetail);
}
I am using the MSSqlServer sink (Serilog.Sinks.MSSqlServer) For error logging, all is well.
I have a perf logger, which I plug into my request pipeline. For this logger, I don't want to save every property in the LogEntry object. I only want to save the Message property in the Message column of the table which I have created.
So, normally, when you call write on the serilog logger and pass in a complex object, the Message column contains the whole object, serialized as JSON.
I want to know if there is some way that I can specify the MessageTemplate to be something like {Message} or {#Message}, so that the Message column in the database only contains the string stored in the Message property of the LogEntryDetail object. Any other property is redundant and a waste of storage space.
When I specify the MessageTemplate to be {Message}, the Message property contains the full name of the LogEntryDetail type (including namespace).
I feel like I am close and just missing some little thing in my comprehension of Serilog's MessageTemplate feature.
I'll just explain what I did here to try and get the best of both worlds. It seems here we have the age-old developer conundrum of sacrificing specific features of a library in order to comply with the SOLID principles. We've seen this before with things like repository abstractions which make it impossible to leverage the granular features of some of the ORMs which they abstract.
So, my SerilogAdapter looks like this:
public class SerilogLogAdapter<T> : ILogger
{
private readonly Serilog.ILogger _logger;
public SerilogLogAdapter(Serilog.ILogger logger)
{
_logger = logger;
}
public void Log(LogEntryDetail logEntryDetail)
{
if (ReferenceEquals(null, logEntryDetail.Layer))
{
logEntryDetail.Layer = typeof(T).Name;
}
if (logEntryDetail.MessageTemplate.Equals(MessageTemplates.LogEntryDetailMessageTemplate, StringComparison.Ordinal))
{
_logger.Write(ToLevel(logEntryDetail.Severity), logEntryDetail.Exception, logEntryDetail.MessageTemplate, logEntryDetail);
}
else
{
_logger.Write(ToLevel(logEntryDetail.Severity), logEntryDetail.MessageTemplate, logEntryDetail.Message, logEntryDetail.AdditionalInfo);
}
}
private static LogEventLevel ToLevel(LoggingEventType severity) =>
severity == LoggingEventType.Debug ? LogEventLevel.Debug :
severity == LoggingEventType.Information ? LogEventLevel.Information :
severity == LoggingEventType.Warning ? LogEventLevel.Warning :
severity == LoggingEventType.Error ? LogEventLevel.Error :
LogEventLevel.Fatal;
}
If the MessageTemplate is one which represents the whole object, then that will be logged. Otherwise, a custom MessageTemplate can be used and the Message property, along with the AdditionalInfo property (a dictionary) can be logged.
We at least squeeze one more thing out of Serilog, and it is one of its strengths - the ability log using different Message templates and to search the log by Message Template.
By all means let me know if it could be better!

MVC DataAnnotation Accept No Spaces

I'm developing a Login View in MVC5.
I would like to set at the ViewModel level a DataAnnotation to state the the field does NOT accept empty spaces.
Is there a Data Annotation in MVC (something like [NoSpaces] that can be used to NOT allow a string field to contain "spaces" ?
How about this:
[RegularExpression(#"^\S*$", ErrorMessage = "No white space allowed")]
Well, the simplest but robust thing I can think of is to look at how existing code works, or how existing data annotation work.
For example, let's look at the System.ComponentModel.DataAnnotations.StringLengthAttribute class.
Here's the definition only (just to keep short):
namespace System.ComponentModel.DataAnnotations
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class StringLengthAttribute : ValidationAttribute
{
public StringLengthAttribute(int maximumLength);
public int MinimumLength { get; set; }
public override string FormatErrorMessage(string name);
public override bool IsValid(object value);
}
}
So I would simply copy the original implementation source code from GitHub and customize it to my needs. For example, to obtain a signature like this (if I understood correctly and this is what you wanted):
public StringLengthAttribute(int maximumLength, int minLength = 0, allowEmptySpaces = true);
For more in-depth info, I would also read the Microsoft docs on the ValidationAttribute class, which is your base class for custom validation data annotations.
EDIT:
I would NOT rely on Regular Expressions to validate data in cases where I just need to exclude empty strings or strings containing only white space(s), because Regular Expressions are very expensive to process (and require a lot of memory allocation, because of an expression compiler, a state machine, etc.).
If this is convenient for anyone, I got pass this issue by doing this on the client:
//trim each form input
var $form = $("#myForm");
$form.find("input:text").each(function(){
var $self= $(this);
$self.va($self.val().trim());
});
//then validate before submit
if($form.valid()){ $form.submit(); }
// on the server ModelState.Isvalid does verify each item with [Required] and won't accept only white spaces from an input (but this means additional roundtrip to server and it's not always convenient)

The field must be a number. How to change this message to another language?

How can I change that messages for all int fields so that instead of saying:
The field must be a number in English, it shows:
El campo tiene que ser numerico in Spanish.
Is there are a way?
If you happen to be using ASP.NET MVC 4 onwards, check this post:
Localizing Default Error Messages in ASP.NET MVC and WebForms
Basically you have to add the following piece of code in your Application_Start() method in Global.asax:
ClientDataTypeModelValidatorProvider.ResourceClassKey = "Messages";
DefaultModelBinder.ResourceClassKey = "Messages";
Right click your ASP.NET MVC project in Solution Explorer inside Visual Studio and select Add => Add ASP.NET Folder => App_GlobalResources.
Now add a .resx file inside this folder called Messages.resx.
Finally add the following string resources in that .resx file:
Name Value
==== =====
FieldMustBeDate The field {0} must be a date.
FieldMustBeNumeric The field {0} must be a number.
PropertyValueInvalid The value '{0}' is not valid for {1}.
PropertyValueRequired A value is required.
You should be good to go.
Note that the value you're interested in is the FieldMustBeNumeric. To localize it to Spanish, you have to add another resource file named Messages.es.resx. In this specific .resx file replace the resource value with:
Name Value
==== =====
FieldMustBeNumeric El campo {0} tiene que ser numerico.
If you happen to be using ASP.NET MVC 3 downwards, this solution can help you achieve the same result: https://stackoverflow.com/a/2551481/114029
you can set your custom message for your validation.
[RegularExpression("\d{9}",ErrorMessage="El campo tiene que ser numerico")]
public decimal UnitPrice { get; set; }
If you want to specify custom message for each Integer , double and float . you can use Range Attribute with String as below.
[Required(ErrorMessageResourceType = typeof(Global), ErrorMessageResourceName = "YearOfEstablishmentRequired")]
[Range(0, int.MaxValue, ErrorMessageResourceType = typeof(Global), ErrorMessageResourceName = "ValidYearOfEstablishment")]
[Display(Name = "Year Of Establishment")]
public string YearOfEstablishment { get; set; }
Now as above you can specify custom message for each and every propery .
For those using Razor Pages, this code may help (should be placed in Program.cs or Startup.cs)
builder.Services.AddRazorPages().AddMvcOptions(options =>
{
options.ModelBindingMessageProvider.SetValueMustBeANumberAccessor(
_ => "Укажите численное значение!");
});

Grails data binding - command objects with Lists

Grails 1.3.7
Trouble with data binding Command objects that have List content. Example Command:
class Tracker {
String name
String description
List<Unit> units = new ArrayList()
}
class Unit {
String name
Long unitMax
Long unitMin
}
create GSP for Tracker has the Unit fields. One example:
<g:textField name="units[0].unitMax" value=""/>
TrackerController save method:
def save = { Tracker trackerInstance ->
trackerInstance = trackingService.saveOrUpdateTracker(trackerInstance)
}
But, always java.lang.IndexOutOfBoundsException
Alternatively, if I update controller to:
def save = {
Tracker trackerInstance = new Tracker()
trackerInstance.properties = params
....
Then groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: properties for class: com.redbrickhealth.dto.Tracker
Any ideas?
There seems to be a difference between binding in GORM vs Command objects.
Maybe I need to extend and register a PropertyEditorSupport for Unit?
-Todd
Since Groovy 1.8.7 the List interface has a method called withLazyDefault that can be used instead of apache commons ListUtils:
List<Unit> units = [].withLazyDefault { new Unit() }
This creates a new Unit instance every time units is accessed with a non-existent index.
See the documentation of withLazyDefault for more details. I also wrote a small blog post about this a few days ago.
Grails requires an command with existing list, that will be filled with data from reques.
If you know exact number of units, say 3, you can:
class Tracker {
String name
String description
List<Unit> units = [new Unit(), new Unit(), new Unit()]
}
or use LazyList from apache commons collections
import org.apache.commons.collections.ListUtils
import org.apache.commons.collections.Factory
class Tracker {
String name
String description
List<Unit> units = ListUtils.lazyList([], {new Unit()} as Factory)
}

Resources