Remote Validation not displaying the errors from

8 posts, 0 answers
  1. Yassen
    Yassen avatar
    22 posts
    Member since:
    Oct 2016

    Posted 01 Mar Link to this post

    Dear all,

    I had followed this useful link Remote Validation to apply the remote validation on MetaData class as follows;

    Metadata

    public class CoinMetadata
    {
        [Required(ErrorMessageResourceType = typeof(Errors), ErrorMessageResourceName = "NameReq")]
        [Display(Name = "CoinName", ResourceType = typeof(ScreenRes))]
        [Remote("IsValidName", "Coin", ErrorMessageResourceType = typeof(Errors), ErrorMessageResourceName = "NameDuplicate")]
        // AdditionalFields="coin_id", ErrorMessageResourceType = typeof(Errors), ErrorMessageResourceName = "NameDuplicate")
        public string name { get; set; }
    }

     

    The Controller:

    public JsonResult IsValidName(string name)
    {
        bool found = db.IGL_coin.Any(name => name.name == name);
        if (!found)
        {
            return Json(true, JsonRequestBehavior.AllowGet);
        }
        return Json(false, JsonRequestBehavior.AllowGet);
    }

    The error message from resource file doesn't show up, instead It shows false.

    How can I make it works?

  2. Stefan
    Admin
    Stefan avatar
    912 posts

    Posted 03 Mar Link to this post

    Hello Yassen,

    After inspecting the example I noticed that IsValidName is returning false instead of the validation message like in the provided example:

    string suggestedName = String.Format(CultureInfo.InvariantCulture,
                    "{0} is not available.", ProductName);
    .....
    return Json(suggestedName, JsonRequestBehavior.AllowGet);

    Please check if returning a string message instead of false will resolve the issue.

    Regards,
    Stefan
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Yassen
    Yassen avatar
    22 posts
    Member since:
    Oct 2016

    Posted 04 Mar in reply to Stefan Link to this post

    Hello Stefan,

    It does as a matter of fact !!,

    My question is that Why it can not return the error message from the data annotation ?

    regards

  4. Stefan
    Admin
    Stefan avatar
    912 posts

    Posted 07 Mar Link to this post

    Hello Yassen,
     
    The error message which will be should is a custom one and it is set in the validation controller.

    Please advise if the desired result is to return different message that the one set in the validation controller?

    Also, I can suggest checking the other available approaches for validation:

    http://docs.telerik.com/aspnet-mvc/getting-started/validation?wcmmode=disabled#common-scenarios

    Regards,
    Stefan
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  5. Yassen
    Yassen avatar
    22 posts
    Member since:
    Oct 2016

    Posted 10 Mar in reply to Stefan Link to this post

    Helllo Stefan,

    Yes, I need to return the message on the MetaData class defined by DataAnnotation attribute

    1.public class CoinMetadata
    2.{
    3.    [Required(ErrorMessageResourceType = typeof(Errors), ErrorMessageResourceName = "NameReq")]
    4.    [Display(Name = "CoinName", ResourceType = typeof(ScreenRes))]
    5.    [Remote("IsValidName", "Coin", ErrorMessageResourceType = typeof(Errors), ErrorMessageResourceName = "NameDuplicate")]
    6.    // AdditionalFields="coin_id", ErrorMessageResourceType = typeof(Errors), ErrorMessageResourceName = "NameDuplicate")
    7.    public string name { get; set; }
    8.}

     

    have a look at line number 5, the error message comes from a resource file.

    Best regards

  6. Stefan
    Admin
    Stefan avatar
    912 posts

    Posted 13 Mar Link to this post

    Hello Yassen,

    Please have in mind that the provided demo example is completely custom and it is based on the following MSDN documentation, and it is not directly related to Kendo UI:

    https://msdn.microsoft.com/en-us/library/gg508808(vs.98).aspx

    The message is coming from data-val-remote
      
    messages: {
     remote: function (input) {
      return input.attr("data-val-remote");
     }
    }

    Which respectively is coming from the success function:

    success: function (data) {
     if (data == true) {
      input.attr("data-val-remote", "");
     }
     else {
      input.attr("data-val-remote", data);
     }
     input.attr("data-val-remote-recieved", true);
     validator.validateInput(currentInput);
     
                            }

    If the success is not set, the standard data-val-remote attribute will be shown.

    Regards,
    Stefan
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  7. Yassen
    Yassen avatar
    22 posts
    Member since:
    Oct 2016

    Posted 16 Mar in reply to Stefan Link to this post

    Hello Stefan,

    Appreciate your rely.

    I have a very basic scenario, what should I do to show the errors from the resources attached using data annotation?

    Regards

  8. Stefan
    Admin
    Stefan avatar
    912 posts

    Posted 20 Mar Link to this post

    Hello Yassen,

    The link provided in a previous reply demonstrates the most common scenarios when working with validation and data annotations:

    http://docs.telerik.com/aspnet-mvc/getting-started/validation?wcmmode=disabled#common-scenarios

    Please advise if more information is needed on this matter.

    Regards,
    Stefan
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top