Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 5 Remote Validation

Tags:

I need to validate an input field value from user before the form is submitted.

I have created an action in my custom controller and decorated the field with it:

action name: CheckValue controller name: Validate

[Remote("CheckValue", "Validate"), ErrorMessage="Value is not valid"] public string Value { get; set; } 

The problem is when I press submit, the form is being submitted and then the message Value is not valid is shown if the value entered by the user is not valid.

How can I validate the value entered by user and prevent the form to be submitted if value is not valid, and display the error message?

If I try in JavaScript to check if the form is valid $("#formId").valid() that returns true, that means no matter what is the status of the value (valid or not) the form is valid.

In the other hand if I decorate another field with the [Required] attribute the form is not submitted and the error is shown for that field that is required. However the validation doesn't occur behind the scene for the remote validation field.

like image 849
user2818430 Avatar asked Jul 21 '14 11:07

user2818430


People also ask

What is remote validation in MVC?

ASP.NET MVC 3 provides a mechanism that can make a remote server call in order to validate a form field without posting the entire form to the server. This is useful when you have a field that cannot be validated on the client and is therefore likely to fail validation when the form is submitted.

What is meant by remote validation?

Remote Validation is a technique that uses client side script to validate user input on the server without posting the entire form. It is typically used to compare the user input with a dynamic list of values. One example of its use would be to prevent duplicate user names being submitted.

How does ModelState IsValid work?

ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process. In your example, the model that is being bound is of class type Encaissement .

What is the purpose of remote attribute in MVC?

Remote is the attribute for validation in Data Annotation, which is used in model class to validate records instantly.


2 Answers

The complete solution of Remote Validation in MVC. It will check if the email exists in database and show the following error:

Email already exists

  1. Account Controller Action

    [AllowAnonymous] [HttpPost] public ActionResult CheckExistingEmail(string Email) {     try     {         return Json(!IsEmailExists(Email));     }     catch (Exception ex)     {         return Json(false);     } }  private bool IsEmailExists(string email)     =>  UserManager.FindByEmail(email) != null; 
  2. Add Model Validation

    [Required] [MaxLength(50)] [EmailAddress(ErrorMessage = "Invalid Email Address")] [System.Web.Mvc.Remote("CheckExistingEmail", "Account", HttpMethod = "POST", ErrorMessage = "Email already exists")] public string Email { get; set; } 
  3. Add Scripts

    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 
like image 103
adnan Avatar answered Sep 20 '22 12:09

adnan


You dont put the Controller code. But must to be something like this:

Your code:

[Remote("CheckValue", "Validate", ErrorMessage="Value is not valid")] public string Value { get; set; } 

My code for the controller(Validate):

public ActionResult CheckValue(string Value)         {             if (Value == "x value")             {         // This show the error message of validation and stop the submit of the form                 return Json(true, JsonRequestBehavior.AllowGet);             }             else             {         // This will ignore the validation and the submit of the form is gone to take place.                return Json(false, JsonRequestBehavior.AllowGet);             }         } 
like image 29
Ali Briceño Avatar answered Sep 18 '22 12:09

Ali Briceño