5

I would like to validate that Ville + Code-postal + Pays are unique.

If validation does not pass, I would like to mark fields as invalid (red as usually).

enter image description here

I already try a first implementation like below:

public class CityEditViewModel
{
    public int CityID { get; set; }

    [Required, Remote("CityAlreadyExists", "City", AdditionalFields = "CountryID, CityID, PostCode", ErrorMessageResourceName = "CityAlreadyExists", ErrorMessageResourceType = typeof(UserResource))]
    [Display(Name = "City", ResourceType = typeof(UserResource))]
    public string CityName { get; set; }

    [Required, Remote("CityAlreadyExists", "City", AdditionalFields = "CountryID, CityID, CityName", ErrorMessageResourceName = "CityAlreadyExists", ErrorMessageResourceType = typeof(UserResource))]
    [Display(Name = "PostCode", ResourceType = typeof(UserResource))]
    public string PostCode { get; set; }

    [Required, Remote("CityAlreadyExists", "City", AdditionalFields = "CityName, PostCode, CityID", ErrorMessageResourceName = "CityAlreadyExists", ErrorMessageResourceType = typeof(UserResource))]
    [Display(Name = "Country", ResourceType = typeof(UserResource))]
    public int CountryID { get; set; }

    public List<SelectListItem> Countries { get; set; }
}

But all the fields are not checked (validate) until I really change something in it. I need a solution where every time I change one of the 3 fields, the all 3 fields are validated and marked in red if needed.

I already check other Stackoverflow posts but did not found a solution to my specific problem.

Thanks for your help.

Bronzato
  • 9,438
  • 29
  • 120
  • 212
  • 1
    I have exactly the same scenario - did you find a solution to this? –  Sep 21 '12 at 07:50

2 Answers2

2

I had similar scenario too, this one helped me get going

$("#FirstName").change(function () {
        $('#CardNumber').removeData('previousValue');
        $('#CardNumber').valid();
  });

Multiple fields validation using Remote Validation

Community
  • 1
  • 1
Suchan Dongol
  • 95
  • 1
  • 11
1

Expanding on Suchan's answer, I wrote this helper method that finds each remotely validating element that has "additional fields," and then causes validation on said element to fire each time one of those fields changes.

// I hate naming things
function initializeRemotelyValidatingElementsWithAdditionalFields($form) {
    var remotelyValidatingElements = $form.find("[data-val-remote]");

    $.each(remotelyValidatingElements, function (i, element) {
        var $element = $(element);

        var additionalFields = $element.attr("data-val-remote-additionalfields");

        if (additionalFields.length == 0) return;

        var rawFieldNames = additionalFields.split(",");

        var fieldNames = $.map(rawFieldNames, function (fieldName) { return fieldName.replace("*.", ""); });

        $.each(fieldNames, function (i, fieldName) {
            $form.find("#" + fieldName).change(function () {
                // force re-validation to occur
                $element.removeData("previousValue");
                $element.valid();
            });
        });
    });
}

Call the function like so:

$(document).ready(function() {
    initializeRemotelyValidatingElementsWithAdditionalFields($("#myFormId"));
});
Chris Staley
  • 2,370
  • 1
  • 20
  • 21