ASP MVC Remote Validation Removing Area - asp.net

I have the following Model:
public class UserModel
{
[Required]
public String Name {get; set; }
[Required]
public String Surname {get; set; }
[Required]
[Remote("ValidateIdNumber", "CustomValidation", "", ErrorMessage = "Invalid ID")]
public String IDNumber {get; set; }
}
I have a create user form that is the Admin area. So it's "/Admin/User/Create".
My "ValidateIdNumber" action is in the root in the controller "CustomValidation"
So to access it you would need to go to "/CustomValidation/ValidateIdNumber"
public Boolean ValidateIdNumber(String IDNumber) {
//Validate ID number and return result.
}
The problem is that when the form gets created, the remote validation targets "/Admin/CustomValidation/ValidateIdNumber".
<input class="text-box single-line" data-val="true" data-val-remote="Invalid ID" data-val-remote-additionalfields="*.IDNumber" data-val-remote-url="/Admin/CustomValidation/ValidateIdNumber" data-val-required="Please provide a ID Number" id="IDNumber" name="IDNumber" type="text" value="">
How can I remove the Area? I've tried making the area parameter in the Remote attribute an empty string("") and null.
any ideas? I want to try and keep my validation in the root and not in areas.

You can write a custom remote attribute , see here:
ASP.NET MVC 3 Remote Validation

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.

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.

The LoginModel RememberMe is being marked as required field

I am using the LoginModel inside asp.net mvc web application which looks as follow:-
public class LoginModel
{
[Required(ErrorMessage = "Required")]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required(ErrorMessage = "Required")]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
but the generated mark-up is as follow:-
<div class="input-prepend">
Remember Me ? <input data-val="true" data-val-required="The Remember me? field is required." id="RememberMe" name="RememberMe" type="checkbox" value="true" /><input name="RememberMe" type="hidden" value="false" />
</div>
So why the RememberMe is being marked as Required fields , although inside the model class there is no [Required] data annotation specified?
Change the property to use a nullable bool:
[Display(Name = "Remember me?")]
public bool? RememberMe { get; set; }
That way you declare that no value given to this property is OK, which means it can be optional. By using a normal bool, MVC renders a required error since that property must be either true or false.
EDIT:
Based on your comments, it seems that RememberMe should not be optional, since a NULL value is not satisfactory. In that regard, having RememberMe as bool is the way to go. Yes you will get the data-val-required stuff in the HTML, but it sounds like that's fine.
EDIT:
OK I see where you're coming from. You really don't want a red asterix next to the Remember Me checkbox then you must define RememberMe property as bool?
Then, when the user submits their data, if RememberMe.Value is null, can you manually set RememberMe.Value to false? Because if the user has not ticked the box, that means they do not want the site to remember them. If you can set the value to false yourself, so that you're not passing NULL to SetAuthCookie, then you'll be fine.

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.

ASP.NET MVC 3 Model Validation

I have a model called Organisation, on the organisation I have a remote validation attribute:
[Required(ErrorMessage = "The organisation name is required")]
[Remote("NameCheck", "Manage", "Organisations", ErrorMessage="That organisation already exists")]
public string Name { get; set; }
This checks that the name of the organisation someone is adding doesn't already exists. If it does then they get an error message saying so.
I'm using a strongly typed view to render the organisation "edit" view. Because someone is editing, I don't want that remote validation to run because of course the organisation will exist.
Is there any way to achieve this? Basically, turn off the remote validation in some way when editing an organisation and have it turned on when creating an organisation.
You could/SHOULD use different view models for the two views. So for example you will have CreateOrganizationViewModel and UpdateOrganizationViewModel. On the first view model the Name property will be decorated with the remote attribute whereas on the second view model it will not.
public class BaseOrganizationModel {
public int ID {get; set;}
}
public class UpdateOrganizationModel : BaseOrganizationModel {
[Required(ErrorMessage = "The organisation name is required")]
public string Name { get; set; }
}
public class InsertOrganizationModel : BaseOrganizationModel {
[Required(ErrorMessage = "The organisation name is required")]
[Remote("NameCheck", "Manage", "Organisations", ErrorMessage="That organisation already exists")]
public string Name { get; set; }
}

Resources