ASP.NET Data annotations validation - hide low level validations failures? - asp.net

I have a model on which I'm using validation by data annotations, like this :
public int Id { get; set; }
[Required(ErrorMessage = "Please enter the Sample Description.")]
public string Description { get; set; }
[Required(ErrorMessage = "Please enter the Start Date.")]
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }
Data gets inserted via an API and, if validation fails, I would like to take any returned error messages and put them directly out to the screen.
This is fine for something like this :
[Required(ErrorMessage = "Please enter the Sample Description.")]
public string Description { get; set; }
But in the case of the integer property shown, "Id", and the client sending, a value that can't be cast to an integer, for instance "x", then you get returned a low level message like this :
"Could not convert string to integer: x. Path 'Id', line 1, position 9.
What I really want is a message more like
"Please provide an integer for the Id".
Is there anyway to allow data validation by annotation and still provide a more useful message than what I'm getting ?

Few possible options:
(1)
In MVC 4 you can change the error message in the View with Html.ValidationMessageFor. Like:
#Html.ValidationMessageFor(model => model.YourIntField, "Please provide an integer for the Id".)
In model:
[DataType(DataType.Int)]
public int YourIntField { get; set; }
(2)
For any number validation you have to use different different range validation as per your requirements
For Integer
[Range(0, int.MaxValue, ErrorMessage = "Please enter valid integer Number")]
For Float:
[Range(0, float.MaxValue, ErrorMessage = "Please enter valid float Number")]
Ref: Int or Number DataType for DataAnnotation validation attribute

Related

Only Require input to be filled if other inputs are not filled

I have an ASP.NET application which requires the user to fill in a form to create a new object. It is defined as follows:
public class Address
{
public int ID { get; set; }
[Required]
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
[Required]
public string City { get; set; }
public string Postcode { get; set; }
public string AirportCode { get; set; }
}
The fields AddressLine1 and City are required when filling out the create form. However, a new development has introduced the field AirportCode, which changes the requirements. Basically, if an airport code value is given, no other values are required. If not, the address line 1 and city values are both still required. If the user neglected to fill out any of the form, I would expect all three fields to display an error message until wither the City and AddressLine1 values or the AirportCode value was given, but I can worry asbout the semantics of it later - the important thing is to not let an invalid object get through.
I guess the logic would be:
if AirportCode OR (AddressLine1 AND City)
Here is a small sample of the City input field. It's the default generated by the application:
<div class="form-group">
<label asp-for="City" class="col-md-2 control-label">City</label>
<div class="col-md-10">
<input asp-for="City" class="form-control" />
<span asp-validation-for="City" class="text-danger"></span>
</div>
</div>
Is there a way to go about implementing this without resorting to JavaScript? The form validation has been so neat and easy so far, and I would love to be able to do this through the framework. Thanks.
An afterthought: I may later on want to grey out the other fields if AirportCode started getting filled out. It's not strictly related to validation but it might tie into this?
You would need to write your own custom validation attribute.
Howerver there is a plugin called "ExpressiveAnnotations" which is very easy to use and helps you implement logic into your "DataAnnotations".
[RequiredIf("AirportCode == null",
ErrorMessage = "Your error message.")]
public string AddressLine1 { get; set; }
[RequiredIf("AirportCode == null",
ErrorMessage = "Your error message.")]
public string City { get; set; }
[RequiredIf("City == null")]
[RequiredIf("AddressLine1 == null")]
public string AirportCode { get; set; }
If you feel it would be useful for your purposes, more information about ExpressiveAnnotations library can be found here. Client side validation is also supported out of the box.
A small .NET and JavaScript library which provides annotation-based
conditional validation mechanisms. Given attributes allow to forget
about imperative way of step-by-step verification of validation
conditions in many cases. Since fields validation requirements are
applied as metadata, domain-related code is more condensed.

Return empty error message in validation

I want to return empty string for my validation in ASP.NET MVC 5 project. I have created a property in my view model:
[Required(ErrorMessageResourceType = typeof(TestResources), ErrorMessageResourceName = "Empty_Message", ErrorMessage = null)]
public int? TestProperty { get; set; }
Inside a view:
#Html.ValidationMessageFor(x => x.TestProperty)
When I try this one I get the default message "This field is required.". I want it to be empty in this case, because I add a class to error message's span element that shows up some icon.
I tried something like that:
[Required(ErrorMessage = "")]
public int? TestProperty { get; set; }
...but got an error:
Either ErrorMessageString or ErrorMessageResourceName must be set, but
not both.
Try setting a space. Example "Space". That would solve your problem.
[Required(ErrorMessage = " ")]
public int? TestProperty { get; set; }
you can use the following code:
[DisplayFormat(ConvertEmptyStringToNull=false)]
Reference for more details:
RequiredAttribute with AllowEmptyString=true in ASP.NET MVC 3 unobtrusive validation

Asp.Net MVC Validations Not working

[Display(Name = "SentDoc_lblDescription ", ResourceType = typeof(EXTDocuments.Resources))]
[Required(ErrorMessage="Description Required")]
public string Description {get;set;}
This Description Property is required in business logic.
When I am clicking submit button it throws an error.
The error is decription is null, I want to validate that property from within the UI.
I want to recall running into something like this and currently use this attribute set in order to ensure my strings get validated correctly:
[Required]
[MaxLength(200)]
[MinLength(5)]
[Display(Name = "Name")]
public string ItemName { get; set; }
By using the MinLength and Required, you get a required string with a minimum length. Hopefully this helps you out.

MVC models' unique field Create-Edit Complication

I have a model in MVC like that;
public class Employee
{
public int EmployeeId { get; set; }
[Required]
[EmailAddress]
[DisplayName("Email")]
[Remote("doesEmailExist", "Employee", HttpMethod = "POST", ErrorMessage = "Email already exists. Please enter a different Email.")]
public string Email { get; set; }
}
On create action, that is okey, it works great. But on edit action, program see email address already exist. And I cannot update my employee with the same email address. What can I do?
You could use the AdditionalFields property of RemoteAttribute to include EmployeeId in the validation. Then in your action method, if EmployeeId=null validation fails if a matching email is found, otherwise only check for matching emails where its a different EmployeeID

Prevent user from submitting default form values in .NET MVC3

I have the following model:
public class Contact
{
public Contact()
{
Name = "Your Name";
Email = "Your Email";
Message = "Your Message";
}
[Required]
[StringLength(60,MinimumLength = 3)]
public string Name { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[RegularExpression(#"\b[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b")]
public string Email { get; set; }
[Required]
[StringLength(2200, MinimumLength = 10)]
[DataType(DataType.MultilineText)]
public string Message { get; set; }
}
For Message and Name, their default values (in the constructor) actually pass validation, obviously that is bad. I know I could check for this and throw an error in the Controller, but I'm trying to find a way to do these in the model (as I assume that is the correct place to do it).
I wouldn't do this at all server side. Use a textbox watermark ala one of the many methods for ex.
http://code.google.com/p/jquery-watermark/
Those look like hints, not default values. You should implement these with javascript, instead of setting them as input values.

Resources